คู่มือการใช้งาน Firewalld สำหรับ Linux

Introduction

ความสำคัญของ Network Security

ในยุคดิจิทัลปัจจุบัน การรักษาความปลอดภัยของเครือข่ายเป็นสิ่งที่มีความสำคัญอย่างยิ่ง การโจมตีทางไซเบอร์มีความซับซ้อนและเพิ่มมากขึ้นทุกวัน การป้องกันระบบเครือข่ายและเซิร์ฟเวอร์จากการเข้าถึงที่ไม่ได้รับอนุญาตเป็นหน้าที่สำคัญของทุกองค์กรและผู้ดูแลระบบ

Firewall เป็นหนึ่งในเครื่องมือพื้นฐานที่สำคัญที่สุดในการป้องกันความปลอดภัยของระบบ โดยทำหน้าที่เป็นกำแพงป้องกันระหว่างเครือข่ายภายใน (trusted network) กับเครือข่ายภายนอก (untrusted network เช่น Internet)

บทบาทของ Firewall ในระบบ Linux

ระบบปฏิบัติการ Linux มีความสามารถในการจัดการ firewall ที่แข็งแกร่งผ่าน kernel-level framework ชื่อ Netfilter/iptables ซึ่งเป็นพื้นฐานของระบบ packet filtering บน Linux แต่การใช้งาน iptables โดยตรงอาจมีความซับซ้อนสำหรับผู้ใช้ทั่วไป

Firewalld จึงถูกพัฒนาขึ้นเพื่อเป็นเครื่องมือจัดการ firewall ที่ใช้งานง่ายขึ้น มีความยืดหยุ่นสูง และรองรับการปรับเปลี่ยนแบบ dynamic โดยไม่ต้อง restart service หรือตัดการเชื่อมต่อที่มีอยู่


ความเข้าใจพื้นฐานเกี่ยวกับ Firewall

Firewall คืออะไร

Firewall เป็นระบบรักษาความปลอดภัยเครือข่ายที่ทำหน้าที่ตรวจสอบและควบคุมการรับส่งข้อมูลเครือข่าย (network traffic) ตามกฎเกณฑ์ด้านความปลอดภัยที่กำหนดไว้ โดยทำงานเป็น barrier ระหว่างเครือข่ายที่เชื่อถือได้กับเครือข่ายที่ไม่เชื่อถือได้

ประเภทของ Firewall

1. Packet Filtering Firewall

2. Stateful Inspection Firewall

3. Application Layer Firewall

4. Next-Generation Firewall (NGFW)


Firewalld คืออะไร

ภาพรวม

Firewalld เป็น firewall management tool สำหรับ Linux ที่ให้การจัดการ firewall แบบ dynamic โดยไม่ต้อง restart service หรือขัดจังหวะการเชื่อมต่อที่มีอยู่ Firewalld ใช้แนวคิดของ "zones" และ "services" เพื่อทำให้การจัดการกฎง่ายขึ้น

คุณสมบัติหลัก

สถาปัตยกรรม

graph TB
    A[firewall-cmd / GUI] --> B[firewalld Service]
    B --> C[D-Bus Interface]
    C --> D[Netfilter/nftables]
    D --> E[Kernel]
    
    F[Applications] --> C
    
    B --> G[Zone Configuration]
    B --> H[Service Configuration]
    B --> I[Rich Rules]
    
    G --> D
    H --> D
    I --> D
    
    style B fill:#f9f,stroke:#333,stroke-width:4px
    style D fill:#bbf,stroke:#333,stroke-width:2px

สถาปัตยกรรมของ Firewalld

Zones (โซน)

Zones เป็นแนวคิดหลักของ Firewalld ที่กำหนดระดับความเชื่อถือของ network connections โดยแต่ละ zone มีกฎการกรอง traffic ที่แตกต่างกัน

Predefined Zones:

Zone Trust Level คำอธิบาย การใช้งานทั่วไป
drop ต่ำสุด ทุก incoming packets ถูก drop โดยไม่มี reply สถานการณ์ที่ต้องการความปลอดภัยสูงสุด
block ต่ำมาก Incoming connections ถูก reject พร้อม icmp-host-prohibited เครือข่ายที่ไม่เชื่อถือ
public ต่ำ เชื่อถือเฉพาะ services ที่เลือก เหมาะกับพื้นที่สาธารณะ เซิร์ฟเวอร์ที่เปิดให้ public access
external ปานกลาง-ต่ำ สำหรับ external networks ที่มี masquerading Gateway หรือ router
dmz ปานกลาง จำกัด access เฉพาะ services ที่จำเป็น Web servers ใน DMZ
work ปานกลาง-สูง เชื่อถือเพื่อนร่วมงานส่วนใหญ่ เครือข่ายในที่ทำงาน
home สูง เชื่อถือเครื่องส่วนใหญ่ในเครือข่าย เครือข่ายที่บ้าน
internal สูงมาก Internal network ที่เชื่อถือ เครือข่ายภายในองค์กร
trusted สูงสุด เชื่อถือทุก connections เครือข่ายที่ควบคุมได้ทั้งหมด

Services

Services คือ predefined ชุดของ ports และ protocols ที่ใช้โดยแอปพลิเคชันต่างๆ เช่น HTTP, SSH, FTP ทำให้การเปิด-ปิด services ง่ายขึ้นโดยไม่ต้องจำ port numbers

Runtime vs Permanent Configuration

sequenceDiagram
    participant User
    participant firewall-cmd
    participant Runtime Config
    participant Permanent Config
    participant Kernel
    
    User->>firewall-cmd: firewall-cmd --add-service=http
    firewall-cmd->>Runtime Config: Add HTTP service
    Runtime Config->>Kernel: Apply rules immediately
    
    Note over User,Kernel: Changes lost on restart
    
    User->>firewall-cmd: firewall-cmd --permanent --add-service=http
    firewall-cmd->>Permanent Config: Save HTTP service
    
    User->>firewall-cmd: firewall-cmd --reload
    firewall-cmd->>Permanent Config: Read configuration
    Permanent Config->>Runtime Config: Update runtime
    Runtime Config->>Kernel: Apply rules
    
    Note over User,Kernel: Changes persist after restart

การเปรียบเทียบ Firewall Tools

Firewalld vs iptables vs UFW vs nftables

คุณสมบัติ Firewalld iptables UFW nftables
ความง่ายในการใช้งาน ★★★★☆ ★★☆☆☆ ★★★★★ ★★★☆☆
Dynamic Management ✅ ใช่ ❌ ไม่ ✅ ใช่ ✅ ใช่
Zone-based Configuration ✅ ใช่ ❌ ไม่ ❌ ไม่ ⚠️ Manual
GUI Support ✅ firewall-config ⚠️ Third-party ⚠️ GUFW ❌ ไม่
Backend Technology nftables/iptables Netfilter iptables Netfilter
D-Bus Interface ✅ ใช่ ❌ ไม่ ❌ ไม่ ❌ ไม่
IPv4/IPv6 ✅ รองรับทั้งคู่ ✅ รองรับทั้งคู่ ✅ รองรับทั้งคู่ ✅ รองรับทั้งคู่
Rich Rules ✅ ใช่ ⚠️ Complex syntax ❌ จำกัด ✅ ใช่
Learning Curve ปานกลาง สูง ต่ำ ปานกลาง-สูง
Distributions RHEL, CentOS, Fedora ทุก Linux Ubuntu, Debian Modern Linux
Performance ดี ดีมาก ดี ดีที่สุด
Community Support ดี ดีมาก ดี กำลังเติบโต

