คู่มือ nftables ฉบับสมบูรณ์: ย้ายจาก iptables และ Hardening ไฟร์วอลล์ Linux ปี 2026

คู่มือการย้ายจาก iptables ไป nftables แบบ step-by-step พร้อมเทคนิค hardening ไฟร์วอลล์ Linux ด้วย rate limiting, dynamic IP blocking, Sets, Maps และ Verdict Maps รวม config ใช้งานจริงสำหรับ web server และ database server

บทนำ: ทำไมต้องเปลี่ยนมาใช้ nftables ในปี 2026

ถ้าคุณยังใช้ iptables อยู่ในปี 2026 ต้องบอกตรงๆ ว่าถึงเวลาเปลี่ยนแล้วครับ iptables เคยเป็นเครื่องมือไฟร์วอลล์มาตรฐานบน Linux มานานกว่า 20 ปี แต่ตอนนี้มันถูกประกาศเป็น legacy อย่างเป็นทางการแล้ว และ nftables ก็กลายเป็น default firewall framework ใน distro หลักๆ เกือบทุกตัว ไม่ว่าจะเป็น Debian 10+, Ubuntu 20.04+, RHEL 9+, Fedora หรือ openSUSE

พูดจากประสบการณ์ตรงเลยนะครับ ตอนที่ผมย้ายเซิร์ฟเวอร์จาก iptables มา nftables ครั้งแรก ก็กังวลอยู่เหมือนกัน แต่พอลองทำจริงกลับรู้สึกว่าทำไมไม่ย้ายมาตั้งนานแล้ว

ปัญหาใหญ่ของ iptables คือมันใช้เครื่องมือแยกกันถึง 4 ตัวสำหรับแต่ละ protocol family: iptables, ip6tables, arptables และ ebtables ทำให้การจัดการซับซ้อน syntax ยืดยาว แถม performance ก็ไม่ดีเท่าที่ควรเมื่อต้องรับมือกับ rule set ขนาดใหญ่ nftables แก้ปัญหาพวกนี้ได้หมดด้วยเครื่องมือตัวเดียวคือ nft

บทความนี้จะพาคุณผ่านทุกขั้นตอนครับ ตั้งแต่เข้าใจสถาปัตยกรรมของ nftables การย้ายจาก iptables ไปจนถึงการ hardening ไฟร์วอลล์เซิร์ฟเวอร์ Linux ด้วยเทคนิคขั้นสูงอย่าง rate limiting, dynamic IP blocking และ verdict maps

nftables คืออะไร และแตกต่างจาก iptables อย่างไร

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

nftables เป็น packet filtering framework ที่ทำงานร่วมกับ Netfilter ใน Linux kernel โดยใช้สถาปัตยกรรมที่ทันสมัยกว่า iptables อย่างมาก โครงสร้างหลักมีแบบนี้ครับ:

  • Tables — namespace ที่รวบรวม chains, rules, sets และ objects ต่างๆ แต่ละ table ต้องกำหนด address family เช่น ip, ip6, inet, bridge, arp, netdev
  • Chains — container สำหรับ rules แบ่งเป็น base chains (รับ packets จาก networking stack) และ regular chains (ใช้เป็น jump target)
  • Rules — กฎที่กำหนดว่าจะทำอย่างไรกับ packet ที่ตรงเงื่อนไข
  • Sets/Maps — data structures สำหรับจัดกลุ่มค่าต่างๆ ใช้ hashtable หรือ red-black tree ภายใน ทำให้ lookup เร็วมากๆ

ข้อได้เปรียบหลักของ nftables เทียบกับ iptables

มาดูตารางเปรียบเทียบกันให้ชัดเจนครับ:

คุณสมบัติ iptables nftables
เครื่องมือ CLI 4 ตัวแยกกัน (iptables, ip6tables, arptables, ebtables) ตัวเดียว (nft)
IPv4/IPv6 ต้องเขียน rule ซ้ำ ใช้ inet family รวมทั้งสอง
Default chains สร้างอัตโนมัติ ประมวลผลทุก packet ไม่มี ต้องสร้างเอง ลดค่า overhead
การ update rules ต้องแทนที่ทั้ง chain Atomic transactions
Data structures ipset (ติดตั้งเพิ่ม) Sets, Maps, vmaps ในตัว
Performance Linear rule matching Hashtable/red-black tree lookups
Debugging จำกัด nftrace สำหรับ tracing

