LoginSignup
33
31

More than 5 years have passed since last update.

iptablesの読み方覚え書き

Last updated at Posted at 2014-11-19

iptablesの機能を有効化する

# service iptables restart

iptablesの機能を自動的に起動する

# chkconfig iptables on

基本パケットの種類

  • INPUT(入力)
  • OUTPUT(出力)
  • FORWARD(転送)

チェーン

上の3種類のパケットはそれぞれ名前と同じ INPUT, OUTPUT, FORWARD のチェーンを通過し、一番最初にマッチした条件に動作が適用される。
(チェインは自分でも定義ができて、定義したチェインをINPUTなどのチェーンに繋げる事も可能)

パケットフィルタリング条件の表示

# cat /etc/sysconfig/iptables
・
・
COMMIT
*filter
:INPUT DROP [0:0]
・
・
#COMMIT

で、iptablesの内容を記述しているファイルを直接見れる。
パケットフィルタリングの条件は *filter から #COMMIT の間に記述されている。

ただ、ファイルを直接見るよりも以下のコマンドで見た方が整形されていて分かりやすい。

# iptables -v -L 
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
11835   17M ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED
    3   144 ACCEPT     icmp --  any    any     anywhere             anywhere
    2   120 ACCEPT     all  --  lo     any     anywhere             anywhere
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ss
  202 13759 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
・
・

Chain毎に空行区切りで表示される。
出力結果の一行目

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

は INPUT チェーンのデフォルトポリシーが ACCEPT で デフォルトポリシーが適用されたパケットの数とバイト数を表す。
ちなみに、INPUTチェーンの最後に全条件にマッチするREJECTがあるので、デフォルトポリシーが適用されるパケットは 0 packets, 0 bytes となっている。

その次の行以降にあるカラムはそれぞれ

  • target:適用されるアクション(ACCEPT, DROP, REJECT, LOG, ...)
  • prot:プロトコル
  • opt:オプション
  • in:受信するNIC(lo, eth0, ...)
  • out:送信するNIC(lo, eth0, ...)
  • source:送信元IP
  • destination:送信先IP

特定のチェインのパケットフィルタリング条件の表示

-L オプションの後に 引数でチェイン名を付けると、
特定のチェインのパケットフィルタリング条件だけを表示できる。

# iptables -v -L INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
11837   17M ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED
    3   144 ACCEPT     icmp --  any    any     anywhere             anywhere
    2   120 ACCEPT     all  --  lo     any     anywhere             anywhere
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ssh
  202 13759 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited
33
31
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
31