Network & Security

nftables 2026: คู่มือ Modern Linux Firewall ทดแทน iptables สำหรับ SME ไทย

nftables คือ Modern Linux Firewall ที่จะเข้ามาทดแทน iptables อย่างเต็มตัวในปี 2026 — เร็วกว่า, syntax สั้นกว่า, จัดการ Rule ได้ใน Atomic Operation คู่มือนี้พร้อมตัวอย่าง Config สำหรับ SME ไทย

AF
ADS FIT Team
·8 นาที
Share:
🌐

# nftables 2026: คู่มือ Modern Linux Firewall ทดแทน iptables สำหรับ SME ไทย

หากคุณยังใช้ iptables อยู่ในปี 2026 ขอบอกว่ามาถึงยุคที่ควรเปลี่ยนแล้ว — Linux Distribution ส่วนใหญ่ (Debian 12+, RHEL 9+, Ubuntu 22.04+) ได้ย้ายมาใช้ nftables เป็น Default Backend แล้ว ส่วน iptables-nft เป็นแค่ Compatibility Shim ที่แปลคำสั่งเก่ากลับไปยัง nftables เบื้องหลัง การเรียนรู้ syntax ใหม่จึงเป็นการลงทุนที่คุ้มสำหรับ Sysadmin และ DevOps SME ไทยทุกคน

nftables ถูกพัฒนาโดยทีม Netfilter ทีมเดียวกับที่สร้าง iptables มันถูกออกแบบใหม่ทั้งหมด แก้ปัญหาที่สะสมมา 20 ปี: Performance ดีขึ้น, Syntax ที่อ่านง่าย, รวม IPv4/IPv6 ในตารางเดียว, และการ Apply Rule แบบ Atomic (ไม่มีช่วงเวลาที่ Firewall เปิดโหว่) บทความนี้จะสรุปข้อแตกต่างกับ iptables, สอน Syntax พื้นฐาน, ตัวอย่าง Config สำหรับ Web Server, การ Migrate จาก iptables, และข้อควรระวังสำหรับ SME ไทย

ทำไมต้องย้ายจาก iptables มา nftables

iptables มีปัญหาทางเทคนิคหลายอย่างที่ nftables แก้ไขได้ดีกว่า:

| ประเด็น | iptables | nftables |

|---------|----------|----------|

| Performance | ทุก Rule ผ่านการประมวลผลเป็น Linear List | ใช้ Pseudo-Bytecode VM, Set Lookup O(1) |

| Atomic Rule | ไม่มี — ระหว่าง Reload มี Window ที่ Firewall ว่าง | มี — Apply ทั้งชุดในธุรกรรมเดียว |

| IPv4/IPv6 | ต้องมี iptables + ip6tables แยกกัน | รวมใน Table เดียวด้วย `inet` family |

| Syntax | คำสั่งยาว ต้องระบุ -A -j -p ทุกครั้ง | สั้นกว่า มี Variable, Set, Map |

| Logging | จำกัด ต้องใช้ ulogd | Native, ใส่ Prefix และ Group ได้ |

| Counter | ทุก Rule นับ เปลือง Memory | Optional, ต้องระบุ keyword `counter` |

ในการทดสอบจริง nftables จัดการ Rule 1 ล้านบรรทัดได้เร็วกว่า iptables ถึง 10 เท่า และใช้ Memory น้อยกว่าครึ่งหนึ่ง — สำคัญมากสำหรับ SME ที่ Run บน VPS หรือ Cloud Instance ขนาดเล็ก

โครงสร้างพื้นฐานของ nftables

