iptables

ネットワーク

書き方

sudo iptables -L -n -v --line-numbers

いま動いている包の濾過規則を書き換えます。規則は上から下へ見て最初に合ったものが勝つので、ACCEPT のあとに足した DROP は永遠に効かず、保存しなければ再起動で消え、22 番を開ける前に既定方針を DROP にすると遠隔の機械で自分を締め出します。

よく使うオプション

オプション意味
-L -n -v --line-numbersList the rules with counters and the numbers you need for -D.
-A <CHAIN>Append to the end of a chain, where an earlier rule may already have decided.
-I <CHAIN> 1Insert at the top, which is usually what you meant.
-D <CHAIN> <n>Delete rule number n.
-p tcp --dport 22Match protocol and destination port.
-s 10.0.0.0/8Match a source address or range.
-j ACCEPT|DROP|REJECTWhat to do with a matching packet.
-P <CHAIN> DROPSet the default policy, applied when no rule matched.

sudo iptables -L -n -v --line-numbers

Read the current rules before changing anything.

sudo iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT

Guarantee your own SSH access first.

sudo iptables-save > /etc/iptables/rules.v4

Make the rules survive a reboot.

つながらないとき、どこまで行けたのかを絞ります — 名前が引けないのか、経路がないのか、ポートが閉じているのか。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. iptables は何をしますか。

いま動いている包の濾過規則を書き換えます。規則は上から下へ見て最初に合ったものが勝つので、ACCEPT のあとに足した DROP は永遠に効かず、保存しなければ再起動で消え、22 番を開ける前に既定方針を DROP にすると遠隔の機械で自分を締め出します。

Q. どう打ちますか。

sudo iptables -L -n -v --line-numbers — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは8個です。全部は man iptables にあります。このコマンドはネットワークの仲間です。

関連するコマンド

man ページ: man iptables