เมื่อไรควรใช้อะไร

ใช้ Firewalld เมื่อ:

ใช้ iptables เมื่อ:

ใช้ UFW เมื่อ:

ใช้ nftables เมื่อ:


การติดตั้ง Firewalld

ตรวจสอบว่ามี Firewalld หรือไม่

# ตรวจสอบว่าติดตั้งแล้วหรือไม่
firewall-cmd --version

# ตรวจสอบสถานะ service
systemctl status firewalld

ติดตั้งบน RHEL/CentOS/Fedora

# ติดตั้ง firewalld
sudo dnf install firewalld -y

# หรือบน CentOS/RHEL 7
sudo yum install firewalld -y

ติดตั้งบน Debian/Ubuntu

# Update package list
sudo apt update

# ติดตั้ง firewalld
sudo apt install firewalld -y

เปิดใช้งาน Firewalld

# Start firewalld service
sudo systemctl start firewalld

# เปิดใช้งานตอน boot
sudo systemctl enable firewalld

# ตรวจสอบสถานะ
sudo systemctl status firewalld

# ตรวจสอบว่า firewalld ทำงานหรือไม่
sudo firewall-cmd --state

หมายเหตุสำคัญเกี่ยวกับการติดตั้ง

⚠️ คำเตือน: ก่อนเปิดใช้งาน firewalld บนเซิร์ฟเวอร์ production ควร:

  1. เปิด SSH port ก่อน (หากเชื่อมต่อผ่าน SSH)
  2. ทดสอบบน staging environment ก่อน
  3. มี console access หรือ out-of-band access
  4. Backup configuration ปัจจุบัน
# เปิด SSH port ก่อนเปิดใช้งาน firewalld
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

พื้นฐานการใช้งาน Firewalld

คำสั่งพื้นฐาน

ตรวจสอบสถานะและข้อมูลทั่วไป

# ตรวจสอบว่า firewalld ทำงานหรือไม่
sudo firewall-cmd --state

# แสดง default zone
sudo firewall-cmd --get-default-zone

# แสดง active zones
sudo firewall-cmd --get-active-zones

# แสดงทุก zones
sudo firewall-cmd --get-zones

# แสดงข้อมูลของ zone เฉพาะ
sudo firewall-cmd --zone=public --list-all

# แสดงข้อมูลทุก zones
sudo firewall-cmd --list-all-zones

การจัดการ Service

# แสดง services ทั้งหมดที่มี
sudo firewall-cmd --get-services

# แสดง services ที่เปิดใช้งานใน zone ปัจจุบัน
sudo firewall-cmd --list-services

# แสดง services ใน zone เฉพาะ
sudo firewall-cmd --zone=public --list-services

# เพิ่ม service (runtime)
sudo firewall-cmd --add-service=http

# เพิ่ม service (permanent)
sudo firewall-cmd --permanent --add-service=http

# เพิ่ม service และทำให้ permanent พร้อมกัน
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

# ลบ service
sudo firewall-cmd --remove-service=http
sudo firewall-cmd --permanent --remove-service=http

การ Reload และ Restart

# Reload configuration (โหลด permanent config ไปยัง runtime)
sudo firewall-cmd --reload

# Complete restart (ตัดการเชื่อมต่อชั่วคราว)
sudo systemctl restart firewalld

# Reload โดยไม่ตัดการเชื่อมต่อ
sudo firewall-cmd --reload

Runtime vs Permanent Configuration

graph LR
    A[User Command] --> B{--permanent flag?}
    B -->|Yes| C[Permanent Storage
/etc/firewalld/] B -->|No| D[Runtime Memory] C --> E[firewall-cmd --reload] E --> D D --> F[Active Rules
in Kernel] style C fill:#afa,stroke:#333 style D fill:#faa,stroke:#333 style F fill:#aaf,stroke:#333

กลยุทธ์ในการใช้งาน:

  1. ทดสอบก่อน (Runtime)

    # เพิ่ม service ชั่วคราว
    sudo firewall-cmd --add-service=http
    # ทดสอบว่าทำงานตามที่ต้องการ
    
  2. ทำให้ถาวร (Permanent)

    # ถ้าทำงานได้ดี ก็ทำให้ permanent
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --reload
    
  3. หรือทำทั้งสองพร้อมกัน

    # เพิ่มทั้ง runtime และ permanent
    sudo firewall-cmd --add-service=http
    sudo firewall-cmd --permanent --add-service=http
    

การจัดการ Zones

เข้าใจ Zones

Zones เป็นหัวใจหลักของ Firewalld ที่ช่วยจัดการ security policies ตาม network environment ที่แตกต่างกัน

การดู Zones

# แสดง zones ทั้งหมด
sudo firewall-cmd --get-zones

# แสดง default zone
sudo firewall-cmd --get-default-zone

# แสดง active zones พร้อม interfaces
sudo firewall-cmd --get-active-zones

# แสดงรายละเอียดของ zone
sudo firewall-cmd --zone=public --list-all

# แสดงรายละเอียดทุก zones
sudo firewall-cmd --list-all-zones

การเปลี่ยน Default Zone

# เปลี่ยน default zone
sudo firewall-cmd --set-default-zone=home

# ตรวจสอบ
sudo firewall-cmd --get-default-zone

การกำหนด Interface ให้กับ Zone

# แสดง interface ทั้งหมด
ip link show

# กำหนด interface ให้ zone (runtime)
sudo firewall-cmd --zone=public --change-interface=eth0

# กำหนด interface ให้ zone (permanent)
sudo firewall-cmd --permanent --zone=public --change-interface=eth0

# ตรวจสอบว่า interface อยู่ใน zone ไหน
sudo firewall-cmd --get-zone-of-interface=eth0

# ลบ interface ออกจาก zone
sudo firewall-cmd --zone=public --remove-interface=eth0

การสร้าง Custom Zone

# สร้าง zone ใหม่
sudo firewall-cmd --permanent --new-zone=database

# Reload เพื่อให้ zone ใหม่มีผล
sudo firewall-cmd --reload

# กำหนดค่า zone ใหม่
sudo firewall-cmd --permanent --zone=database --set-target=DROP
sudo firewall-cmd --permanent --zone=database --add-service=mysql
sudo firewall-cmd --permanent --zone=database --add-source=192.168.1.0/24

# Reload อีกครั้ง
sudo firewall-cmd --reload

# ตรวจสอบ zone ใหม่
sudo firewall-cmd --zone=database --list-all

การจัดการ Source-based Zones