nftables มีลำดับชั้นแบบนี้: Table → Chain → Rule → Set/Map

  • **Table**: คอนเทนเนอร์หลัก ต้องระบุ Family (`ip`, `ip6`, `inet`, `arp`, `bridge`, `netdev`) แนะนำใช้ `inet` เพื่อรวม IPv4/IPv6
  • **Chain**: บรรจุ Rule แยกตาม Hook Point (`input`, `output`, `forward`, `prerouting`, `postrouting`)
  • **Rule**: บรรทัดคำสั่งจริง บอก Match + Action
  • **Set/Map**: โครงสร้างข้อมูลใหม่ ใช้เก็บ IP List, Port List แบบ Hash Table
  • ตัวอย่างไฟล์ Config พื้นฐาน `/etc/nftables.conf`:

    ```

    #!/usr/sbin/nft -f

    flush ruleset

    table inet filter {

    chain input {

    type filter hook input priority 0; policy drop;

    ct state established,related accept

    iif lo accept

    ip protocol icmp accept

    tcp dport { 22, 80, 443 } accept

    log prefix "nft-drop: " counter drop

    }

    chain forward {

    type filter hook forward priority 0; policy drop;

    }

    chain output {

    type filter hook output priority 0; policy accept;

    }

    }

    ```

    แค่ไฟล์เดียวรวม Policy ของทุกอย่าง — เปรียบเทียบกับ iptables ที่ต้องเขียนหลายสิบบรรทัดเพื่อให้ทำงานเหมือนกัน

    ขั้นตอน Setup nftables บน Ubuntu/Debian Server

    Step 1: ติดตั้งและเปิดใช้งาน

    ```bash

    sudo apt update

    sudo apt install nftables -y

    sudo systemctl enable --now nftables

    ```

    Step 2: ตรวจสอบ Default Rule

    ```bash

    sudo nft list ruleset

    ```

    Step 3: เขียน Config สำหรับ Web Server

    แก้ไข `/etc/nftables.conf` ให้รองรับ SSH, HTTP, HTTPS, HTTP/3 (UDP/443):

    ```

    #!/usr/sbin/nft -f

    flush ruleset

    define web_ports = { 80, 443 }

    define ssh_admin_ips = { 203.0.113.10, 198.51.100.5 }

    table inet filter {

    chain input {

    type filter hook input priority 0; policy drop;

    ct state invalid drop

    ct state established,related accept

    iif lo accept

    ip protocol icmp limit rate 5/second accept

    ip6 nexthdr icmpv6 accept

    ip saddr $ssh_admin_ips tcp dport 22 accept

    tcp dport $web_ports accept

    udp dport 443 accept

    log prefix "nft-drop: " counter drop

    }

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

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

    }

    ```

    Step 4: ทดสอบและ Apply

    ```bash

    sudo nft -c -f /etc/nftables.conf # Syntax Check

    sudo systemctl reload nftables # Apply

    sudo nft list ruleset # Verify

    ```

    ถ้าทำงานบน Server ที่ Production แนะนำให้ตั้ง Cron Job สำรอง: ก่อน Apply Rule ใหม่ให้ Schedule `flush ruleset` ใน 5 นาที — ถ้า Connection หลุดและคุณ Login กลับเข้าไปไม่ได้ Server จะรีเซ็ตตัวเองอัตโนมัติ

    การ Migrate จาก iptables มา nftables

    มี 2 วิธีหลัก:

  • **Translate ทันที**: ใช้ `iptables-translate` เพื่อแปลงคำสั่ง iptables เก่าเป็น nftables ทีละบรรทัด เหมาะสำหรับ Rule ไม่กี่สิบบรรทัด
  • **Save & Restore**: Dump iptables ปัจจุบันแล้วใช้ `iptables-restore-translate -f rules.v4 > nftables.conf`
  • ตัวอย่างการแปลง:

    ```bash

    iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT

    # Output: nft 'add rule ip filter INPUT tcp dport 22 counter accept'

    ```

    หลัง Translate ควร Review โดยเฉพาะ Rule ที่ใช้ Module เก่า เช่น `recent`, `string`, หรือ `xt_*` modules บางตัวอาจไม่ถูก Map สมบูรณ์

    ข้อดีของ Set และ Map

    ฟีเจอร์ใหม่ที่เปลี่ยนเกมคือ Named Set:

    ```

    table inet filter {

    set blacklist {

    type ipv4_addr

    flags timeout

    elements = {

    192.0.2.10 timeout 1h,

    198.51.100.50 timeout 24h

    }

    }

    chain input {

    type filter hook input priority 0; policy drop;

    ip saddr @blacklist drop

    }

    }

    ```

    Set รองรับ Auto-Timeout — IP จะถูกเอาออกอัตโนมัติเมื่อหมดเวลา ใช้คู่กับ Fail2Ban หรือ Crowdsec เพื่อ Block IP โจมตีแบบ Dynamic ได้สบาย

    Map ใช้สำหรับ DNAT/SNAT แบบหลายเงื่อนไข — เช่น Forward Port ตาม Destination Network

    ข้อควรระวังและ Best Practices

  • **Lock-out Risk**: ถ้าตั้ง Default Drop แล้วลืมเปิด Port 22 จะหลุดจาก Server ทันที — ควร Test ผ่าน Console (เช่น VNC ของ Cloud Provider) ก่อน
  • **IPv6 Default**: เซิร์ฟเวอร์ส่วนใหญ่ในไทยปี 2026 มี IPv6 พร้อมใช้แล้ว ใช้ `inet` Family เพื่อ Handle ทั้ง IPv4 และ IPv6 ในชุด Rule เดียว
  • **Logging Rate Limit**: ตั้ง `limit rate 10/second` หน้า log statement เพื่อไม่ให้ disk เต็มจาก SYN Flood
  • **Backup ก่อน Apply**: `sudo nft list ruleset > /root/nft-backup-$(date +%F).nft` ก่อนแก้ไขทุกครั้ง
  • **Use Variables**: ใส่ IP/Port เป็น `define` เพื่อแก้จุดเดียวเปลี่ยนทั้งไฟล์
  • **Combine with fail2ban**: fail2ban 1.0+ รองรับ nftables backend ใช้คู่กันได้ทันที
  • สรุปและขั้นตอนถัดไป

    nftables เป็นอนาคตของ Linux Firewall ที่ SME ไทยควรเริ่มใช้ตั้งแต่วันนี้ — Performance ดีขึ้น, Syntax สั้น, Atomic Rule ปลอดภัย, รวม IPv4/IPv6 ในที่เดียว

    Key Takeaways:

  • nftables คือ Default Firewall บน Linux Modern แทน iptables
  • ใช้ `inet` Family เพื่อจัดการ IPv4/IPv6 พร้อมกัน
  • Atomic Apply ลดความเสี่ยง Lock-out
  • Set/Map รองรับ Dynamic Block และ Timeout
  • หากต้องการความช่วยเหลือในการ Migrate Firewall, ออกแบบ Network Security, หรือวาง Hardening Plan สำหรับ Production Server — [ติดต่อทีม ADS FIT](/#contact) หรืออ่านบทความที่เกี่ยวข้องในหมวด [Network & Security](/blog) ของเรา

    Tags

    #nftables#iptables#Linux Firewall#Network Security#Self-hosted#SME

    สนใจโซลูชันนี้?

    ปรึกษาทีม ADS FIT ฟรี เราพร้อมออกแบบระบบที่ฟิตกับธุรกิจของคุณ

    ติดต่อเรา →

    บทความที่เกี่ยวข้อง