LoginSignup
4
6

More than 5 years have passed since last update.

iptablesメモ

Last updated at Posted at 2015-09-06

iptables に関する小難しい説明の記事は見飽きた。mangle テーブルがあまり使用されないのは解ったから、何を設定すれば良いのか教えてほしい。

注意

当たり前だが、設定を誤るとリモート接続できなくなるので注意すること。

テーブル

いくつかある。最初は filter テーブルだけ覚えると良い。これからiptablesを覚えようとする人は大抵パケットフィルタリングを第一の目的としている。それ以外の用途もあることは頭の片隅にでも置いておくと良い。

ただし iptables は filter テーブルをデフォルトとしているので、サンプルのコマンドでは省略されていることが多い。明示するのであれば -t, --table オプションを使用する。

iptables -t filter ...

チェイン(ルール)

INPUT を覚えておけば良い。チェインはルール(フィルタ)が数珠つなぎになってるものである。パケットは、チェインの先頭にあるルールから順に評価され、最終的に ACCEPT されるか DROP されるか決定される。INPUT チェインはサーバに到着したパケットに対して適用されるチェインである。チェインに対して、ルールを追加(--append, -A)・削除(--delete, -D)・挿入(--insert, -I)できる。

iptables -t filter -A INPUT ...
iptables -t filter -D INPUT 1 ... # チェインの何番目のルールを削除するか指定する
iptables -t filter -I INPUT 1 ... # チェインの何番目にルールを追加するか指定する。

マッチ

パケットがルールに合致することをマッチという。マッチする条件の指定方法は多岐にわたり、この部分が iptables の理解を妨げていると思われる。

2015/09/07 早速理解が誤っていたので下記の例を修正した。dport オプションはプロトコル tcp や udp が指定されている場合のみ指定可能なよう。

# テーブルの指定は省略する
iptables -A INPUT --protocol tcp ... # プロトコルが tcp のパケット
iptables -A INPUT --protocol tcp --dport 80 ... # dport は tcp のオプション

ターゲット

ルールにマッチしたパケットは、ターゲットと呼ばれる処理に送られる。ターゲットは ACCEPT(受理)と DROP(破棄)を覚えておくと良い。指定するには --jump, -j を使用する。

iptables -A INPUT -p tcp --dport 80 --jump ACCEPT

設定の保存

iptables は再起動すると設定が初期化される。設定を保存したいところだが、デフォルトを削除してしまうのは怖い(未熟)のでホームディレクトリに上記のコマンドを列挙したスクリプトを配置している。

参考

iptables の設定は -nvL オプションで詳細表示できる。以下は(たぶん)初期状態の iptables の設定。

[root@test ~]# iptables --version
iptables v1.4.7
[root@test ~]#
[root@test ~]# uname -a
Linux test 2.6.32-504.30.3.el6.x86_64 #1 SMP Wed Jul 15 10:13:09 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@test ~]#
[root@test ~]#
[root@test ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
11628  946K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
   14   840 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
    1    52 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
 1301  155K REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 9331 packets, 11M bytes)
 pkts bytes target     prot opt in     out     source               destination
[root@test ~]#
4
6
0

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
4
6