แทนที่จะใช้ interface สามารถใช้ source IP address เพื่อกำหนด zone ได้

# เพิ่ม source IP/network ให้ zone
sudo firewall-cmd --permanent --zone=internal --add-source=192.168.1.0/24
sudo firewall-cmd --permanent --zone=internal --add-source=10.0.0.50/32

# แสดง sources ใน zone
sudo firewall-cmd --zone=internal --list-sources

# ลบ source
sudo firewall-cmd --permanent --zone=internal --remove-source=192.168.1.0/24

# Reload
sudo firewall-cmd --reload

ตัวอย่างการใช้งาน Zones

สถานการณ์: เซิร์ฟเวอร์ที่มีหลาย network interfaces

# eth0: Public Internet (ใช้ zone public)
sudo firewall-cmd --permanent --zone=public --change-interface=eth0
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https

# eth1: Internal Network (ใช้ zone internal)
sudo firewall-cmd --permanent --zone=internal --change-interface=eth1
sudo firewall-cmd --permanent --zone=internal --add-service=ssh
sudo firewall-cmd --permanent --zone=internal --add-service=nfs

# Reload
sudo firewall-cmd --reload

# ตรวจสอบ
sudo firewall-cmd --get-active-zones
graph TB
    Internet[Internet] --> eth0[eth0 - Public Zone]
    eth0 --> Server[Server]
    InternalNet[Internal Network] --> eth1[eth1 - Internal Zone]
    eth1 --> Server
    
    subgraph Public Zone Rules
    HTTP[HTTP:80]
    HTTPS[HTTPS:443]
    end
    
    subgraph Internal Zone Rules
    SSH[SSH:22]
    NFS[NFS:2049]
    end
    
    eth0 -.-> HTTP
    eth0 -.-> HTTPS
    eth1 -.-> SSH
    eth1 -.-> NFS
    
    style Server fill:#f9f,stroke:#333,stroke-width:4px
    style eth0 fill:#faa,stroke:#333
    style eth1 fill:#afa,stroke:#333

การจัดการ Services

Services คืออะไร

Services ใน Firewalld คือ predefined configuration ที่รวม ports และ protocols ที่จำเป็นสำหรับ application หรือ service ต่างๆ

การดู Services

# แสดง services ทั้งหมดที่มี
sudo firewall-cmd --get-services

# แสดง services ที่เปิดใช้งาน
sudo firewall-cmd --list-services

# แสดง services ใน zone เฉพาะ
sudo firewall-cmd --zone=public --list-services

# ดูรายละเอียดของ service
sudo firewall-cmd --info-service=http

การเพิ่มและลบ Services

# เพิ่ม service
sudo firewall-cmd --add-service=http

# เพิ่มหลาย services พร้อมกัน
sudo firewall-cmd --add-service=http --add-service=https

# เพิ่ม service แบบ permanent
sudo firewall-cmd --permanent --add-service=http

# เพิ่มให้ zone เฉพาะ
sudo firewall-cmd --zone=dmz --add-service=http

# ลบ service
sudo firewall-cmd --remove-service=http
sudo firewall-cmd --permanent --remove-service=http

# Reload
sudo firewall-cmd --reload

Services ที่ใช้บ่อย

Service Port Protocol คำอธิบาย
ssh 22 TCP Secure Shell
http 80 TCP Web Server
https 443 TCP Secure Web Server
ftp 21 TCP File Transfer Protocol
smtp 25 TCP Mail Server
dns 53 TCP/UDP Domain Name System
mysql 3306 TCP MySQL Database
postgresql 5432 TCP PostgreSQL Database
mongodb 27017 TCP MongoDB Database
redis 6379 TCP Redis Database
nfs 2049 TCP/UDP Network File System
samba 137-139, 445 TCP/UDP Windows File Sharing

การสร้าง Custom Service

บางครั้งเราต้องการสร้าง service เองสำหรับแอปพลิเคชันที่ไม่มี predefined service

ขั้นตอนที่ 1: สร้างไฟล์ service definition

# สร้างไฟล์ XML สำหรับ custom service
sudo nano /etc/firewalld/services/myapp.xml

เนื้อหาไฟล์ myapp.xml:

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MyApp</short>
  <description>My Custom Application Service</description>
  <port protocol="tcp" port="8080"/>
  <port protocol="tcp" port="8443"/>
</service>

ขั้นตอนที่ 2: Reload และใช้งาน service ใหม่

# Reload firewalld เพื่ออ่าน service ใหม่
sudo firewall-cmd --reload

# ตรวจสอบว่า service ปรากฏ
sudo firewall-cmd --get-services | grep myapp

# เพิ่ม custom service
sudo firewall-cmd --permanent --add-service=myapp
sudo firewall-cmd --reload

# ตรวจสอบ
sudo firewall-cmd --list-services

ตัวอย่าง: Setup Web Server

# เปิด HTTP และ HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# ถ้าใช้ alternative port (เช่น 8080)
sudo firewall-cmd --permanent --add-port=8080/tcp

# Reload
sudo firewall-cmd --reload

# ตรวจสอบ
sudo firewall-cmd --list-all

ตัวอย่าง: Setup Database Server

# สร้าง zone สำหรับ database
sudo firewall-cmd --permanent --new-zone=database
sudo firewall-cmd --reload

# กำหนดให้ accept เฉพาะจาก application servers
sudo firewall-cmd --permanent --zone=database --add-source=192.168.1.10/32
sudo firewall-cmd --permanent --zone=database --add-source=192.168.1.11/32

# เปิด MySQL service
sudo firewall-cmd --permanent --zone=database --add-service=mysql

# ตั้งค่า default target เป็น DROP
sudo firewall-cmd --permanent --zone=database --set-target=DROP

# Reload
sudo firewall-cmd --reload

# ตรวจสอบ
sudo firewall-cmd --zone=database --list-all

การจัดการ Ports

การเปิด-ปิด Ports

บางครั้งเราต้องการเปิด port เฉพาะโดยไม่ต้องสร้าง service

# เปิด single port
sudo firewall-cmd --add-port=8080/tcp

# เปิด port แบบ permanent
sudo firewall-cmd --permanent --add-port=8080/tcp

# เปิดหลาย ports พร้อมกัน
sudo firewall-cmd --permanent --add-port=8080/tcp --add-port=8443/tcp

# เปิด port range
sudo firewall-cmd --permanent --add-port=3000-3010/tcp

# เปิด UDP port
sudo firewall-cmd --permanent --add-port=53/udp

# ลบ port
sudo firewall-cmd --remove-port=8080/tcp
sudo firewall-cmd --permanent --remove-port=8080/tcp

# แสดง ports ที่เปิดอยู่
sudo firewall-cmd --list-ports

# Reload
sudo firewall-cmd --reload

Port Forwarding

Port forwarding ช่วยให้เราส่งต่อ traffic จาก port หนึ่งไปยังอีก port หรือ host หนึ่ง

Port Forwarding บน Local Machine

# Forward port 80 ไปยัง port 8080 บนเครื่องเดียวกัน
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080

# Forward พร้อมระบุ local address
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=127.0.0.1