จุดที่สำคัญที่สุดเลยคือ nftables ไม่บังคับสร้าง default chains และ tables ให้อัตโนมัติ ใน iptables ทุก packet จะต้องผ่าน chains ที่สร้างไว้ล่วงหน้าแม้จะไม่มี rule ใดๆ แต่ nftables ให้คุณสร้างเฉพาะสิ่งที่ต้องใช้จริง ลด overhead ได้เยอะมากสำหรับเซิร์ฟเวอร์ที่รับ traffic สูงๆ

การติดตั้งและเตรียมพร้อม nftables

เอาล่ะ มาเริ่มลงมือกันเลยครับ

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

# อัปเดต package list และติดตั้ง nftables
sudo apt update && sudo apt install nftables -y

# เปิดใช้งาน nftables service ให้ start ตอน boot
sudo systemctl enable nftables
sudo systemctl start nftables

# ตรวจสอบเวอร์ชัน
nft --version

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

# ติดตั้ง nftables (RHEL 9+ มี nftables เป็น default)
sudo dnf install nftables -y

# เปิดใช้งาน service
sudo systemctl enable nftables
sudo systemctl start nftables

# ถ้าใช้ firewalld อยู่ ให้ตรวจสอบว่า backend เป็น nftables
sudo grep FirewallBackend /etc/firewalld/firewalld.conf

ตรวจสอบว่า iptables ยังทำงานอยู่หรือไม่

ขั้นตอนนี้สำคัญนะครับ อย่าข้ามไป เพราะถ้า iptables-legacy ยังมี rules ค้างอยู่ อาจจะเกิด conflict ได้

# ตรวจสอบว่า iptables-legacy ยังมี rules อยู่ไหม
sudo iptables-legacy -L -n
sudo ip6tables-legacy -L -n

# ดู alternatives ว่าระบบใช้ backend ตัวไหน
sudo update-alternatives --display iptables 2>/dev/null || echo "No alternatives configured"

การย้ายจาก iptables ไป nftables: ทีละขั้นตอน

ขั้นตอนที่ 1: สำรอง iptables rules ที่มีอยู่

ก่อนทำอะไรทั้งหมด สำรองข้อมูลเสมอครับ ข้อนี้เป็นกฎเหล็ก (เชื่อผมเถอะ เคยพลาดมาแล้ว)

# สำรอง rules ปัจจุบัน
sudo iptables-save > ~/iptables-backup-$(date +%Y%m%d).rules
sudo ip6tables-save > ~/ip6tables-backup-$(date +%Y%m%d).rules

# ตรวจสอบจำนวน rules
echo "IPv4 rules: $(sudo iptables-save | grep -c '^-A')"
echo "IPv6 rules: $(sudo ip6tables-save | grep -c '^-A')"

ขั้นตอนที่ 2: แปลง rules ด้วย iptables-translate

ข่าวดีคือ Linux มีเครื่องมือแปลง rules ให้อัตโนมัติครับ ไม่ต้องนั่งเขียนใหม่เองทั้งหมด:

# แปลง rule เดี่ยว
iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
# ผลลัพธ์: nft add rule ip filter INPUT tcp dport 22 counter accept

# แปลง rule ที่ซับซ้อนกว่า
iptables-translate -A INPUT -p tcp -m multiport --dports 80,443 -m state --state NEW -j ACCEPT
# ผลลัพธ์: nft add rule ip filter INPUT tcp dport { 80, 443 } ct state new counter accept

# แปลงทั้ง ruleset จากไฟล์ backup
iptables-restore-translate -f ~/iptables-backup-*.rules > ~/nftables-migrated.nft

# ตรวจสอบผลลัพธ์ก่อน apply
cat ~/nftables-migrated.nft

สำคัญ: ไม่ใช่ทุก rules จะแปลงได้สมบูรณ์ 100% นะครับ ตรวจสอบ rules ที่แปลงแล้วเสมอ โดยเฉพาะ rules ที่ใช้ custom modules หรือ extensions เฉพาะทาง บาง rules อาจต้องเขียนใหม่ด้วยมือ

ขั้นตอนที่ 3: Load rules ที่แปลงแล้ว

# validate ก่อน apply (dry-run)
sudo nft -c -f ~/nftables-migrated.nft

# ถ้าไม่มี error ให้ load จริง
sudo nft -f ~/nftables-migrated.nft

# ตรวจสอบ rules ที่ load แล้ว
sudo nft list ruleset

