VPSならラピッドサイト:ホーム > カスタマーサポート > オンラインマニュアル > RV-7シリーズ マニュアル > リファレンスマニュアル > FireWall・TCPwrapper > 04) iptables 設定
04) iptables 設定
3つのテーブルと組み込みチェイン
iptablesには以下3つの"テーブル"があります。filterテーブルがデフォルトテーブルとなり、テーブルを指定せずにiptablesコマンドを実行した場合にはこれが使われます。テーブルによって使用できる"チェイン"が異なります。
RV-7シリーズのiptablesではINPUTとOUTPUTチェインを使用しますので、filterテーブルを使います。
RV-7シリーズのiptablesではINPUTとOUTPUTチェインを使用しますので、filterテーブルを使います。
テーブル
| filter | INPUT,FORWARD,OUTPUT |
|---|---|
| nat | PREROUTING,OUTPUT,POSTROUTING |
| mangle | PREROUTING,OUTPUT |
チェイン
| INPUT | 入力(受信)パケット |
|---|---|
| OUTPUT | 出力(送信)パケット |
| FORWARD | フォワードするパケット |
| PREROUTING | 受信時を変換するチェイン |
| POSTROUTING | 送信時に変換するチェイン |
iptablesコマンド
パケットフィルタリングを設定するiptablesコマンドはオプションとパラメータを組み合わせてルールを作成します。
オプション
| -A | チェインの最後にルールを追加 |
|---|---|
| -D | チェインからルールを削除 |
| -P | チェインのポリシーを指定したターゲットに設定 |
パラメータ
| -p | プロトコル(tcp,udp,icmp,all)を指定 |
|---|---|
| -s | 送信元(source)IPを指定。ネットマスクとホスト名も指定可 |
| -d | 接続先(destination)IPを指定。ネットマスクとホスト名も指定可 |
| -j | パケットが条件にマッチしたときのターゲットを指定 |
| --sport | 送信元ポート(source port)を指定 |
| --dport | 送信先ポート(destination port)を指定 |
ターゲット
| ACCEPT | パケットを許可 |
|---|---|
| DROP | パケットを破棄 |
| REJECT | パケットを拒否、ICMPエラーパケットを送信 |
iptablesの設定手順
1) パケットフィルタリングルールをクリア
2) パケットフィルタリングルールを追加
3) ポリシーを設定 (その他全ポートを閉じる)
4) iptablesの保存と再起動
2) パケットフィルタリングルールを追加
3) ポリシーを設定 (その他全ポートを閉じる)
4) iptablesの保存と再起動
iptablesではINPUTとOUTPUTをセットで設定する必要があります。
対象サービスへのパケットの送受信を制御しますのでINPUTとOUTPUTをセットで許可するよう設定してください。
対象サービスへのパケットの送受信を制御しますのでINPUTとOUTPUTをセットで許可するよう設定してください。
1) パケットフィルタリングルールをクリア
[root@linux ~]# iptables -F
2) パケットフィルタリングルールを追加
ループバックアドレスからのアクセスを許可
[root@linux ~]# iptables -A INPUT -i lo -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -o lo -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -o lo -j ACCEPT
不特定ホストから自ホストへのPINGを許可
[root@linux ~]# iptables -A INPUT -p icmp --icmp-type echo-request -d $localhostIP -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p icmp --icmp-type echo-reply -s $localhostIP -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p icmp --icmp-type echo-reply -s $localhostIP -j ACCEPT
自ホストから不特定ホストへのPINGを許可
[root@linux ~]# iptables -A OUTPUT -p icmp --icmp-type echo-request -s $localhostIP -j ACCEPT
[root@linux ~]# iptables -A INPUT -p icmp --icmp-type echo-reply -d $localhostIP -j ACCEPT
[root@linux ~]# iptables -A INPUT -p icmp --icmp-type echo-reply -d $localhostIP -j ACCEPT
特定IPから自ホストへのSSHを許可
[root@linux ~]# iptables -A INPUT -p tcp -s $TrustIP -d $localhostIP --dport 22 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 22 -d $TrustIP -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 22 -d $TrustIP -j ACCEPT
任意のネットワークからのHTTPへのアクセスを許可
[root@linux ~]# iptables -A INPUT -p tcp -s 0.0.0.0/0 -d $localhostIP --dport 80 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 80 -d 0.0.0.0/0 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 80 -d 0.0.0.0/0 -j ACCEPT
任意のネットワークからのFTP接続を許可
[root@linux ~]# iptables -A INPUT -p tcp -d $localhostIP --dport 21 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 21 -j ACCEPT
[root@linux ~]# iptables -A INPUT -p tcp -d $localhostIP --dport 20 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 20 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 21 -j ACCEPT
[root@linux ~]# iptables -A INPUT -p tcp -d $localhostIP --dport 20 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 20 -j ACCEPT
任意のネットワークからのSMTP接続を許可
[root@linux ~]# iptables -A INPUT -p tcp -d $localhostIP --dport 25 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 25 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP --sport 25 -j ACCEPT
任意のネットワークからのメール受信(POP)を許可
[root@linux ~]# iptables -A INPUT -p tcp -s $TrustIP -d $localhostIP --dport 110 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP -d $TrustIP --sport 110 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp -s $localhostIP -d $TrustIP --sport 110 -j ACCEPT
DNSの利用を許可
[root@linux ~]# iptables -A INPUT -p udp --sport 53 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
[root@linux ~]# iptables -A INPUT -p tcp --sport 53 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
[root@linux ~]# iptables -A INPUT -p tcp --sport 53 -j ACCEPT
[root@linux ~]# iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
ログ取得設定
[root@linux ~]# iptables -N LOGGING
[root@linux ~]# iptables -A LOGGING -j LOG --log-level warning --log-prefix "DROP:" -m limit
[root@linux ~]# iptables -A LOGGING -j DROP
[root@linux ~]# iptables -A INPUT -j LOGGING
[root@linux ~]# iptables -A LOGGING -j LOG --log-level warning --log-prefix "DROP:" -m limit
[root@linux ~]# iptables -A LOGGING -j DROP
[root@linux ~]# iptables -A INPUT -j LOGGING
3) ポリシーを設定 -> その他全ての接続を拒否
[root@linux ~]# iptables -P INPUT DROP
[root@linux ~]# iptables -P OUTPUT DROP
[root@linux ~]# iptables -P FORWARD DROP
[root@linux ~]# iptables -P OUTPUT DROP
[root@linux ~]# iptables -P FORWARD DROP
4) iptablesの保存と再起動
[root@linux ~]# /etc/init.d/iptables save
Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
[root@linux ~]# /etc/init.d/iptables restart
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: mangle filter nat [ OK ]
Unloading iptables modules: [ OK ]
Applying iptables firewall rules: [ OK ]
Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
[root@linux ~]# /etc/init.d/iptables restart
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: mangle filter nat [ OK ]
Unloading iptables modules: [ OK ]
Applying iptables firewall rules: [ OK ]
設定内容確認