# Reload
sudo firewall-cmd --reload

# แสดง forward ports
sudo firewall-cmd --list-forward-ports

Port Forwarding ไปยัง Remote Host

# เปิดใช้งาน masquerade ก่อน
sudo firewall-cmd --permanent --add-masquerade

# Forward port 80 ไปยัง 192.168.1.100:8080
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100

# Reload
sudo firewall-cmd --reload

# ตรวจสอบ
sudo firewall-cmd --list-all

Masquerading (NAT)

Masquerading เป็นรูปแบบหนึ่งของ NAT (Network Address Translation) ที่ใช้ในการแชร์ internet connection

# เปิดใช้งาน masquerading
sudo firewall-cmd --permanent --add-masquerade

# เปิดในเฉพาะ zone
sudo firewall-cmd --permanent --zone=external --add-masquerade

# ตรวจสอบว่าเปิดใช้งานหรือไม่
sudo firewall-cmd --query-masquerade

# ปิดการใช้งาน masquerading
sudo firewall-cmd --permanent --remove-masquerade

# Reload
sudo firewall-cmd --reload

ตัวอย่าง: Setup NAT Gateway

# สมมติว่า eth0 เชื่อมกับ Internet, eth1 เชื่อมกับ Internal Network

# กำหนด zones
sudo firewall-cmd --permanent --zone=external --change-interface=eth0
sudo firewall-cmd --permanent --zone=internal --change-interface=eth1

# เปิด masquerading บน external zone
sudo firewall-cmd --permanent --zone=external --add-masquerade

# เปิด SSH สำหรับ management
sudo firewall-cmd --permanent --zone=external --add-service=ssh

# Internal zone อนุญาตทุกอย่าง
sudo firewall-cmd --permanent --zone=internal --set-target=ACCEPT

# Reload
sudo firewall-cmd --reload

# ตรวจสอบ
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=external --list-all
sudo firewall-cmd --zone=internal --list-all
graph LR
    A[Internet] --> B[eth0 - External Zone
Masquerading ON] B --> C[Linux Gateway
NAT/Firewall] C --> D[eth1 - Internal Zone] D --> E[Internal Network
192.168.1.0/24] style C fill:#f9f,stroke:#333,stroke-width:4px style B fill:#faa,stroke:#333 style D fill:#afa,stroke:#333

Rich Rules

Rich Rules คืออะไร

Rich Rules เป็นรูปแบบการกำหนดกฎที่ซับซ้อนและละเอียดกว่าการใช้ services และ ports ทั่วไป สามารถระบุเงื่อนไขต่างๆ เช่น source IP, destination, logging, และ action ได้อย่างละเอียด

Syntax ของ Rich Rules

rule [family="<rule family>"]
    [ source [NOT] [address="<address>"] [mac="<mac address>"] [ipset="<ipset name>"] ]
    [ destination [NOT] address="<address>" ]
    [ <element> ]
    [ log [prefix="<prefix text>"] [level="<log level>"] [limit value="<rate>/<duration>"] ]
    [ audit ]
    [ accept|reject|drop ]

ตัวอย่าง Rich Rules

1. อนุญาต IP เฉพาะให้เข้า SSH

# อนุญาตเฉพาะ 192.168.1.100 เข้า SSH
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="ssh" accept'

# Reload
sudo firewall-cmd --reload

2. บล็อก IP เฉพาะ

# บล็อก IP 10.0.0.50
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.50" reject'

# บล็อก network subnet
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.100.0/24" drop'

# Reload
sudo firewall-cmd --reload

3. Rate Limiting

# จำกัด SSH connections (ป้องกัน brute force)
sudo firewall-cmd --permanent --add-rich-rule='rule service name="ssh" limit value="3/m" accept'

# จำกัด HTTP connections
sudo firewall-cmd --permanent --add-rich-rule='rule service name="http" limit value="100/s" accept'

# Reload
sudo firewall-cmd --reload

4. Logging

# Log SSH connections จาก specific network
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" log prefix="SSH-Access: " level="info" accept'

# Log และ drop packets จาก IP ที่ไม่ต้องการ
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" log prefix="Blocked-IP: " level="warning" drop'

# Reload
sudo firewall-cmd --reload

5. Port-based Rules

# อนุญาต specific port จาก specific IP
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.50" port port="3306" protocol="tcp" accept'

# อนุญาต port range
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" port port="8000-8100" protocol="tcp" accept'

# Reload
sudo firewall-cmd --reload

6. Time-based Rules (ต้องใช้ร่วมกับ cron)

Rich rules เองไม่รองรับ time-based rules โดยตรง แต่สามารถใช้ร่วมกับ cron scripts ได้

# สร้าง script สำหรับเปิด-ปิดกฎตามเวลา
sudo nano /usr/local/bin/firewall-schedule.sh
#!/bin/bash
# เปิด FTP เฉพาะ 8:00-18:00

HOUR=$(date +%H)

if [ $HOUR -ge 8 ] && [ $HOUR -lt 18 ]; then
    firewall-cmd --add-service=ftp
else
    firewall-cmd --remove-service=ftp
fi
# ทำให้ executable
sudo chmod +x /usr/local/bin/firewall-schedule.sh

# เพิ่มใน crontab
sudo crontab -e
# เพิ่มบรรทัด: */30 * * * * /usr/local/bin/firewall-schedule.sh

การจัดการ Rich Rules

# แสดง rich rules ทั้งหมด
sudo firewall-cmd --list-rich-rules

# แสดง rich rules ใน zone เฉพาะ
sudo firewall-cmd --zone=public --list-rich-rules

# ลบ rich rule
sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="ssh" accept'

# Reload
sudo firewall-cmd --reload

ตัวอย่างการใช้งานจริง

สถานการณ์ 1: Web Server ที่ป้องกัน DDoS

# อนุญาต HTTP/HTTPS แต่จำกัด rate
sudo firewall-cmd --permanent --add-rich-rule='rule service name="http" limit value="50/s" accept'
sudo firewall-cmd --permanent --add-rich-rule='rule service name="https" limit value="50/s" accept'

# อนุญาต SSH เฉพาะจาก admin network พร้อม rate limit
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" service name="ssh" limit value="3/m" log prefix="SSH-Admin: " level="info" accept'

# บล็อก known bad IPs
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.0/24" log prefix="Blocked-Attacker: " level="warning" drop'

# Reload
sudo firewall-cmd --reload

สถานการณ์ 2: Database Server ที่ปลอดภัย

# อนุญาต MySQL เฉพาะจาก app servers
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.10.10" service name="mysql" log prefix="DB-Access-App1: " accept'
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.10.11" service name="mysql" log prefix="DB-Access-App2: " accept'

# อนุญาต SSH เฉพาะจาก bastion host
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.10.1" service name="ssh" accept'

# Drop อื่นๆ ทั้งหมดและ log
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule log prefix="DB-Denied: " level="warning" drop'