ขั้นตอนที่ 4: ปิดการใช้งาน iptables-legacy

# เปลี่ยน alternatives ไปใช้ nft backend
sudo update-alternatives --set iptables /usr/sbin/iptables-nft
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-nft

# ปิด iptables service (ถ้ามี)
sudo systemctl disable iptables 2>/dev/null
sudo systemctl stop iptables 2>/dev/null

คำเตือนจริงจัง: อย่าใช้ iptables-legacy กับ nftables พร้อมกันเด็ดขาด มันจะสร้าง ruleset ที่ขัดแย้งกันและอาจทำให้ traffic หลุดหรือถูก block โดยไม่ตั้งใจ ในระหว่าง migration ให้เลือกใช้ backend ตัวเดียว หรือใช้ iptables-nft compatibility layer แทน

การเขียน nftables Rules พื้นฐาน

ตรงนี้ถ้าใครเคยเขียน iptables มาก่อน จะเห็นว่า syntax ของ nftables อ่านง่ายกว่าเยอะเลยครับ

สร้าง Table และ Chain แรก

# สร้าง table ที่รองรับทั้ง IPv4 และ IPv6
sudo nft add table inet myfilter

# สร้าง input chain ด้วย default policy drop
sudo nft add chain inet myfilter input \
  '{ type filter hook input priority 0; policy drop; }'

# สร้าง forward chain
sudo nft add chain inet myfilter forward \
  '{ type filter hook forward priority 0; policy drop; }'

# สร้าง output chain
sudo nft add chain inet myfilter output \
  '{ type filter hook output priority 0; policy accept; }'

เพิ่ม Rules พื้นฐาน

# อนุญาต loopback traffic
sudo nft add rule inet myfilter input iifname lo accept

# อนุญาต connections ที่ established/related
sudo nft add rule inet myfilter input ct state established,related accept

# อนุญาต SSH (port 22)
sudo nft add rule inet myfilter input tcp dport 22 ct state new accept

# อนุญาต HTTP และ HTTPS
sudo nft add rule inet myfilter input tcp dport { 80, 443 } ct state new accept

# อนุญาต ICMP ping (จำกัด rate)
sudo nft add rule inet myfilter input ip protocol icmp icmp type echo-request limit rate 1/second accept
sudo nft add rule inet myfilter input ip6 nexthdr icmpv6 icmpv6 type echo-request limit rate 1/second accept

# Log แล้ว drop ทุกอย่างที่เหลือ
sudo nft add rule inet myfilter input log prefix \"[nftables-drop] \" drop

ดู Rules ทั้งหมด

# แสดง ruleset ทั้งหมด
sudo nft list ruleset

# แสดงเฉพาะ table
sudo nft list table inet myfilter

# แสดงเฉพาะ chain พร้อม handle numbers (สำหรับลบ rule เฉพาะ)
sudo nft -a list chain inet myfilter input

Hardening ไฟร์วอลล์เซิร์ฟเวอร์: Default-Deny Ruleset ฉบับสมบูรณ์

ส่วนนี้เป็นหัวใจของบทความเลยครับ ผมจะให้ config ที่ใช้งานได้จริงเลย ไม่ใช่แค่ตัวอย่างแบบผิวเผิน

Config สำหรับ Web Server

นี่คือ configuration ที่ผมแนะนำสำหรับ web server ที่ต้องการความปลอดภัยสูงครับ:

#!/usr/sbin/nft -f
# /etc/nftables.conf — Hardened Web Server Configuration

flush ruleset

# กำหนดตัวแปร
define SSH_PORT = 22
define WEB_PORTS = { 80, 443 }
define ADMIN_IPS = { 203.0.113.10, 203.0.113.20 }

