2016年5月3日火曜日

iptables にルールを追加する

サーバの設定をしていると、iptablesの設定変更はよく行うが、結構簡単に忘れてしまうので、メモ。
  1. 設定のバックアップ
    # cp -a /etc/sysconfig/iptables{,.`date +%Y%m%d`}
    # ll /etc/sysconfig/iptables*
    -rw-------. 1 root root  476  4月 30 07:17 2016 /etc/sysconfig/iptables
    -rw-------. 1 root root 1974  7月 24 11:10 2015 /etc/sysconfig/iptables-config
    -rw-------. 1 root root  476  4月 30 07:17 2016 /etc/sysconfig/iptables.20160503
    -rw-------. 1 root root  476  4月 30 07:17 2016 /etc/sysconfig/iptables.old
    
  2. iptablesの起動
    # service iptables start
    iptables: ファイアウォールルールを適用中:                  [  OK  ]
    
  3. 現在のルールを確認する
    # iptables -L --line-numbers
    Chain INPUT (policy ACCEPT)
    num  target     prot opt source               destination
    1    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
    2    ACCEPT     icmp --  anywhere             anywhere
    3    ACCEPT     all  --  anywhere             anywhere
    4    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
    5    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
    (以下略)
    
  4. 新ルールを追加
    下記コマンドで新ルールを追加する。
    # iptables -I [チェインの種類] [ルール番号] -p [プロトコル] -m [プロトコル] --dport [ポート番号] -j ACCEPT
    
    今回は、ルール番号5に80番ポートを開放するルールを追加する。
    # iptables -I INPUT 5 -p tcp -m tcp --dport 80 -j ACCEPT
    
  5. 新ルールを確認
    # iptables -L --line-numbers
    Chain INPUT (policy ACCEPT)
    num  target     prot opt source               destination         
    1    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
    2    ACCEPT     icmp --  anywhere             anywhere            
    3    ACCEPT     all  --  anywhere             anywhere            
    4    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
    5    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 
    6    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
    (以下略)
    
  6. 設定を保存
    このままでは、iptablesを再起動すると設定が消えてしまう。下記コマンドを実行し、設定を保存する。
    # service iptables save
    iptables: ファイアウォールのルールを /etc/sysconfig/iptable[  OK  ]中:
    
  • ルールを削除する時は
    # iptables -D [チェインの種類] [ルール番号]
    

0 件のコメント:

コメントを投稿