# Reload
sudo firewall-cmd --reload
flowchart TD
    A[Incoming Packet] --> B{Match Rich Rule?}
    B -->|Yes| C{Check Source IP}
    C -->|Allowed| D{Check Rate Limit}
    D -->|Within Limit| E{Log?}
    E -->|Yes| F[Write to Log]
    E -->|No| G[Accept Packet]
    F --> G
    D -->|Exceeded| H[Drop Packet]
    C -->|Blocked| I{Log?}
    I -->|Yes| J[Write to Log]
    I -->|No| H
    J --> H
    B -->|No| K[Check Next Rule]
    
    style G fill:#afa,stroke:#333
    style H fill:#faa,stroke:#333

การจัดการ Direct Rules

Direct Rules คืออะไร

Direct Rules ช่วยให้เราสามารถใช้ iptables/nftables rules โดยตรงใน firewalld ได้ เหมาะสำหรับกรณีที่ต้องการควบคุมที่ละเอียดมากหรือใช้ features ที่ firewalld ยังไม่รองรับ

⚠️ คำเตือน: การใช้ direct rules ควรเป็นทางเลือกสุดท้าย เพราะอาจทำให้การจัดการซับซ้อนขึ้นและอาจขัดแย้งกับ firewalld rules อื่นๆ

Syntax พื้นฐาน

firewall-cmd --direct --add-rule ipv4|ipv6|eb <table> <chain> <priority> <args>

ตัวอย่างการใช้งาน

1. เพิ่ม Direct Rule พื้นฐาน

# อนุญาต traffic จาก specific IP
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -s 192.168.1.100 -j ACCEPT

# บล็อก traffic ไปยัง specific IP
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 0 -d 203.0.113.50 -j DROP

# Reload
sudo firewall-cmd --reload

2. การใช้ Connection Tracking

# อนุญาต established และ related connections
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -m state --state ESTABLISHED,RELATED -j ACCEPT

# Reload
sudo firewall-cmd --reload

3. Advanced Logging

# Log packets ที่ถูก drop พร้อม prefix
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 5 -j LOG --log-prefix "DROPPED: " --log-level 4

# Reload
sudo firewall-cmd --reload

การดู Direct Rules

# แสดง direct rules ทั้งหมด
sudo firewall-cmd --direct --get-all-rules

# แสดง direct rules ใน chain เฉพาะ
sudo firewall-cmd --direct --get-rules ipv4 filter INPUT

# แสดง direct chains
sudo firewall-cmd --direct --get-all-chains

การลบ Direct Rules

# ลบ rule (ใช้ syntax เหมือนตอนเพิ่มแต่เปลี่ยนเป็น --remove-rule)
sudo firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 0 -s 192.168.1.100 -j ACCEPT

# Reload
sudo firewall-cmd --reload

ตัวอย่างขั้นสูง: Custom Chain

# สร้าง custom chain
sudo firewall-cmd --permanent --direct --add-chain ipv4 filter CUSTOM_LOGGING

# เพิ่ม rules ใน custom chain
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter CUSTOM_LOGGING 0 -j LOG --log-prefix "Custom-Log: "
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter CUSTOM_LOGGING 1 -j DROP

# ส่ง traffic ไปยัง custom chain
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -s 10.0.0.0/8 -j CUSTOM_LOGGING

# Reload
sudo firewall-cmd --reload

คำแนะนำในการใช้งาน Direct Rules

  1. ใช้เป็นทางเลือกสุดท้าย: พยายามใช้ rich rules ก่อนเสมอ
  2. Document ทุก direct rule: เขียน comment หรือบันทึกไว้ว่าทำไมต้องใช้ direct rule
  3. ทดสอบอย่างละเอียด: direct rules อาจมีผลข้างเคียงที่ไม่คาดคิด
  4. Backup configuration: ก่อนเพิ่ม direct rules ที่ซับซ้อน
  5. ระวัง priority: priority ต่ำกว่าจะทำงานก่อน (0 ทำงานก่อน 1)

ตัวอย่างการใช้งานจริง

ตัวอย่างที่ 1: Web Server พื้นฐาน

Requirements:

Implementation:

# ขั้นตอนที่ 1: ตั้งค่า default zone
sudo firewall-cmd --set-default-zone=public

# ขั้นตอนที่ 2: เปิด HTTP และ HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# ขั้นตอนที่ 3: ตั้งค่า SSH ให้รับเฉพาะจาก admin network
# ลบ SSH service จาก public zone ก่อน
sudo firewall-cmd --permanent --remove-service=ssh

# เพิ่ม rich rule สำหรับ SSH
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" service name="ssh" log prefix="SSH-Admin-Access: " level="info" accept'

# ขั้นตอนที่ 4: ตั้งค่า default target เป็น DROP
sudo firewall-cmd --permanent --zone=public --set-target=DROP

# ขั้นตอนที่ 5: Reload และตรวจสอบ
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

# ขั้นตอนที่ 6: ทดสอบจากเครื่องอื่น
# curl http://your-server-ip
# ssh admin@your-server-ip  (จาก 10.0.1.0/24)

ตรวจสอบ logs:

# ดู SSH access logs
sudo journalctl -u firewalld | grep "SSH-Admin-Access"

ตัวอย่างที่ 2: Database Server ที่ปลอดภัย

Requirements:

Implementation:

# ขั้นตอนที่ 1: สร้าง custom zone สำหรับ database
sudo firewall-cmd --permanent --new-zone=database
sudo firewall-cmd --reload

# ขั้นตอนที่ 2: กำหนด interface ให้ zone
sudo firewall-cmd --permanent --zone=database --change-interface=eth0

# ขั้นตอนที่ 3: ตั้งค่า MySQL access
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.10.10" service name="mysql" log prefix="MySQL-App1: " level="info" accept'

sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.10.11" service name="mysql" log prefix="MySQL-App2: " level="info" accept'

# ขั้นตอนที่ 4: ตั้งค่า PostgreSQL access
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.10.20" service name="postgresql" log prefix="PostgreSQL-App: " level="info" accept'

# ขั้นตอนที่ 5: ตั้งค่า SSH access
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.10.1" service name="ssh" accept'

# ขั้นตอนที่ 6: ตั้งค่า default target เป็น DROP
sudo firewall-cmd --permanent --zone=database --set-target=DROP

# ขั้นตอนที่ 7: Log ทุก denied connections
sudo firewall-cmd --permanent --zone=database --add-rich-rule='rule log prefix="DB-Denied: " level="warning" drop'

# ขั้นตอนที่ 8: Reload และตรวจสอบ
sudo firewall-cmd --reload
sudo firewall-cmd --zone=database --list-all

Script สำหรับทดสอบ connection:

#!/bin/bash
# test-db-connection.sh

echo "Testing MySQL from App1 (192.168.10.10)..."
mysql -h database-server -u testuser -p

echo "Testing PostgreSQL from App (192.168.10.20)..."
psql -h database-server -U testuser -d testdb

ตัวอย่างที่ 3: Multi-tier Application

Architecture:

Internet → Load Balancer → Web Servers → App Servers → Database Server

Setup Load Balancer:

# เปิด HTTP/HTTPS จาก Internet
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https

