本日も乙

ただの自己満足な備忘録。

iptablesの設定

iptablesでファイアーウォールを設定する。 具体的にはサーバーのポート番号を限定する。

  1. iptablesがインストールされているか確認する。
  2. $ yum list installed | grep iptables
    
    iptables.x86_64       1.4.7-5.1.el6_2   @updates/6.2
    iptables-ipv6.x86_64  1.4.7-5.1.el6_2   @updates/6.2
    
  3. インストールされている場合、現在のiptables設定を確認する。
  4. $ sudo iptables -L
    
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination   
    
  5. iptablesの設定
  6. $ sudo vim /etc/sysconfig/iptables
    
    以下の内容を入力する
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    :FIREWALL - [0:0]
    
    -A INPUT -j FIREWALL
    -A FORWARD -j FIREWALL
    -A FIREWALL -i lo -j ACCEPT
    -A FIREWALL -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 4 -j ACCEPT
    -A FIREWALL -p udp --sport 53 -j ACCEPT
    -A FIREWALL -p icmp --icmp-type any -j ACCEPT
    -A FIREWALL -p 50 -j ACCEPT
    -A FIREWALL -p 51 -j ACCEPT
    -A FIREWALL -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
    -A FIREWALL -p udp -m udp --dport 631 -j ACCEPT
    -A FIREWALL -p tcp -m tcp --dport 631 -j ACCEPT
    -A FIREWALL -m state --state ESTABLISHED,RELATED -j ACCEPT
    
    # SSH, HTTP, HTTPS, MySQL, smtp
    # 必要なポートだけ許可する
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 【SSHのポート番号】  -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 80    -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 443   -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 25    -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 110   -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 465   -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 995   -j ACCEPT
    
    -A FIREWALL -j REJECT --reject-with icmp-host-prohibited
    
    COMMIT
    
    ・空行にスペースがあるとiptables再起動時にエラーが発生するので注意する。 ・メールサーバーも立てるので25, 110, 465, 995も開けている。 追記(2012年12月3日) ・MySQLのポートは基本的に開けない。レプリケーション等を行う場合は、接続先のみ許可するように設定する。
  7. 再起動
  8. $ sudo /etc/rc.d/init.d/iptables restart
                                          
    iptables: ファイアウォールルールを消去中:                  [  OK  ]
    iptables: チェインをポリシー ACCEPT へ設定中filter         [  OK  ]
    iptables: モジュールを取り外し中:                          [  OK  ]
    iptables: ファイアウォールルールを適用中:                  [  OK  ]
    
  9. 確認
  10. $ sudo iptables -L
                                          
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    FIREWALL   all  --  anywhere             anywhere            
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    FIREWALL   all  --  anywhere             anywhere            
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain FIREWALL (2 references)
    target     prot opt source               destination         
    ACCEPT     all  --  anywhere             anywhere            
    ACCEPT     icmp --  anywhere             anywhere            icmp echo-request limit: avg 1/sec burst 4 
    ACCEPT     udp  --  anywhere             anywhere            udp spt:domain 
    ACCEPT     icmp --  anywhere             anywhere            icmp any 
    ACCEPT     esp  --  anywhere             anywhere            
    ACCEPT     ah   --  anywhere             anywhere            
    ACCEPT     udp  --  anywhere             224.0.0.251         udp dpt:mdns 
    ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipp 
    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ipp 
    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:【SSHのポート番号】
    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http 
    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:https 
    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:smtp 
    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:pop3 
    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:urd 
    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:pop3s 
    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
    

参考URL:さくらのVPS を使いはじめる 3 – iptables を設定する