コンテンツにスキップ

firewalldとiptablesの違いと使い分け

Linux環境における2つの主要なファイアウォール管理ツールの特徴、違い、使い分けについて詳しく解説します。

実現できること

  • :fontawesome-solid-balance-scale:{ .lg .middle } 適切な選択


    環境と要件に応じた最適なツール選択

  • 移行戦略


    既存環境からの安全な移行方法

  • パフォーマンス比較


    用途別の性能特性理解

  • :fontawesome-solid-tools:{ .lg .middle } 統合管理


    複数環境での一貫した管理手法

アーキテクチャの違い

システム構成比較

graph TB
    subgraph "firewalld アーキテクチャ"
        A1[firewalld デーモン]
        A2[firewall-cmd CLI]
        A3[GUI ツール]
        A4[D-Bus インターフェース]
        A5[XML設定ファイル]
        A6[netfilter/iptables]

        A2 --> A4
        A3 --> A4
        A4 --> A1
        A1 --> A5
        A1 --> A6
    end

    subgraph "iptables アーキテクチャ"
        B1[iptables コマンド]
        B2[設定ファイル]
        B3[netfilter/iptables]

        B1 --> B3
        B2 --> B3
    end

    style A1 fill:#4ecdc4
    style A6 fill:#ffeaa7
    style B1 fill:#ff6b6b
    style B3 fill:#ffeaa7

管理レイヤーの違い

項目firewalldiptables
管理レベル高レベル(抽象化)低レベル(直接制御)
設定方法サービス・ゾーンベースルールベース
設定反映動的(再起動不要)静的(再読み込み必要)
バックエンドiptables/nftablesカーネル直接
GUI利用可能コマンドラインのみ

基本概念の比較

firewalldのゾーンベース管理

graph LR
    subgraph "firewalld ゾーン概念"
        A[ネットワーク<br/>インターフェース] --> B[ゾーン割り当て]
        B --> C{trusted}
        B --> D{public}
        B --> E{internal}
        B --> F{dmz}

        C --> G[全サービス許可]
        D --> H[基本サービスのみ]
        E --> I[内部ネットワーク向け]
        F --> J[DMZ向け制限]
    end

    style C fill:#4ecdc4
    style D fill:#ffeaa7
    style E fill:#a8e6cf
    style F fill:#ffaaa5

iptablesのチェーンベース管理

graph TD
    subgraph "iptables チェーン概念"
        A[パケット受信] --> B{処理方向}
        B --> C[INPUT<br/>ローカル宛]
        B --> D[FORWARD<br/>転送]
        B --> E[OUTPUT<br/>送信]

        C --> F[filter テーブル]
        D --> F
        E --> F

        G[nat テーブル] --> H[PREROUTING]
        G --> I[POSTROUTING]

        F --> J[ACCEPT/DROP/REJECT]
    end

    style F fill:#4ecdc4
    style G fill:#ffeaa7

設定方法の比較

基本的なWebサーバー設定

firewalldでの設定

# サービスベースの設定
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --add-service=ssh --permanent

# ゾーン変更
sudo firewall-cmd --set-default-zone=public

# 設定反映
sudo firewall-cmd --reload

# 確認
sudo firewall-cmd --list-all

iptablesでの設定

# ルールベースの設定
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

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

# 設定保存
sudo iptables-save > /etc/iptables/rules.v4

高度な設定例の比較

IP制限設定

firewalld:

# Rich Ruleを使用
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept' --permanent
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" reject' --permanent

iptables:

# 直接ルール指定
sudo iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -s 10.0.0.0/8 -j REJECT

運用面での比較

設定変更の影響範囲

sequenceDiagram
    participant A as 管理者
    participant F as firewalld
    participant I as iptables
    participant K as カーネル

    Note over A,K: firewalld での変更
    A->>F: firewall-cmd --add-service=http
    F->>K: ルール追加(即座に反映)
    Note over F,K: 既存接続は維持される

    Note over A,K: iptables での変更
    A->>I: iptables -A INPUT
    I->>K: ルール追加(即座に反映)
    Note over I,K: 設定によっては接続断もある

管理コストの比較

項目firewalldiptables
学習コスト中(概念理解が重要)高(詳細知識必要)
設定複雑度低(サービス定義活用)高(ルール詳細指定)
保守性高(構造化された設定)中(ルール数に依存)
デバッグ中(ログ出力は詳細)高(ルール追跡が容易)
自動化高(API/D-Bus活用)中(スクリプト化)

パフォーマンス比較

処理性能の違い

graph LR
    subgraph "firewalld パフォーマンス"
        A1[管理要求] --> A2[firewalld daemon]
        A2 --> A3[iptables実行]
        A3 --> A4[カーネル処理]
        A4 --> A5[パケット処理]
    end

    subgraph "iptables パフォーマンス"  
        B1[iptables実行] --> B2[カーネル処理]
        B2 --> B3[パケット処理]
    end

    style A2 fill:#ffeaa7
    style A4 fill:#4ecdc4
    style B2 fill:#4ecdc4

パフォーマンス特性