# เปิด health check port (8080) จาก monitoring server
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port port="8080" protocol="tcp" accept'

# Reload
sudo firewall-cmd --reload

Setup Web Servers:

# สร้าง zone สำหรับ web tier
sudo firewall-cmd --permanent --new-zone=web
sudo firewall-cmd --reload

# รับ traffic จาก load balancer
sudo firewall-cmd --permanent --zone=web --add-rich-rule='rule family="ipv4" source address="192.168.2.10" service name="http" accept'
sudo firewall-cmd --permanent --zone=web --add-rich-rule='rule family="ipv4" source address="192.168.2.10" service name="https" accept'

# SSH จาก bastion
sudo firewall-cmd --permanent --zone=web --add-rich-rule='rule family="ipv4" source address="192.168.1.1" service name="ssh" accept'

# Drop อื่นๆ
sudo firewall-cmd --permanent --zone=web --set-target=DROP

# Reload
sudo firewall-cmd --reload

Setup Application Servers:

# สร้าง zone สำหรับ app tier
sudo firewall-cmd --permanent --new-zone=app
sudo firewall-cmd --reload

# รับ traffic จาก web servers (port 8080)
sudo firewall-cmd --permanent --zone=app --add-rich-rule='rule family="ipv4" source address="192.168.3.0/24" port port="8080" protocol="tcp" accept'

# SSH จาก bastion
sudo firewall-cmd --permanent --zone=app --add-rich-rule='rule family="ipv4" source address="192.168.1.1" service name="ssh" accept'

# Drop อื่นๆ
sudo firewall-cmd --permanent --zone=app --set-target=DROP

# Reload
sudo firewall-cmd --reload

Architecture Diagram:

graph TB
    Internet[Internet] --> LB[Load Balancer
Public Zone
HTTP/HTTPS: ANY] LB --> WS1[Web Server 1
Web Zone] LB --> WS2[Web Server 2
Web Zone] WS1 --> AS1[App Server 1
App Zone
Port 8080: Web Tier] WS2 --> AS1 WS1 --> AS2[App Server 2
App Zone
Port 8080: Web Tier] WS2 --> AS2 AS1 --> DB[(Database Server
Database Zone
MySQL: App Tier)] AS2 --> DB Bastion[Bastion Host
192.168.1.1] -.SSH.-> WS1 Bastion -.SSH.-> WS2 Bastion -.SSH.-> AS1 Bastion -.SSH.-> AS2 Bastion -.SSH.-> DB Monitor[Monitoring
192.168.1.100] -.Health Check.-> LB style LB fill:#f96,stroke:#333,stroke-width:2px style WS1 fill:#9cf,stroke:#333 style WS2 fill:#9cf,stroke:#333 style AS1 fill:#9f9,stroke:#333 style AS2 fill:#9f9,stroke:#333 style DB fill:#f9f,stroke:#333,stroke-width:2px style Bastion fill:#fc9,stroke:#333

ตัวอย่างที่ 4: VPN Server

Requirements:

Implementation:

# ขั้นตอนที่ 1: เปิด OpenVPN port
sudo firewall-cmd --permanent --add-port=1194/udp

# ขั้นตอนที่ 2: เปิด TUN interface สำหรับ VPN
sudo firewall-cmd --permanent --zone=trusted --add-interface=tun0

# ขั้นตอนที่ 3: เปิด masquerading
sudo firewall-cmd --permanent --zone=public --add-masquerade

# ขั้นตอนที่ 4: Forward traffic จาก VPN ไปยัง internal network
sudo firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s 10.8.0.0/24 -o eth0 -j MASQUERADE

# ขั้นตอนที่ 5: อนุญาต forwarding
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i tun0 -o eth0 -j ACCEPT
sudo firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i eth0 -o tun0 -m state --state RELATED,ESTABLISHED -j ACCEPT

# ขั้นตอนที่ 6: Reload
sudo firewall-cmd --reload

# ขั้นตอนที่ 7: Enable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf

ตัวอย่างที่ 5: Development Environment

Requirements:

Implementation:

# ขั้นตอนที่ 1: เปลี่ยนไปใช้ zone home หรือ internal
sudo firewall-cmd --set-default-zone=home

# ขั้นตอนที่ 2: เปิด development ports
sudo firewall-cmd --permanent --add-port=3000-3100/tcp
sudo firewall-cmd --permanent --add-port=8000-8100/tcp

# ขั้นตอนที่ 3: เปิด database services
sudo firewall-cmd --permanent --add-service=mysql
sudo firewall-cmd --permanent --add-service=postgresql
sudo firewall-cmd --permanent --add-service=mongodb
sudo firewall-cmd --permanent --add-service=redis

# ขั้นตอนที่ 4: เปิด Docker ports (ถ้าใช้)
sudo firewall-cmd --permanent --zone=docker --add-interface=docker0

# ขั้นตอนที่ 5: Reload
sudo firewall-cmd --reload

# ขั้นตอนที่ 6: ตรวจสอบ
sudo firewall-cmd --list-all

Best Practices

1. Principle of Least Privilege

เปิดเฉพาะ ports และ services ที่จำเป็นจริงๆ เท่านั้น

# ❌ ไม่ดี: เปิดทุกอย่าง
sudo firewall-cmd --set-default-zone=trusted

# ✅ ดี: เปิดเฉพาะที่จำเป็น
sudo firewall-cmd --set-default-zone=public
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

2. ใช้ Zones อย่างเหมาะสม

แยก zones ตามระดับความเชื่อถือและหน้าที่

# Public-facing services
sudo firewall-cmd --permanent --zone=public --add-service=http

# Internal services
sudo firewall-cmd --permanent --zone=internal --add-service=mysql

# Management
sudo firewall-cmd --permanent --zone=internal --add-service=ssh

3. Document Configuration

บันทึกทุกการเปลี่ยนแปลงและเหตุผล

# สร้างไฟล์ documentation
sudo nano /etc/firewalld/FIREWALL_RULES.md
# Firewall Configuration Documentation

## Last Updated: 2025-01-15
## Maintained by: SysAdmin Team

### Zone: public
- HTTP (80/tcp): Web server for public website
- HTTPS (443/tcp): SSL web server for public website
- Added: 2025-01-10
- Ticket: #12345

### Zone: internal
- SSH (22/tcp): Management access from admin network (10.0.1.0/24)
- MySQL (3306/tcp): Database access from app servers
- Added: 2025-01-12
- Ticket: #12346

4. ทดสอบก่อนทำให้ Permanent

# 1. เพิ่มแบบ runtime ก่อน
sudo firewall-cmd --add-service=http

# 2. ทดสอบ
curl http://localhost

# 3. ถ้าทำงาน ค่อยทำให้ permanent
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

5. Backup Configuration

# Backup firewalld configuration
sudo tar -czf firewalld-backup-$(date +%Y%m%d).tar.gz /etc/firewalld/

# Backup iptables rules (สำรอง)
sudo iptables-save > iptables-backup-$(date +%Y%m%d).txt

