0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

iptables徹底解説:基本概念からカスタムチェーン活用まで

Last updated at Posted at 2025-05-20

1. iptablesの基本概念

1.1 ルールの構成要素

ルール(rule):パケットの条件に合致した場合、定義された処理アクションを実行する判定ロジック
※ チェーン内での評価順序はルールの追加順序で決定

主な構成要素

  • マッチ条件(AND条件で複数指定可能)
    • 基本条件:IPアドレス/ポート番号/TCPフラグ(SYN/ACK等)
    • 拡張条件:高度なマッチング機能
  • 処理アクション(ターゲット)
    • ビルトインアクション:ACCEPT/DROP/REJECT/SNAT/DNATなど
    • カスタムチェーン:複雑な条件をグループ化して管理

チェーン戦略

  • ホワイトリスト方式:明示的に許可したもののみ通過
  • ブラックリスト方式:明示的に拒否したもの以外を通過(デフォルト)

1.2 ルール作成時の考慮ポイント

  1. 実装する機能 → テーブル選択(filter/natなど)
  2. パケットの経路 → チェーン選択(INPUT/OUTPUTなど)
  3. 通信方向 → 送信元/送信先の特定
  4. ビジネス要件 → マッチ条件の精緻化

1.3 事前環境設定

# CentOS 7/8の場合
sudo systemctl disable --now firewalld.service

# CentOS 6の場合
sudo service iptables stop
sudo chkconfig iptables off

2. iptablesコマンド詳細解説

2.1 基本構文

iptables [-t テーブル] サブコマンド チェーン [ルール番号] マッチ条件 -j ターゲット

image.png

主なサブコマンド

オプション 説明
-A チェーン末尾にルール追加
-I 指定位置にルール挿入
-D ルール削除
-L ルール一覧表示
-F チェーン内全ルール削除
-N カスタムチェーン作成
-X 空のカスタムチェーン削除
-P デフォルトポリシー設定

マニュアル

 iptables [-t table] {-A|-C|-D} chain rule-specification
 iptables [-t table] -I chain [rulenum] rule-specification
 iptables [-t table] -R chain rulenum rule-specification
 iptables [-t table] -D chain rulenum
 iptables [-t table] -S [chain [rulenum]]
 iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
 iptables [-t table] -N chain
 iptables [-t table] -X  [chain]
 iptables [-t table] -P chain target
 iptables [-t table] -E old-chain-name new-chain-name
 rule-specification = [matches...]  [target]
 match = -m matchname [per-match-options]
 target = -j targetname [per-target-options]

2.2 カスタムチェーン活用例

チェーン作成と関連付け

# WEB用カスタムチェーン作成
sudo iptables -N WEB_CHAIN

# 80/443ポート許可ルール追加
sudo iptables -A WEB_CHAIN -p tcp --dport 80 -j ACCEPT
sudo iptables -A WEB_CHAIN -p tcp --dport 443 -j ACCEPT

# INPUTチェーンからカスタムチェーン呼び出し
sudo iptables -A INPUT -s 10.0.0.0/24 -j WEB_CHAIN

ルールの詳細確認

sudo iptables -vnL --line-numbers

出力例

Chain WEB_CHAIN (1 references)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
2        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:443

カスタムチェーン削除手順

# 関連ルールを先に削除
sudo iptables -D INPUT 1

# チェーン内ルールを全削除
sudo iptables -F WEB_CHAIN

# カスタムチェーン削除
sudo iptables -X WEB_CHAIN

3. 実践的な活用テクニック

3.1 複数ポートの一括指定

sudo iptables -A WEB_CHAIN -p tcp -m multiport --dports 80,443,8080 -j ACCEPT

3.2 特定ホストのアクセス制限

# 10.0.0.6からの接続を拒否
sudo iptables -I WEB_CHAIN 2 -s 10.0.0.6 -j DROP

# ICMPプロトコルのみ許可
sudo iptables -A WEB_CHAIN -p icmp -j ACCEPT

3.3 チェーン評価の早期終了

# 特定条件でメインチェーンに戻る
sudo iptables -I WEB_CHAIN 3 -s 10.0.0.100 -j RETURN

4. トラブルシューティングTips

  • ルールの優先順位:上から順に評価されるため、詳細な条件を先に記述
  • ログ出力活用:問題のあるパケットをログに記録して分析
    sudo iptables -A INPUT -j LOG --log-prefix "[IPTABLES DEBUG] "
    
  • ポリシー確認:デフォルトポリシーが意図しない動作を引き起こしていないか確認
    sudo iptables -S | grep POLICY
    

4.1 カスタマイズ

[root@centos8 ~]#iptables -N web_chain 
[root@centos8 ~]#iptables -N web_chain -t nat
[root@centos8 ~]#iptables -X web_chain -t nat
[root@centos8 ~]#iptables -E web_chain WEB_CHAIN
[root@centos8 ~]#iptables -A WEB_CHAIN -s 10.0.0.6 -p tcp -m multiport  --dports 
80,443 -j REJECT
[root@centos8 ~]#iptables -R WEB_CHAIN 1  -s 10.0.0.6 -p tcp -m multiport  -
dports 80,443,8080 -j REJECT
[root@centos8 ~]#iptables -vnL  WEB_CHAIN
 Chain WEB_CHAIN (1 references)
 pkts bytes target     prot opt in
     out     
1    
60 REJECT     tcp  --  *      *       
source
 10.0.0.6             
               destination 
0.0.0.0/0   
         multiport dports 80,443,8080 reject-with icmp-port-unreachable
 [root@centos8 ~]#iptables -AINPUT -j WEB_CHAIN 
 [root@centos8 ~]#iptables -X WEB_CHAIN
 iptables v1.8.2 (nf_tables):  CHAIN_USER_DEL failed (Device or resource busy): 
chain WEB_CHAIN
 [root@centos8 ~]#iptables -F  WEB_CHAIN
 [root@centos8 ~]#iptables -X WEB_CHAIN
iptables v1.8.2 (nf_tables):  CHAIN_USER_DEL failed (Device or resource busy): 
chain WEB_CHAIN
 [root@centos8 ~]#iptables -D INPUT 1
 [root@centos8 ~]#iptables -X WEB_CHAIN

5. まとめ

iptablesの効果的な運用には、以下のポイントが重要です:

  1. ネットワーク要件の明確化
  2. 適切なチェーン設計
  3. カスタムチェーンを活用したルールのグループ化
  4. 定期的なルールの見直しと最適化
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?