項目firewalldiptables
起動時間やや遅い(daemon起動)早い(直接実行)
設定反映早い(差分更新)中(全体再読み込み)
実行時性能同等(同じbackend)同等(カーネル処理)
メモリ使用量多い(daemon常駐)少ない(ルールのみ)
大量ルール良好(構造化管理)性能低下可能性

使い分けガイドライン

firewalldが適している場面

mindmap
    root((firewalld適用場面))
        デスクトップ環境
            GUI管理が必要
            頻繁な設定変更
            ネットワーク切り替え
        サーバー管理
            複数環境管理
            自動化・API連携
            チーム運用
        学習・検証
            概念学習重視
            試行錯誤が多い
            安全な設定変更

推奨ケース: - CentOS/RHEL 7以降の標準環境 - GUI管理ツールを活用したい場合 - ネットワーク環境が動的に変わる環境 - チーム開発での一貫した管理が必要 - 自動化・オーケストレーション環境

iptablesが適している場面

mindmap
    root((iptables適用場面))
        高性能要求
            最小オーバーヘッド
            大量トラフィック
            リアルタイム制御
        細かい制御
            特殊プロトコル
            複雑なNAT設定
            パフォーマンス調整
        レガシー環境
            古いディストリビューション
            既存スクリプト資産
            移行コスト削減

推奨ケース: - 高トラフィック環境での最適性能が必要 - 細かいパケット制御が必要な用途 - 既存のiptables資産を活用したい場合 - 古いLinuxディストリビューション - 最小限のシステムリソース使用が必要

移行戦略

iptables → firewalld 移行

# 1. 現在の設定を確認・保存
sudo iptables-save > /backup/iptables-backup.rules

# 2. firewalldインストール・有効化
sudo yum install firewalld  # CentOS/RHEL
sudo systemctl enable firewalld

# 3. iptablesを無効化
sudo systemctl disable iptables
sudo systemctl stop iptables

# 4. 基本設定移行
# iptables: -A INPUT -p tcp --dport 80 -j ACCEPT
# ↓
sudo firewall-cmd --add-service=http --permanent

# 5. 設定反映・確認
sudo systemctl start firewalld
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

firewalld → iptables 移行

# 1. firewalld設定をiptablesで確認
sudo iptables -L -n

# 2. iptables設定を手動で構築
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -P INPUT DROP

# 3. 設定保存
sudo iptables-save > /etc/iptables/rules.v4

# 4. firewalld停止・iptables有効化
sudo systemctl stop firewalld
sudo systemctl disable firewalld
sudo systemctl enable iptables

トラブルシューティング

共通的な問題と対処

1. 設定が反映されない

firewalld:

# 設定確認
sudo firewall-cmd --list-all
sudo firewall-cmd --list-all --permanent

# permanent設定を忘れていないか確認
sudo firewall-cmd --reload

iptables:

# ルール確認
sudo iptables -L -n --line-numbers

# 保存を忘れていないか確認
sudo iptables-save

2. 両方が同時に動作している

# 状態確認
sudo systemctl status firewalld
sudo systemctl status iptables

# 片方を停止
sudo systemctl stop firewalld
sudo systemctl disable firewalld

3. 設定バックアップ・復元

firewalld:

# バックアップ
sudo tar -czf firewalld-backup.tar.gz /etc/firewalld/

# 復元
sudo tar -xzf firewalld-backup.tar.gz -C /
sudo firewall-cmd --reload

iptables:

# バックアップ
sudo iptables-save > iptables-backup.rules

# 復元
sudo iptables-restore < iptables-backup.rules

実環境での選択基準

環境別推奨設定

環境タイプ推奨ツール理由
エンタープライズサーバーfirewalld管理性・自動化対応
高性能Webサーバーiptables最小オーバーヘッド
開発・検証環境firewalld柔軟な設定変更
組み込み・IoTiptablesリソース効率性
コンテナ環境iptablesシンプル・軽量
デスクトップfirewalldGUI管理・動的環境

決定フローチャート

graph TD
    A[ファイアウォール選択] --> B{GUI管理必要?}
    B -->|Yes| C[firewalld]
    B -->|No| D{既存iptables資産?}
    D -->|Yes| E[iptables継続]
    D -->|No| F{高性能要求?}
    F -->|Yes| G[iptables]
    F -->|No| H{自動化・API必要?}
    H -->|Yes| I[firewalld]
    H -->|No| J{チーム管理?}
    J -->|Yes| K[firewalld]
    J -->|No| L[どちらでも可]

    style C fill:#4ecdc4
    style E fill:#ff6b6b
    style G fill:#ff6b6b
    style I fill:#4ecdc4
    style K fill:#4ecdc4
    style L fill:#ffeaa7

まとめ

firewalldとiptablesは、それぞれ異なる管理哲学とアプローチを持つツールです。

選択のポイント: - ✅ 管理性重視 → firewalld - ✅ 性能・効率重視 → iptables
- ✅ 既存資産活用 → 現在使用中のツール継続 - ✅ 将来性考慮 → 環境に応じた適切な選択

どちらも強力なツールであり、要件と環境に応じた適切な選択が重要です。

関連記事