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?

Linuxファイアウォールとiptablesの完全理解

Last updated at Posted at 2025-05-19

1. Netfilterの概要

図解: Netfilter Packet Flow

image.png

Linuxのファイアウォール機能はNetfilterコンポーネントによって提供されます。Netfilterはカーネル空間で動作し、Linuxカーネルに統合されています。

1.1 Netfilterの特徴

  • Linux 2.4.x以降で採用された新しいファイアウォール機構
  • モジュール設計による拡張性の高さ
  • ネットワークサービス拡張のための構造化フレームワーク
  • IPプロトコルスタックとシームレスに統合
  • パケットフィルタリング、アドレス変換、処理機能を提供

公式ドキュメント: Netfilter Documentation

1.2 主要ディストリビューションでの設定確認

# CentOS 8での確認例
$ grep -m 10 NETFILTER /boot/config-4.18.0-193.el8.x86_64
CONFIG_NETFILTER=y
CONFIG_NETFILTER_ADVANCED=y
CONFIG_BRIDGE_NETFILTER=m
CONFIG_NETFILTER_INGRESS=y

# Ubuntu 20.04での確認例
$ grep -m 10 NETFILTER /boot/config-5.4.0-33-generic
CONFIG_NETFILTER=y
CONFIG_NETFILTER_ADVANCED=y
CONFIG_BRIDGE_NETFILTER=m

2. 主要ファイアウォールツール比較

2.1 iptables

  • ユーザー空間で動作するCLIツール
  • ルール作成とNetfilterへの指示を担当
  • 歴史的な経緯から複雑な実装になっている
[root@centos8 ~]#rpm -qi iptables
 Name        : iptables
 Version     : 1.8.2
 Release     : 9.el8
 Architecture: x86_64
 Install Date: Wed 25 Sep 2019 09:29:06 PM CST
 Group       : Unspecified
 Size        : 2050086
 License     : GPLv2 and Artistic 2.0 and ISC
 Signature   : RSA/SHA256, Tue 02 Jul 2019 06:50:00 AM CST, Key ID 
05b555b38483c65d
 Source RPM  : iptables-1.8.2-9.el8.src.rpm
 Build Date  : Sat 11 May 2019 10:21:57 PM CST
 Build Host  : x86-01.mbox.centos.org
 Relocations : (not relocatable)
 Packager    : CentOS Buildsys <bugs@centos.org>
 Vendor      : CentOS
 URL         : http://www.netfilter.org/
 Summary     : Tools for managing Linux kernel packet filtering capabilities
 Description :
 The iptables utility controls the network packet filtering code in the
 Linux kernel. If you need to set up firewalls and/or IP masquerading,
 you should either install nftables or this package.
 Note: This package contains the nftables-based variants of iptables and
 ip6tables, which are drop-in replacements of the legacy tools.
 [root@centos8 ~]# iptables --version
 iptables v1.8.2 (nf_tables)
 [root@centos8 ~]#ll /usr/sbin/iptables
 lrwxrwxrwx. 1 root root 17 May 11  2019 /usr/sbin/iptables -> xtables-nft-multi
 [root@centos7 ~]#ll /usr/sbin/iptables
 lrwxrwxrwx. 1 root root 13 Dec  9  2018 /usr/sbin/iptables -> xtables-multi
 [root@centos7 ~]# iptables --version
 iptables v1.4.21
 [root@centos6 ~]#iptables --version
 iptables v1.4.7
 [root@centos6 ~]#ll /sbin/iptables
 lrwxrwxrwx. 1 root root 33 Dec 12  2018 /sbin/iptables -> 
/etc/alternatives/iptables.x86_64
 [root@centos6 ~]#ll /etc/alternatives/iptables.x86_64
 lrwxrwxrwx. 1 root root 20 Dec 12  2018 /etc/alternatives/iptables.x86_64 -> 
/sbin/iptables-1.4.7
 [root@centos6 ~]#ll /sbin/iptables
 lrwxrwxrwx. 1 root root 33 Dec 12  2018 /sbin/iptables -> 
/etc/alternatives/iptables.x86_64


[root@centos8 ~]#dnf -y install iptables-services
 [root@centos8 ~]#rpm -ql iptables-services
 /etc/sysconfig/ip6tables
 /etc/sysconfig/iptables
 /usr/lib/systemd/system/ip6tables.service
 /usr/lib/systemd/system/iptables.service
 /usr/libexec/initscripts/legacy-actions/ip6tables
 /usr/libexec/initscripts/legacy-actions/ip6tables/panic
 /usr/libexec/initscripts/legacy-actions/ip6tables/save
 /usr/libexec/initscripts/legacy-actions/iptables
 /usr/libexec/initscripts/legacy-actions/iptables/panic
 /usr/libexec/initscripts/legacy-actions/iptables/save
 /usr/libexec/iptables
 /usr/libexec/iptables/ip6tables.init
 /usr/libexec/iptables/iptables.init

2.2 firewalld

  • CentOS 7以降で導入された新しいフロントエンド
  • ダイナミックなルール管理が可能
  • zone概念を導入した柔軟な構成

主要管理ツール:

  • firewall-cmd(CLI)
  • firewall-config(GUI)