table inet firewall {

    # Set สำหรับ IP ที่ถูก block (auto-expiring)
    set blocklist {
        type ipv4_addr
        flags dynamic,timeout
        timeout 1h
    }

    # Set สำหรับ rate limiting tracker
    set ssh_ratelimit {
        type ipv4_addr
        flags dynamic,timeout
        timeout 5m
    }

    chain input {
        type filter hook input priority filter; policy drop;

        # Drop packets จาก blocklist ก่อน
        ip saddr @blocklist drop

        # อนุญาต loopback
        iifname "lo" accept

        # Drop invalid connections
        ct state invalid drop

        # อนุญาต established/related connections
        ct state { established, related } accept

        # Rate limit ICMP
        ip protocol icmp icmp type echo-request \
            limit rate 5/second burst 10 packets accept

        ip6 nexthdr icmpv6 icmpv6 type { echo-request, nd-neighbor-solicit, nd-router-advert, nd-neighbor-advert } \
            limit rate 5/second burst 10 packets accept

        # SSH — จำกัดเฉพาะ ADMIN_IPs และ rate limit
        tcp dport $SSH_PORT ip saddr $ADMIN_IPS ct state new accept

        # Web ports — อนุญาตจากทุกที่
        tcp dport $WEB_PORTS ct state new accept

        # Log packets ที่ถูก drop (จำกัด rate ของ log)
        limit rate 5/minute burst 10 packets \
            log prefix "[nft-drop-input] " level warn

        # Counter สำหรับ monitoring
        counter comment "dropped input packets"
    }

    chain forward {
        type filter hook forward priority filter; policy drop;
        counter comment "dropped forward packets"
    }

    chain output {
        type filter hook output priority filter; policy accept;
        # อนุญาต outbound ทั้งหมด (ปรับตามต้องการ)
    }
}

สังเกตว่าเรา define ตัวแปรไว้ข้างบนนะครับ ทำให้แก้ไขง่าย ไม่ต้องไล่หาค่าทั่วทั้งไฟล์

Config สำหรับ Database Server (เข้าถึงจาก internal เท่านั้น)

สำหรับ database server หลักการคือ lock down ให้มากที่สุด เปิดเฉพาะ port ที่จำเป็นจริงๆ เท่านั้น:

#!/usr/sbin/nft -f
# nftables config สำหรับ Database Server

flush ruleset

define INTERNAL_NET = 10.0.0.0/8
define DB_PORTS = { 3306, 5432, 6379 }
define SSH_PORT = 22
define ADMIN_IPS = { 10.0.1.10, 10.0.1.20 }

table inet firewall {

    chain input {
        type filter hook input priority filter; policy drop;

        iifname "lo" accept
        ct state invalid drop
        ct state { established, related } accept

        # SSH เฉพาะจาก Admin IPs
        tcp dport $SSH_PORT ip saddr $ADMIN_IPS ct state new accept

        # Database ports เฉพาะจาก internal network
        tcp dport $DB_PORTS ip saddr $INTERNAL_NET ct state new accept

        # Drop ทุกอย่างจากภายนอก
        log prefix "[nft-db-drop] " limit rate 3/minute
        counter
    }

    chain forward {
        type filter hook forward priority filter; policy drop;
    }

    chain output {
        type filter hook output priority filter; policy accept;
    }
}

เทคนิคขั้นสูง: Rate Limiting และ Brute Force Protection

ถ้าคุณเคยเจอ log เต็มไปด้วย failed SSH attempts คุณจะเข้าใจว่าส่วนนี้สำคัญแค่ไหน

ป้องกัน SSH Brute Force ด้วย Dynamic Sets

เทคนิคนี้เจ๋งมากครับ มันจะ track IP ที่พยายาม connect SSH ถี่เกินไป แล้ว block ให้อัตโนมัติ โดยไม่ต้องพึ่งเครื่องมือภายนอก:

#!/usr/sbin/nft -f
flush ruleset

table inet ssh_protection {

    # IP ที่ถูก ban (auto-expire หลัง 30 นาที)
    set banned_hosts {
        type ipv4_addr
        flags dynamic,timeout
        timeout 30m
    }

    # นับจำนวน connection ต่อ IP
    set conn_tracker {
        type ipv4_addr
        flags dynamic,timeout
        timeout 2m
    }

    chain input {
        type filter hook input priority filter; policy accept;

        # Drop ทันทีถ้าอยู่ใน banned list
        tcp dport 22 ip saddr @banned_hosts drop

        # ถ้า connection ใหม่ไป SSH เกิน 5 ครั้งใน 2 นาที → เพิ่มเข้า banned list
        tcp dport 22 ct state new \
            add @conn_tracker { ip saddr limit rate over 5/minute } \
            add @banned_hosts { ip saddr } \
            drop

        # อนุญาต SSH connection ปกติ
        tcp dport 22 ct state new accept
    }
}

ตรงนี้ผมชอบมากคือ banned IP จะ expire อัตโนมัติหลัง 30 นาที ไม่ต้องมานั่ง unban เอง แต่ถ้าอยากให้ ban นานขึ้นก็แก้ timeout ได้เลย