# เก็บ backup ไว้ที่ปลอดภัย
sudo mv firewalld-backup-*.tar.gz /backup/firewall/

6. ใช้ Rich Rules สำหรับ Logging

# Log SSH attempts
sudo firewall-cmd --permanent --add-rich-rule='rule service name="ssh" log prefix="SSH-Attempt: " level="info" limit value="3/m" accept'

# Log dropped packets
sudo firewall-cmd --permanent --add-rich-rule='rule log prefix="Dropped: " level="warning" limit value="10/m" drop'

7. Rate Limiting

ป้องกัน brute force และ DDoS

# SSH rate limiting
sudo firewall-cmd --permanent --add-rich-rule='rule service name="ssh" limit value="3/m" accept'

# HTTP rate limiting
sudo firewall-cmd --permanent --add-rich-rule='rule service name="http" limit value="100/s" accept'

8. Regular Auditing

ตรวจสอบ configuration เป็นประจำ

#!/bin/bash
# firewall-audit.sh

echo "=== Firewall Audit Report ==="
echo "Date: $(date)"
echo ""

echo "Firewalld Status:"
sudo firewall-cmd --state
echo ""

echo "Active Zones:"
sudo firewall-cmd --get-active-zones
echo ""

echo "Default Zone:"
sudo firewall-cmd --get-default-zone
echo ""

echo "Public Zone Configuration:"
sudo firewall-cmd --zone=public --list-all
echo ""

echo "Rich Rules:"
sudo firewall-cmd --list-rich-rules
echo ""

echo "Direct Rules:"
sudo firewall-cmd --direct --get-all-rules

9. Use Lockdown Mode (Production)

# เปิด lockdown mode เพื่อป้องกันการเปลี่ยนแปลงโดยไม่ได้รับอนุญาต
sudo firewall-cmd --lockdown-on

# ตรวจสอบ
sudo firewall-cmd --query-lockdown

# เพิ่ม application ที่อนุญาตให้แก้ไข firewall
sudo firewall-cmd --permanent --add-lockdown-whitelist-context=system_u:system_r:NetworkManager_t:s0

# ปิด lockdown mode (เมื่อต้องการแก้ไข)
sudo firewall-cmd --lockdown-off

10. Monitoring และ Alerting

# Setup log monitoring
sudo nano /etc/rsyslog.d/firewall.conf
# Log firewall messages to separate file
:msg, contains, "DROPPED: " /var/log/firewall-dropped.log
:msg, contains, "SSH-Attempt: " /var/log/firewall-ssh.log
& stop
# Restart rsyslog
sudo systemctl restart rsyslog

# Setup logrotate
sudo nano /etc/logrotate.d/firewall
/var/log/firewall-*.log {
    daily
    rotate 30
    compress
    delaycompress
    notifempty
    create 0640 root root
    sharedscripts
    postrotate
        /usr/bin/systemctl reload rsyslog > /dev/null 2>&1 || true
    endscript
}

11. Change Management Process

สร้างกระบวนการสำหรับการเปลี่ยนแปลง firewall rules:

  1. Request: สร้าง ticket หรือ change request
  2. Review: ให้ security team review
  3. Test: ทดสอบใน staging environment
  4. Implement: ใช้งานใน production
  5. Verify: ตรวจสอบว่าทำงานถูกต้อง
  6. Document: บันทึกการเปลี่ยนแปลง

12. Security Checklist

## Firewall Security Checklist

- [ ] Default zone เป็น public หรือ restrictive กว่า
- [ ] SSH มี rate limiting
- [ ] Services ที่ไม่จำเป็นถูกปิด
- [ ] Rich rules มี logging
- [ ] Backup configuration ทำเป็นประจำ
- [ ] Documentation ทันสมัย
- [ ] Monitoring และ alerting ทำงาน
- [ ] Lockdown mode เปิดใน production
- [ ] Audit logs ถูกเก็บและ review
- [ ] Change management process ถูกปฏิบัติตาม

Troubleshooting

ปัญหาที่พบบ่อยและวิธีแก้ไข

1. ไม่สามารถเชื่อมต่อหลังเปิด Firewalld

อาการ:

วิธีแก้:

# ตรวจสอบว่า service เปิดอยู่หรือไม่
sudo firewall-cmd --list-services

# เพิ่ม service ที่จำเป็น
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

# ถ้ายังไม่ได้ ลอง temporarily ปิด firewalld
sudo systemctl stop firewalld
# ทดสอบ connection
# ถ้าได้แล้ว เปิดกลับและแก้ไข rules
sudo systemctl start firewalld

2. Firewalld ไม่ Start

อาการ:

Failed to start firewalld.service

วิธีแก้:

# ตรวจสอบ status และ errors
sudo systemctl status firewalld -l

# ตรวจสอบ configuration errors
sudo firewall-cmd --check-config

# ดู logs
sudo journalctl -xeu firewalld

# ถ้ามี syntax error ใน configuration
# Backup และลบ configuration files ที่เป็นปัญหา
sudo mv /etc/firewalld/zones/problematic.xml /tmp/

# Restart
sudo systemctl restart firewalld

3. Rules ไม่มีผล

อาการ:

วิธีแก้:

# ตรวจสอบว่า rules อยู่ใน zone ที่ถูกต้อง
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all-zones

# ตรวจสอบว่า interface อยู่ใน zone ที่ถูกต้อง
sudo firewall-cmd --get-zone-of-interface=eth0

# Reload firewalld
sudo firewall-cmd --reload

# ตรวจสอบ runtime และ permanent rules
sudo firewall-cmd --list-all
sudo firewall-cmd --permanent --list-all

4. Port Forwarding ไม่ทำงาน

อาการ:

วิธีแก้:

# ตรวจสอบว่าเปิด masquerading
sudo firewall-cmd --query-masquerade

# เปิด masquerading
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --reload

# ตรวจสอบว่า IP forwarding เปิดอยู่
sudo sysctl net.ipv4.ip_forward
# ถ้าเป็น 0 ให้เปิด
sudo sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf

# ตรวจสอบ forward-ports
sudo firewall-cmd --list-forward-ports

# ตรวจสอบ direct rules
sudo firewall-cmd --direct --get-all-rules

5. Performance Issues

อาการ:

วิธีแก้:

# ตรวจสอบจำนวน rules
sudo iptables -L -n | wc -l

# ลด logging ถ้ามีมากเกินไป
sudo firewall-cmd --list-rich-rules | grep "log"

# ปิด logging ที่ไม่จำเป็น
sudo firewall-cmd --permanent --remove-rich-rule='...'

# ใช้ ipsets สำหรับ large IP lists แทน individual rules

# Restart firewalld
sudo firewall-cmd --reload

6. Conflict กับ Docker

อาการ:

วิธีแก้:

# เพิ่ม docker interface ใน trusted zone
sudo firewall-cmd --permanent --zone=trusted --add-interface=docker0

# อนุญาต masquerading
sudo firewall-cmd --permanent --zone=trusted --add-masquerade

# Reload
sudo firewall-cmd --reload

