CentOS7インストールログ5:NICが2つついているマシンをルータにする
遥か昔に書いたSELinux6.5インストールログのつづき。漸く重い腰を上げて全部CentOS7にしたので備忘録。
管理しているのはCONDOR(いや、HTCondorという名前に変わったんだった)を走らせるためのジョブクラスタなので、CONDOR_MASTERを走らせるマシンにはNICが2つ乗っているマシンを使用し、ルータの機能を持たせてslaveマシンをその下にぶらさげたい。NICの名前をそれぞれem1, em2とする。
em1 -- XXX.XX.XX.XXX (global)
em2 -- 192.18.1.1/24
em1側にはグローバルIPを設定し、em2側はハブを通してslaveマシンが接続する。
なお、古い人間なのでfirewalldを使わないため、以下のおまじないが最初に必要。
# systemctl stop firewalld # systemctl disable firewalld # yum install iptables-services
このマシンにぶらさがるslaveマシンのiptables設定は以下の記事で。
Scientific Linux 6.3またはCentOS7インストールログ1:インストール - 渡米生活。(日記)
めんどうなのでGUIでipアドレスをまず設定
em1側は普通にglobal addressの設定方法でよい
em2側は以下のとおり
ip_address 192.168.1.1
netmask 255.255.255.0
gateway 192.168.1.1
DNS これはglobal側の設定と同じでもよし
IP フォワーディング設定
# echo 1 > /proc/sys/net/ipv4/ip_forward # echo 0 > /proc/sys/net/ipv4/conf/em1/rp_filter # echo 0 > /proc/sys/net/ipv4/conf/em2/rp_filter
下2行はCentOS7から有効になったスプーフィング対策フィルタを無効化する呪文。これをやらないとFORWARDが通らないそうな。
ここまでやっておいて、ip_tablesを書き換える。
ちなみに下のスクリプトは上の3行が埋め込んである。
#! /bin/sh anyhost='0.0.0.0/0' internal_ip='192.168.1.0/24' my_global_ip=`ifconfig em1 | grep broadcast | awk '{print $2}' | sed -e 's/addr://'` my_internal_ip=`ifconfig em2 | grep broadcast | awk '{print $2}' | sed -e 's/addr://'` # trust all internal nodes mu01='XXX.XXX.XXX.XXX' mu02=XXX.XXX.XXX.XXX' trusted="$mu01 $mu02" # Make router function ON echo 1 > /proc/sys/net/ipv4/ip_forward # switch off spoofing filter echo 0 > /proc/sys/net/ipv4/conf/em1/rp_filter echo 0 > /proc/sys/net/ipv4/conf/em2/rp_filter ############## #Flush & Reset ############## iptables -F iptables -t nat -F iptables -X ############## #Deafult Rule ############## iptables -P INPUT DROP #iptables -P INPUT ACCEPT # allow established connection iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -P OUTPUT ACCEPT iptables -P FORWARD DROP # accept all packets between internal addresses iptables -A FORWARD -i em2 -o em1 -s $internal_ip -j ACCEPT #iptables -A FORWARD -i em1 -o em2 -d $internal_ip -j ACCEPT iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT #------------- # accept local #------------- iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT #--------------------------------------- # accept all packets from local machines #--------------------------------------- /sbin/iptables -A INPUT -i em2 -s $internal_ip -j ACCEPT /sbin/iptables -A OUTPUT -o em2 -d $internal_ip -j ACCEPT for i in $trusted; do /sbin/iptables -A INPUT -i em1 -s $i -j ACCEPT /sbin/iptables -A OUTPUT -o em1 -d $i -j ACCEPT done #------------ # allow ping #------------ # inbound iptables -A INPUT -p icmp --icmp-type echo-request -d $my_internal_ip -j ACCEPT iptables -A OUTPUT -p icmp --icmp-type echo-reply -s $my_internal_ip -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-request -d $my_global_ip -j ACCEPT iptables -A OUTPUT -p icmp --icmp-type echo-reply -s $my_global_ip -j ACCEPT # outbound iptables -A OUTPUT -p icmp --icmp-type echo-request -s $my_internal_ip -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-reply -d $my_internal_ip -j ACCEPT iptables -A OUTPUT -p icmp --icmp-type echo-request -s $my_global_ip -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-reply -d $my_global_ip -j ACCEPT #------------------------------- # allow ssh #------------------------------- # inbound iptables -A INPUT -p tcp -s $anyhost -d $my_internal_ip --dport 22 -j ACCEPT iptables -A OUTPUT -p tcp -s $my_internal_ip -d $anyhost --sport 22 -j ACCEPT iptables -A INPUT -p tcp -s $anyhost -d $my_global_ip --dport 22 -j ACCEPT iptables -A OUTPUT -p tcp -s $my_global_ip -d $anyhost --sport 22 -j ACCEPT # outbound #/sbin/iptables -A INPUT -p tcp -s $anyhost -d $my_internal_ip --sport 22 -j ACCEPT #/sbin/iptables -A OUTPUT -p tcp -s $my_internal_ip -d $anyhost --dport 22 -j ACCEPT #/sbin/iptables -A INPUT -p tcp -s $anyhost -d $my_global_ip --sport 22 -j ACCEPT #/sbin/iptables -A OUTPUT -p tcp -s $my_global_ip -d $anyhost --dport 22 -j ACCEPT #------------ # allow DNS #------------ iptables -A INPUT -p udp --sport 53 -j ACCEPT iptables -A OUTPUT -p udp --dport 53 -j ACCEPT iptables -A INPUT -p tcp --sport 53 -j ACCEPT iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT ################# #SNAT(masquerade) ################# iptables -t nat -A POSTROUTING -o em1 -s $internal_ip -j MASQUERADE ################################################ #Outgoing packet should be real internet Address ################################################ iptables -A OUTPUT -o em1 -d 10.0.0.0/8 -j DROP iptables -A OUTPUT -o em1 -d 176.16.0.0/12 -j DROP iptables -A OUTPUT -o em1 -d 192.168.0.0/16 -j DROP iptables -A OUTPUT -o em1 -d 127.0.0.0/8 -j DROP ######### #logging ######### iptables -N LOGGING iptables -A LOGGING -j LOG --log-level warning --log-prefix "DROP:" -m limit iptables -A LOGGING -j DROP iptables -A INPUT -j LOGGING iptables -A FORWARD -j LOGGING /sbin/service iptables save