ป้องกัน SYN Flood และ DDoS เบื้องต้น

table inet ddos_protection {

    chain input {
        type filter hook input priority -10; policy accept;

        # Drop invalid packets
        ct state invalid drop

        # จำกัด TCP SYN packets (ป้องกัน SYN flood)
        tcp flags syn limit rate over 25/second burst 50 packets drop

        # จำกัด new connections ต่อ source IP
        ct state new limit rate over 30/second burst 50 packets drop

        # จำกัด ICMP (ป้องกัน ping flood)
        ip protocol icmp limit rate over 10/second burst 20 packets drop

        # จำกัด UDP packets (ป้องกัน UDP flood)
        ip protocol udp limit rate over 50/second burst 100 packets drop
    }
}

ย้ำว่านี่คือการป้องกัน DDoS "เบื้องต้น" นะครับ สำหรับ DDoS ขนาดใหญ่จริงๆ คุณจะต้องใช้โซลูชันระดับ network edge อย่าง Cloudflare หรือ AWS Shield ด้วย

Rate Limiting แบบ Per-IP ด้วย Meters

# จำกัด HTTP requests ต่อ IP (ป้องกัน Layer 7 DDoS)
sudo nft add rule inet firewall input \
    tcp dport { 80, 443 } ct state new \
    meter http_meter { ip saddr limit rate over 100/second burst 200 packets } \
    drop

ฟีเจอร์ขั้นสูง: Sets, Maps และ Verdict Maps

Named Sets สำหรับจัดการ IP

Named sets เป็นฟีเจอร์ที่ทรงพลังมากของ nftables ครับ สามารถ update ได้ขณะ runtime โดยไม่ต้อง reload rules ทั้งหมด (ซึ่งถ้าเคยใช้ iptables + ipset จะรู้ว่าเดิมมันยุ่งยากกว่านี้เยอะ):

# สร้าง set สำหรับ IP ที่อนุญาต
sudo nft add set inet firewall whitelist { type ipv4_addr \; }
sudo nft add element inet firewall whitelist { 192.168.1.100, 192.168.1.101, 10.0.0.0/24 }

# สร้าง set สำหรับ IP ที่ block
sudo nft add set inet firewall blacklist { type ipv4_addr \; flags interval \; }
sudo nft add element inet firewall blacklist { 203.0.113.0/24, 198.51.100.0/24 }

# ใช้ set ใน rules
sudo nft add rule inet firewall input ip saddr @whitelist accept
sudo nft add rule inet firewall input ip saddr @blacklist drop

# อัปเดต set ขณะ runtime (ไม่ต้อง reload)
sudo nft add element inet firewall blacklist { 192.0.2.50 }
sudo nft delete element inet firewall blacklist { 198.51.100.0/24 }

Verdict Maps (vmaps): การ routing แบบ dynamic

Verdict maps ช่วยให้คุณ map ค่าไปยัง verdict (accept, drop, jump) โดยตรงครับ ทำให้ rules กระชับและมีประสิทธิภาพขึ้นมาก แทนที่จะเขียน if-else ยาวๆ:

table inet firewall {

    # vmap สำหรับ routing ตาม port
    map port_policy {
        type inet_service : verdict
        elements = {
            22 : jump ssh_chain,
            80 : jump web_chain,
            443 : jump web_chain,
            3306 : drop,
        }
    }

    chain input {
        type filter hook input priority filter; policy drop;
        iifname "lo" accept
        ct state { established, related } accept

        # ใช้ vmap เพื่อ route traffic ไปยัง chain ที่เหมาะสม
        tcp dport vmap @port_policy

        log prefix "[nft-unmatched] "
    }

    chain ssh_chain {
        # SSH rules เฉพาะ
        ip saddr 10.0.0.0/8 accept
        limit rate 3/minute burst 5 packets accept
        drop
    }

    chain web_chain {
        # Web traffic rules
        ct state new accept
    }
}

Honestly, verdict maps เป็นหนึ่งในเหตุผลหลักที่ผมชอบ nftables มากกว่า iptables เลยครับ มันทำให้ config อ่านง่ายและ maintain ง่ายขึ้นเยอะ

Concatenations สำหรับ Multi-field Matching

ถ้าคุณต้องการ match หลาย field พร้อมกัน (เช่น IP + port) concatenation เป็นคำตอบ:

# สร้าง set ที่ match ทั้ง IP + port พร้อมกัน
table inet firewall {
    set allowed_services {
        type ipv4_addr . inet_service
        elements = {
            10.0.1.0/24 . 22,
            10.0.2.0/24 . 3306,
            10.0.3.0/24 . 5432,
        }
    }

    chain input {
        type filter hook input priority filter; policy drop;
        ct state { established, related } accept
        ip saddr . tcp dport @allowed_services accept
    }
}

การ Persist Rules และ Automation

บันทึก Rules ถาวร

เขียน rules เสร็จแล้วอย่าลืม save นะครับ ไม่งั้นพอ reboot ทุกอย่างจะหายหมด:

# Export ruleset ปัจจุบันเป็นไฟล์ config
sudo nft list ruleset > /etc/nftables.conf

# nftables service จะ load ไฟล์นี้ตอน boot
sudo systemctl enable nftables

# ทดสอบว่า config ถูกต้อง (dry-run)
sudo nft -c -f /etc/nftables.conf && echo "Config OK" || echo "Config ERROR"

# Apply config ใหม่ (atomic)
sudo nft -f /etc/nftables.conf

จัดการด้วย Ansible

สำหรับองค์กรที่มีหลายเซิร์ฟเวอร์ การจัดการ nftables ด้วยมือคงไม่ไหว Ansible เป็นทางเลือกที่ดีมากครับ:

# playbook: nftables-hardening.yml
---
- name: Deploy nftables firewall rules
  hosts: webservers
  become: yes
  tasks:
    - name: Install nftables
      ansible.builtin.package:
        name: nftables
        state: present

    - name: Deploy nftables configuration
      ansible.builtin.template:
        src: templates/nftables.conf.j2
        dest: /etc/nftables.conf
        owner: root
        group: root
        mode: '0644'
        validate: 'nft -c -f %s'
      notify: Restart nftables

    - name: Ensure nftables is enabled and running
      ansible.builtin.systemd:
        name: nftables
        enabled: yes
        state: started

  handlers:
    - name: Restart nftables
      ansible.builtin.systemd:
        name: nftables
        state: restarted

สังเกตว่า validate step ใน template task จะตรวจสอบ syntax ก่อน deploy ให้อัตโนมัติ ถ้า config ผิดมันจะไม่ deploy ทำให้ปลอดภัย

การ Monitoring และ Debugging

เขียน rules เสร็จแล้วต้อง monitor ด้วยนะครับ ไม่ใช่ set แล้วลืม

ใช้ nftrace สำหรับ Debug

# เปิด tracing สำหรับ debug
sudo nft add rule inet firewall input meta nftrace set 1

# ดู trace output แบบ real-time
sudo nft monitor trace

# ปิด tracing เมื่อ debug เสร็จ (ลบ rule ที่เปิด trace)
sudo nft -a list chain inet firewall input
# หา handle number ของ trace rule แล้วลบ
sudo nft delete rule inet firewall input handle <number>

nftrace มีประโยชน์มากตอน troubleshoot ว่าทำไม traffic ถูก drop ครับ แต่อย่าลืมปิดหลังใช้งานเสร็จ เพราะมันสร้าง overhead พอสมควร

ดู Counters และ Statistics

# แสดง counters ของทุก rules
sudo nft list ruleset -c

# แสดง counter ของ set (ดูจำนวน IP ที่ถูก block)
sudo nft list set inet firewall blocklist

# Reset counters
sudo nft reset counters table inet firewall

ส่ง Logs ไปยัง Centralized Logging

การ log dropped packets สำคัญมากสำหรับ security monitoring ครับ ถ้าไม่ log ไว้ตอนมีเหตุก็จะไม่มีข้อมูลให้ตรวจสอบ:

# เพิ่ม log rule ด้วย prefix สำหรับ filtering
sudo nft add rule inet firewall input \
    limit rate 10/minute \
    log prefix "[nft-blocked] " level warn

# ตั้งค่า rsyslog ให้แยก log ไฟล์
# เพิ่มใน /etc/rsyslog.d/nftables.conf:
# :msg, contains, "[nft-blocked]" /var/log/nftables.log
# & stop

Checklist: Hardening ไฟร์วอลล์ Linux ด้วย nftables

