「iptables」の版間の差分
提供: セキュリティ
| 行40: | 行40: | ||
== 使い方 == | == 使い方 == | ||
| + | |||
| + | === chkconfig の確認 === | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | $ chkconfig --list iptables | ||
| + | iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === 起動時に常に起動する === | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | % sudo chkconfig iptables on | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === iptables のコマンドラインオプション === | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | $ iptables --h | ||
| + | iptables v1.4.7 | ||
| + | |||
| + | Usage: iptables -[AD] chain rule-specification [options] | ||
| + | iptables -I chain [rulenum] rule-specification [options] | ||
| + | iptables -R chain rulenum rule-specification [options] | ||
| + | iptables -D chain rulenum [options] | ||
| + | iptables -[LS] [chain [rulenum]] [options] | ||
| + | iptables -[FZ] [chain] [options] | ||
| + | iptables -[NX] chain | ||
| + | iptables -E old-chain-name new-chain-name | ||
| + | iptables -P chain target [options] | ||
| + | iptables -h (print this help information) | ||
| + | |||
| + | Commands: | ||
| + | Either long or short options are allowed. | ||
| + | --append -A chain Append to chain | ||
| + | --delete -D chain Delete matching rule from chain | ||
| + | --delete -D chain rulenum | ||
| + | Delete rule rulenum (1 = first) from chain | ||
| + | --insert -I chain [rulenum] | ||
| + | Insert in chain as rulenum (default 1=first) | ||
| + | --replace -R chain rulenum | ||
| + | Replace rule rulenum (1 = first) in chain | ||
| + | --list -L [chain [rulenum]] | ||
| + | List the rules in a chain or all chains | ||
| + | --list-rules -S [chain [rulenum]] | ||
| + | Print the rules in a chain or all chains | ||
| + | --flush -F [chain] Delete all rules in chain or all chains | ||
| + | --zero -Z [chain [rulenum]] | ||
| + | Zero counters in chain or all chains | ||
| + | --new -N chain Create a new user-defined chain | ||
| + | --delete-chain | ||
| + | -X [chain] Delete a user-defined chain | ||
| + | --policy -P chain target | ||
| + | Change policy on chain to target | ||
| + | --rename-chain | ||
| + | -E old-chain new-chain | ||
| + | Change chain name, (moving any references) | ||
| + | Options: | ||
| + | [!] --proto -p proto protocol: by number or name, eg. `tcp' | ||
| + | [!] --source -s address[/mask][...] | ||
| + | source specification | ||
| + | [!] --destination -d address[/mask][...] | ||
| + | destination specification | ||
| + | [!] --in-interface -i input name[+] | ||
| + | network interface name ([+] for wildcard) | ||
| + | --jump -j target | ||
| + | target for rule (may load target extension) | ||
| + | --goto -g chain | ||
| + | jump to chain with no return | ||
| + | --match -m match | ||
| + | extended match (may load extension) | ||
| + | --numeric -n numeric output of addresses and ports | ||
| + | [!] --out-interface -o output name[+] | ||
| + | network interface name ([+] for wildcard) | ||
| + | --table -t table table to manipulate (default: `filter') | ||
| + | --verbose -v verbose mode | ||
| + | --line-numbers print line numbers when listing | ||
| + | --exact -x expand numbers (display exact values) | ||
| + | [!] --fragment -f match second or further fragments only | ||
| + | --modprobe=<command> try to insert modules using this command | ||
| + | --set-counters PKTS BYTES set the counter during insert/append | ||
| + | [!] --version -V print package version. | ||
| + | </syntaxhighlight> | ||
=== iptables の設定 === | === iptables の設定 === | ||
| 行61: | 行143: | ||
# Completed on Sat Apr 27 15:31:38 2013 | # Completed on Sat Apr 27 15:31:38 2013 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | * どこからでも [[ssh]] のアクセスを許可する | ||
| + | * ローカルホストからのアクセスを許可する | ||
| + | * それ以外は、許可しない | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | *filter | ||
| + | :INPUT DROP [0:0] | ||
| + | :FORWARD DROP [0:0] | ||
| + | :OUTPUT ACCEPT [0:0] | ||
| + | -A INPUT -i lo -j ACCEPT | ||
| + | -A INPUT -p tcp --dport 22 -j ACCEPT | ||
| + | -A OUTPUT -o lo -j ACCEPT | ||
| + | COMMIT | ||
| + | </syntaxhighlight> | ||
| + | |||
=== iptables が動かない === | === iptables が動かない === | ||
| 行80: | 行178: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| − | -A | + | -A INPUT -p tcp --dport 80 -j ACCEPT |
| − | -A | + | -A INPUT -p tcp --dport 443 -j ACCEPT |
| + | </syntaxhighlight> | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | *filter | ||
| + | :INPUT DROP [0:0] | ||
| + | :FORWARD DROP [0:0] | ||
| + | :OUTPUT ACCEPT [0:0] | ||
| + | -A INPUT -i lo -j ACCEPT | ||
| + | -A INPUT -p tcp --dport 22 -j ACCEPT | ||
| + | -A INPUT -p tcp --dport 80 -j ACCEPT | ||
| + | -A INPUT -p tcp --dport 443 -j ACCEPT | ||
| + | -A OUTPUT -o lo -j ACCEPT | ||
| + | COMMIT | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === DNS を許可する === | ||
| + | |||
| + | [[Domain Name System|DNS]] を許可する例です。 | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | -A INPUT -p tcp --dport 53 -j ACCEPT | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | *filter | ||
| + | :INPUT DROP [0:0] | ||
| + | :FORWARD DROP [0:0] | ||
| + | :OUTPUT ACCEPT [0:0] | ||
| + | -A INPUT -i lo -j ACCEPT | ||
| + | -A INPUT -p tcp --dport 22 -j ACCEPT | ||
| + | -A INPUT -p tcp --dport 53 -j ACCEPT | ||
| + | -A OUTPUT -o lo -j ACCEPT | ||
| + | COMMIT | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| 行101: | 行232: | ||
== iptables の設定の保存と復元 == | == iptables の設定の保存と復元 == | ||
| − | iptables-save | + | [[iptables-save]] コマンドでは、[[iptables]]の設定を標準出力に出します。 |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
iptables-save > iptables.save | iptables-save > iptables.save | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | iptables-restore | + | [[iptables-restore]] は、[[iptables]] の設定を読み込み、[[iptables]] に反映します。 |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
iptables-restore < iptables.save | iptables-restore < iptables.save | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | iptables-save の実行例。 | + | [[iptables-save]] の実行例。 |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
% iptables-save | % iptables-save | ||
| 行122: | 行253: | ||
# Completed on Sat Apr 27 15:26:09 2013 | # Completed on Sat Apr 27 15:26:09 2013 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
| + | == iptables の設定を保存する == | ||
| + | |||
| + | [[iptables]] コマンドで設定した情報は、 save を実行しないと保存されません。 | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | % sudo iptables save | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | == iptables の設定をクリアする == | ||
| + | |||
| + | -F もしくは --flush を使います。 | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | % sudo iptables -F | ||
| + | </syntaxhighlight> | ||
| + | |||
== 関連項目 == | == 関連項目 == | ||
* [[ファイアーウォール]] | * [[ファイアーウォール]] | ||
| + | * [[lokkit]] | ||
2013年5月4日 (土) 23:45時点における版
iptables は、Linux に実装されたIPv4用のパケットフィルタリングやネットワーク変換機能、または、コマンドのことです。 IPv6 用には、 ip6tables があります。
読み方
- iptables
目次
概要
iptables は、Linux の ファイアーウォールとして利用できます。
テーブル
フィルタリング用フィルターテーブル
- FORWARD
- フォワードするパケット
- INPUT
- 受信するパケット
- OUTPUT
- 送信するパケット
アドレス変換NATテーブル
- PREROUTING
- 送信時に変換するチェイン
- POSTROUTING
- 受信時に変換するチェイン
- OUTPUT
- 送信するパケット
インストール
CentOSにインストールする場合
sudo yum -y install iptables
設定ファイル
iptables の設定ファイルは、下記の場所にあります。
/etc/sysconfig/iptables-config
iptables の制御
iptables サービスの開始。
sudo service iptables start
iptables サービスの停止。
sudo service iptables stop
iptables サービスの再起動。
sudo service iptables restart
使い方
chkconfig の確認
$ chkconfig --list iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
起動時に常に起動する
% sudo chkconfig iptables on
iptables のコマンドラインオプション
$ iptables --h iptables v1.4.7 Usage: iptables -[AD] chain rule-specification [options] iptables -I chain [rulenum] rule-specification [options] iptables -R chain rulenum rule-specification [options] iptables -D chain rulenum [options] iptables -[LS] [chain [rulenum]] [options] iptables -[FZ] [chain] [options] iptables -[NX] chain iptables -E old-chain-name new-chain-name iptables -P chain target [options] iptables -h (print this help information) Commands: Either long or short options are allowed. --append -A chain Append to chain --delete -D chain Delete matching rule from chain --delete -D chain rulenum Delete rule rulenum (1 = first) from chain --insert -I chain [rulenum] Insert in chain as rulenum (default 1=first) --replace -R chain rulenum Replace rule rulenum (1 = first) in chain --list -L [chain [rulenum]] List the rules in a chain or all chains --list-rules -S [chain [rulenum]] Print the rules in a chain or all chains --flush -F [chain] Delete all rules in chain or all chains --zero -Z [chain [rulenum]] Zero counters in chain or all chains --new -N chain Create a new user-defined chain --delete-chain -X [chain] Delete a user-defined chain --policy -P chain target Change policy on chain to target --rename-chain -E old-chain new-chain Change chain name, (moving any references) Options: [!] --proto -p proto protocol: by number or name, eg. `tcp' [!] --source -s address[/mask][...] source specification [!] --destination -d address[/mask][...] destination specification [!] --in-interface -i input name[+] network interface name ([+] for wildcard) --jump -j target target for rule (may load target extension) --goto -g chain jump to chain with no return --match -m match extended match (may load extension) --numeric -n numeric output of addresses and ports [!] --out-interface -o output name[+] network interface name ([+] for wildcard) --table -t table table to manipulate (default: `filter') --verbose -v verbose mode --line-numbers print line numbers when listing --exact -x expand numbers (display exact values) [!] --fragment -f match second or further fragments only --modprobe=<command> try to insert modules using this command --set-counters PKTS BYTES set the counter during insert/append [!] --version -V print package version.
iptables の設定
/etc/sysconfig/iptables の設定例。
- プライベートアドレスからの ssh のアクセスを許可する
- ローカルホストからのアクセスを許可する
- それ以外は、許可しない
# Generated by iptables-save v1.4.7 on Sat Apr 27 15:31:38 2013 *filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT -A OUTPUT -o lo -j ACCEPT COMMIT # Completed on Sat Apr 27 15:31:38 2013
- どこからでも ssh のアクセスを許可する
- ローカルホストからのアクセスを許可する
- それ以外は、許可しない
*filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -p tcp --dport 22 -j ACCEPT -A OUTPUT -o lo -j ACCEPT COMMIT
iptables が動かない
service iptables start で iptables が動かないことに遭遇するかもしれません。 /etc/sysconfig/iptables の設定ファイルが存在しないとき、iptables のサービスは起動できません。
iptables の設定と起動
% sudo cp ./iptables /etc/sysconfig/iptables % sudo service iptables start iptables: Applying firewall rules: [ OK ]
HTTPとHTTPSを許可する
HTTP (80)と HTTPS (443) を許可する例です。
-A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT
*filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -p tcp --dport 22 -j ACCEPT -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT -A OUTPUT -o lo -j ACCEPT COMMIT
DNS を許可する
DNS を許可する例です。
-A INPUT -p tcp --dport 53 -j ACCEPT
*filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -p tcp --dport 22 -j ACCEPT -A INPUT -p tcp --dport 53 -j ACCEPT -A OUTPUT -o lo -j ACCEPT COMMIT
icmp echo requestを許可する
-A INPUT -p icmp --icmp-type echo-request -j ACCEPT
回数制限を行う
1秒間に4回までアクセスを許可する例です。
-A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 4 -j ACCEPT
sshd に対して行うと、総当たり攻撃 を受けている時に、自分自身もログインができなくなるので、やるべきではないでしょう。
iptables の設定の保存と復元
iptables-save コマンドでは、iptablesの設定を標準出力に出します。
iptables-save > iptables.save
iptables-restore は、iptables の設定を読み込み、iptables に反映します。
iptables-restore < iptables.save
iptables-save の実行例。
% iptables-save # Generated by iptables-save v1.4.7 on Sat Apr 27 15:26:09 2013 *filter :INPUT ACCEPT [79:6000] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [66:7992] COMMIT # Completed on Sat Apr 27 15:26:09 2013
iptables の設定を保存する
iptables コマンドで設定した情報は、 save を実行しないと保存されません。
% sudo iptables save
iptables の設定をクリアする
-F もしくは --flush を使います。
% sudo iptables -F