# หรืออาจต้อง disable firewalld สำหรับ docker
# โดยแก้ไข /etc/firewalld/firewalld.conf
# AllowZoneDrifting=yes

# Restart docker
sudo systemctl restart docker

เครื่องมือสำหรับ Debug

1. ตรวจสอบ Connection

# ดู active connections
sudo ss -tunap

# ตรวจสอบ specific port
sudo ss -tunap | grep :80

# ใช้ netstat (ถ้ามี)
sudo netstat -tunap | grep LISTEN

# ตรวจสอบ ว่า service listen บน port ที่ถูกต้อง
sudo lsof -i :80

2. ทดสอบ Connectivity

# จากเครื่อง local
telnet server-ip 80
nc -zv server-ip 80

# จาก server
curl http://localhost
wget http://localhost

# ทดสอบจาก specific interface
curl --interface eth0 http://target-ip

3. Packet Tracing

# เปิด logging ชั่วคราวสำหรับ debug
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="client-ip" log prefix="DEBUG: " level="info"'

# ดู logs
sudo tail -f /var/log/messages | grep "DEBUG:"

# ใช้ tcpdump
sudo tcpdump -i eth0 port 80 -n

# ใช้ iptables tracing (advanced)
sudo iptables -t raw -A PREROUTING -p tcp --dport 80 -j TRACE
sudo iptables -t raw -A OUTPUT -p tcp --sport 80 -j TRACE

# ดู trace logs
sudo tail -f /var/log/messages | grep TRACE

4. ตรวจสอบ iptables Rules

# ดู current iptables rules
sudo iptables -L -n -v
sudo iptables -t nat -L -n -v

# Save เป็นไฟล์เพื่อ analyze
sudo iptables-save > /tmp/iptables-dump.txt

# Compare กับ firewalld rules
sudo firewall-cmd --direct --get-all-rules

ขั้นตอนการ Debug แบบเป็นระบบ

Step 1: Verify Basic Connectivity

# Ping server
ping server-ip

# Check if port is open
nmap -p 80 server-ip

Step 2: Check Firewalld Status

sudo firewall-cmd --state
sudo firewall-cmd --list-all

Step 3: Check Service/Application

# Check if service is running
sudo systemctl status httpd
sudo ss -tunlp | grep :80

Step 4: Temporarily Disable Firewall

# ⚠️ Use only for testing!
sudo systemctl stop firewalld
# Test connection
# Start firewall again
sudo systemctl start firewalld

Step 5: Check Logs

# Firewalld logs
sudo journalctl -u firewalld -f

# System logs
sudo tail -f /var/log/messages

# Application logs
sudo tail -f /var/log/httpd/error_log

Step 6: Add Debugging Rules

# Log everything temporarily
sudo firewall-cmd --add-rich-rule='rule family="ipv4" log prefix="DEBUG-ALL: " level="info"'

# Test and watch logs
sudo tail -f /var/log/messages | grep "DEBUG-ALL"

# Remove debug rule
sudo firewall-cmd --remove-rich-rule='rule family="ipv4" log prefix="DEBUG-ALL: " level="info"'

Emergency Recovery

หาก firewall configuration พังและไม่สามารถเข้าระบบได้:

Option 1: Through Console/IPMI

# Login through console
# Stop firewalld
sudo systemctl stop firewalld

# Fix configuration
sudo nano /etc/firewalld/zones/public.xml

# Start firewalld
sudo systemctl start firewalld

Option 2: Boot to Single User Mode

# During boot, edit grub entry and add: systemd.unit=rescue.target
# Login as root
# Fix firewall configuration
# Reboot

Option 3: Disable Firewalld Temporarily

# Add to kernel parameters during boot: firewalld.autostart=0
# Or from console:
sudo systemctl disable firewalld
sudo reboot
# Fix issues
# Re-enable firewalld
sudo systemctl enable firewalld

สรุป

Firewalld เป็นเครื่องมือจัดการ firewall ที่ทรงพลังและยืดหยุ่นสำหรับระบบ Linux โดยเฉพาะ RHEL, CentOS, และ Fedora ด้วยแนวคิดของ zones และ services ทำให้การจัดการ network security เป็นเรื่องง่ายและเป็นระบบมากขึ้น

จุดเด่นของ Firewalld

Dynamic Management - เปลี่ยนแปลง rules โดยไม่ต้อง restart service ✅ Zone-based Configuration - จัดการ security policies ตาม network environment ✅ User-friendly Commands - ใช้งานง่ายกว่า iptables โดยตรง ✅ Rich Rules - สร้างกฎที่ซับซ้อนได้อย่างละเอียด ✅ D-Bus Interface - Integration กับ applications และ tools อื่นๆ ✅ IPv4/IPv6 Support - รองรับทั้งสอง protocols ✅ Logging และ Auditing - ติดตามและบันทึกการเข้าถึง

แนวทางการใช้งาน

  1. เริ่มต้นด้วย Default Zones - ใช้ predefined zones ก่อนสร้าง custom zones
  2. ทดสอบก่อนทำให้ Permanent - ใช้ runtime configuration ทดสอบก่อน
  3. Document ทุกอย่าง - บันทึกการเปลี่ยนแปลงและเหตุผล
  4. ใช้ Rich Rules สำหรับ Complex Requirements - แทนการใช้ direct rules
  5. Monitor และ Audit เป็นประจำ - ตรวจสอบ logs และ configuration
  6. Backup Configuration - สำรอง configuration ก่อนเปลี่ยนแปลงสำคัญ
  7. Follow Best Practices - ใช้ principle of least privilege
  8. Keep System Updated - อัพเดท firewalld และ security patches เป็นประจำ

ทรัพยากรเพิ่มเติม

บทสรุปสุดท้าย

การรักษาความปลอดภัยของเครือข่ายเป็นสิ่งสำคัญในทุกระบบ Firewalld ช่วยให้เราสามารถจัดการและควบคุม network traffic ได้อย่างมีประสิทธิภาพ โดยไม่ลดทอนความสะดวกในการใช้งาน การเรียนรู้และใช้งาน Firewalld อย่างถูกต้องจะช่วยเพิ่มระดับความปลอดภัยของระบบให้สูงขึ้นอย่างมีนัยสำคัญ

อย่างไรก็ตาม Firewall เป็นเพียงส่วนหนึ่งของ security strategy การรักษาความปลอดภัยที่ดีต้องมีหลายชั้น (defense in depth) ร่วมกับการอัพเดทระบบ, การใช้ strong authentication, การ monitor logs, และการ backup อย่างสม่ำเสมอ


เอกสารอ้างอิง

  1. Firewalld Official Documentation - https://firewalld.org/
  2. Red Hat Enterprise Linux Security Guide
  3. CentOS/RHEL System Administrator's Guide
  4. Linux Firewall by Michael Rash
  5. NIST Cybersecurity Framework

สิ้นสุดคู่มือการใช้งาน Firewalld

เอกสารนี้จัดทำขึ้นเพื่อการศึกษาและใช้เป็นแนวทางในการจัดการ Firewalld บน Linux Systems