2.3 nftables

  • CentOS 8で正式採用された次世代フレームワーク
  • iptables/arptables/ebtablesを統合
  • 効率的なルール処理と簡潔な構文
[root@centos8 ~]#rpm -qi nftables
 Name        : nftables
 Epoch       : 1
 Version     : 0.9.0
 Release     : 8.el8
 Architecture: x86_64
 Install Date: Wed 25 Sep 2019 09:29:06 PM CST
 Group       : Unspecified
 Size        : 758622
 License     : GPLv2
Signature   : RSA/SHA256, Tue 02 Jul 2019 08:19:09 AM CST, Key ID 
05b555b38483c65d
 Source RPM  : nftables-0.9.0-8.el8.src.rpm
 Build Date  : Sat 11 May 2019 11:06:46 PM CST
 Build Host  : x86-01.mbox.centos.org
 Relocations : (not relocatable)
 Packager    : CentOS Buildsys <bugs@centos.org>
 Vendor      : CentOS
 URL         : http://netfilter.org/projects/nftables/
 Summary     : Netfilter Tables userspace utillites
 Description :
 Netfilter Tables userspace utilities.

 [root@centos8 ~]#systemctl status iptables.service 
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor 
preset: disabled)
   Active: inactive (dead)
 [root@centos8 ~]#systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor 
preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
 [root@centos8 ~]#systemctl status nftables.service 
● nftables.service - Netfilter Tables
   Loaded: loaded (/usr/lib/systemd/system/nftables.service; disabled; vendor 
preset: disabled)
   Active: inactive (dead)
     Docs: man:nft(8)

3. パケット処理フロー

3.1 5つのフックポイント

  1. PREROUTING: ルーティング前処理
  2. INPUT: ローカルプロセスへの入力
  3. FORWARD: 転送処理
  4. OUTPUT: ローカルプロセスからの出力
  5. POSTROUTING: ルーティング後処理

3.2 パケットの流れ方向

[受信パケット]
        ↓
PREROUTINGチェーン
        ↓
[ルーティング判定] → 転送処理 → FORWARDチェーン → POSTROUTING
        ↓
INPUTチェーン → アプリケーション処理 → OUTPUTチェーン → POSTROUTING

4. iptablesの基本構成

4.1 チェーン(Chain)の種類

  • 組み込みチェーン: INPUT, OUTPUT, FORWARD, PREROUTING, POSTROUTING
  • カスタムチェーン: ユーザー定義チェーン

4.2 テーブル(Table)の種類と優先順位

優先度 テーブル 主な機能
最高 security SELinuxなど強制アクセス制御
raw コネクション追跡の無効化
mangle パケットヘッダー変更
nat ネットワークアドレス変換(NAT)
最低 filter デフォルトのフィルタリング

5. 各テーブルの詳細機能

5.1 filterテーブル

  • パケットの許可/拒否を決定
  • 含まれるチェーン: INPUT, FORWARD, OUTPUT

5.2 natテーブル

  • ネットワークアドレス変換(NAT)を実施
  • 含まれるチェーン: PREROUTING, POSTROUTING, OUTPUT

5.3 mangleテーブル

  • パケットヘッダーの変更(TTL値変更など)
  • 全チェーンを保持

5.4 rawテーブル

  • コネクション追跡機構をスキップ
  • 含まれるチェーン: PREROUTING, OUTPUT

5.5 securityテーブル

  • SELinuxなどのMACを実装
  • 含まれるチェーン: INPUT, FORWARD, OUTPUT

6. パケット処理の詳細フロー

[受信パケット]
        ↓
PREROUTING(raw→mangle→nat)
        ↓
[ルーティング] → FORWARD → POSTROUTING(mangle→nat)
        ↓
INPUT(mangle→filter→security) → アプリ処理 → OUTPUT(raw→mangle→nat→filter→security)
        ↓
POSTROUTING(mangle→nat)
        ↓
[送信パケット]

7. コマンド実践例

7.1 テーブル確認コマンド

# filterテーブル
$ iptables -vnL -t filter

# natテーブル
$ iptables -vnL -t nat

# mangleテーブル
$ iptables -vnL -t mangle

7.2 ルール保存と永続化

# ルール保存
$ iptables-save > /etc/sysconfig/iptables

# サービス再起動
$ systemctl restart iptables

8. 注意事項

  • CentOS6ではnatテーブルのINPUTチェーン未対応
  • チェーン処理順序はテーブル優先度に依存
  • カスタムチェーンは明示的に呼び出しが必要

9. 実践設定例

9.1 iptables-servicesのインストール

$ dnf -y install iptables-services
$ rpm -ql iptables-services
/etc/sysconfig/iptables
/usr/lib/systemd/system/iptables.service

9.2 基本的なフィルタリング設定

# SSHアクセス許可
$ iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# デフォルトポリシー設定
$ iptables -P INPUT DROP
$ iptables -P FORWARD DROP
$ iptables -P OUTPUT ACCEPT

9.3 NAT設定例

# マスカレード設定
$ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# ポートフォワーディング
$ iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

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

# 現在の接続状況確認
$ conntrack -L

# パケットトレース
$ iptables -t raw -A PREROUTING -p tcp --dport 80 -j TRACE
$ dmesg | grep TRACE

# ルール適用テスト
$ iptables-apply -t 60 /etc/sysconfig/iptables
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?