脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

iptablesの設定方法(FireWall)

iptablesの設定方法はググれば山ほど出てくるのですが、ちょっとだけ自分の環境と違ったり、ちょっとだけ入力したい設定が入ってない場合が多いので、自分のメモ的要素も含めて書いておきたいと思います。

iptables設定手順

確認

まずは現在のFireWall設定を確認します

iptables -L

初期化

設定前に一度既存の設定のを初期化します

### 初期化
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
確認
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

INPUTのポート設定

### まずは許可するポートを設定します
iptables -A INPUT -i lo -j ACCEPT  # 自分自身へのアクセス(必須)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  # 既にコネクションが確立されているものを許可
iptables -A INPUT -p icmp -j ACCEPT # icmpを許可(pingとかに必要)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # (ssh)
iptables -A INPUT -p tcp --dport 23 -j ACCEPT # (telnet)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # (http)
iptables -A INPUT -p tcp --dport 10000:10010 -j ACCEPT # 範囲指定する場合はxxx:yyyという指定方法にします
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT # (weblick)

### 次に上記で設定したポート以外からのアクセスは全て拒否(DROP)するように設定します
iptables -P INPUT DROP

(上の設定を行わずに"iptables -P INPUT DROP"実行すると、teratermとかから入れなくなります。)

確認
iptables -L

	Chain INPUT (policy DROP)
	target     prot opt source               destination
	ACCEPT     all  --  anywhere             anywhere
	ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
	ACCEPT     icmp --  anywhere             anywhere
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:telnet
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpts:ndmp:rxapi
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:hbci

	Chain FORWARD (policy ACCEPT)
	target     prot opt source               destination

	Chain OUTPUT (policy ACCEPT)
	target     prot opt source               destination

FOWARDの設定

  • 転送は特に使う必要がない場合はDROP(拒否)にしておきましょう
iptables -P FORWARD DROP

OUTPUTの設定

  • outputは逆に全て許可します。
iptables -P OUTPUT ACCEPT
確認
iptables -L

	Chain INPUT (policy DROP)
	target     prot opt source               destination
	ACCEPT     all  --  anywhere             anywhere
	ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
	ACCEPT     icmp --  anywhere             anywhere
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:telnet
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpts:ndmp:rxapi
	ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:hbci

	Chain FORWARD (policy DROP)
	target     prot opt source               destination

	Chain OUTPUT (policy ACCEPT)
	target     prot opt source               destination

設定の保存

(保存しないと起動毎に再設定が必要です。)

service iptables save

その他Tips

iptablesを停止する方法
/etc/rc.d/init.d/iptables stop
  • 本当に行き詰まった時など、iptablesを停止してやるとすんなり通ることがあります。(あくまで原因がFireWallにあるかの確認だけなのですぐに戻しましょう)
iptablesを起動する方法
/etc/rc.d/init.d/iptables start