ก่อนจบ ผมรวบรวม checklist ไว้ให้ครับ เอาไว้เช็คว่าไม่ลืมอะไรสำคัญ:

  1. Default policy เป็น drop — ไม่อนุญาตอะไรเลยจนกว่าจะระบุชัดเจน
  2. Drop invalid connectionsct state invalid drop
  3. อนุญาตเฉพาะ ports ที่จำเป็น — เปิดเฉพาะ service ที่ใช้จริง
  4. จำกัด SSH access — ใช้ IP whitelist หรือ rate limiting
  5. Rate limit ICMP — ป้องกัน ping flood
  6. Rate limit SYN packets — ป้องกัน SYN flood
  7. ใช้ dynamic sets สำหรับ auto-banning — block IP ที่โจมตีอัตโนมัติ
  8. Log dropped packets — สำหรับ forensics และ monitoring
  9. Persist rules — บันทึกใน /etc/nftables.conf
  10. Validate ก่อน apply — ใช้ nft -c -f ทุกครั้ง
  11. ทดสอบใน staging ก่อน — อย่า apply บน production โดยตรง
  12. มี recovery plan — เตรียมทาง console access ไว้กรณี lock out ตัวเอง

คำถามที่พบบ่อย (FAQ)

nftables กับ firewalld ต่างกันอย่างไร?

firewalld เป็น frontend (ตัวจัดการ) ส่วน nftables เป็น backend (กลไกที่ทำงานจริง) ครับ ใน RHEL/CentOS/Fedora ตั้งแต่เวอร์ชัน 9 ขึ้นไป firewalld ใช้ nftables เป็น backend อยู่แล้ว คุณสามารถเลือกใช้ firewalld เป็น interface ระดับสูงที่ง่ายกว่า หรือเขียน nftables rules โดยตรงเพื่อควบคุมทุกรายละเอียดเอง

ข้อควรระวังคืออย่าใช้ทั้งสองพร้อมกันเขียน rules ตรงๆ เพราะจะเกิด conflict ครับ

ย้ายจาก iptables ไป nftables จะทำให้ระบบล่มไหม?

ถ้าทำตามขั้นตอนที่ถูกต้องก็ไม่ครับ สิ่งสำคัญคือ: (1) สำรอง iptables rules เดิมก่อนเสมอ (2) ใช้ iptables-translate เพื่อแปลง rules อัตโนมัติ (3) ทดสอบด้วย nft -c -f ก่อน apply จริง (4) เตรียม console access ไว้กรณีฉุกเฉิน และ (5) ทดสอบใน staging environment ก่อนเสมอ

ที่สำคัญที่สุดคืออย่าใช้ iptables-legacy กับ nftables พร้อมกัน

nftables รองรับ Docker และ Kubernetes ไหม?

รองรับครับ แต่ต้องระวังนิดนึง Docker จะสร้าง iptables rules ของตัวเองสำหรับ networking ถ้าคุณใช้ nftables โดยตรง ต้องตรวจสอบว่า Docker ใช้ iptables-nft backend ซึ่งใน Docker เวอร์ชัน 20.10+ รองรับ nftables ผ่าน iptables-nft compatibility layer แล้ว

ส่วน Kubernetes ก็มีการ migrate จาก iptables ไป nftables ใน kube-proxy เช่นกัน ตั้งแต่ Kubernetes 1.29 ขึ้นไปมี nftables proxy mode เป็น beta แล้วครับ

nftables ทำงานร่วมกับ Fail2Ban ได้ไหม?

ได้เลยครับ Fail2Ban รองรับ nftables เป็น action backend ตั้งแต่เวอร์ชัน 0.11 ขึ้นไป สามารถตั้งค่าใน /etc/fail2ban/jail.local โดยใช้ banaction = nftables และ banaction_allports = nftables[type=allports] ได้เลย Fail2Ban จะจัดการ set ของ banned IPs ผ่าน nftables แทน iptables ให้อัตโนมัติ

จะดูว่ามี IP ไหนถูก block อยู่ได้อย่างไร?

ใช้คำสั่ง sudo nft list set inet firewall blocklist ครับ จะเห็น IP ทั้งหมดที่อยู่ใน blocklist set ถ้าใช้ dynamic set ที่มี timeout จะเห็นเวลา expire ของแต่ละ IP ด้วย สำหรับ monitoring ระยะยาว แนะนำให้ใช้ nft list ruleset -c เพื่อดู counters ว่า packets ถูก block ไปเท่าไหร่

เกี่ยวกับผู้เขียน Editorial Team

Our team of expert writers and editors.