انتقل إلى المحتوى الرئيسي
العودة إلى الفئة

iptables مقابل nftables: المقارنة ودليل الترحيل

مقارنة شاملة بين iptables و nftables، صياغة nftables، دليل الترحيل، مكافئات القواعد الشائعة ومتى تستخدم أي أداة جدار حماية.

وقت القراءة: 13 دقيقة الأمان
iptablesnftablesجدار حمايةlinuxأمان الشبكةتصفية الحزمnetfilterقواعد جدار الحماية

جدول المحتويات

iptables مقابل nftables: المقارنة ودليل الترحيل

لسنوات طويلة كان iptables الأداة المعيارية لتصفية الحزم في Linux. لكن nftables مُدرج في نواة Linux منذ 2014 ويحل محل iptables في التوزيعات الحديثة. سيقارن هذا الدليل الأداتين ويشرح صياغة nftables ويرشدك خلال الترحيل.

جدول المقارنة

الميزةiptablesnftables
الإصدار الأول19982014
الصياغةمعقدة، غير متسقةمتسقة، مقروءة
IPv4/IPv6أدوات منفصلةأداة واحدة
الأداءتقييم لكل قاعدةمحسّن
التحديثات الذريةلانعم
الافتراضي (Ubuntu 20.04+)لانعم
الافتراضي (Debian 10+)لانعم

المفاهيم الأساسية لـ iptables

hljs bash
# عرض قواعد iptables الحالية
sudo iptables -L -n -v
sudo iptables -L -n -v --line-numbers

# القواعد الأساسية
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -P INPUT DROP

# حفظ القواعد
sudo iptables-save > /etc/iptables/rules.v4

المفاهيم الأساسية لـ nftables

في nftables البنية هي: جدول → سلسلة → قاعدة

hljs bash
# تثبيت nftables
sudo apt install nftables -y
sudo systemctl enable nftables
sudo systemctl start nftables

# عرض القواعد الحالية
sudo nft list ruleset

ملف إعداد nftables

hljs bash
sudo nano /etc/nftables.conf
#!/usr/sbin/nft -f

# مسح القواعد الموجودة
flush ruleset

# عائلة inet تغطي IPv4 و IPv6
table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        
        # السماح بواجهة loopback
        iif lo accept
        
        # السماح بالاتصالات المنشأة
        ct state established,related accept
        
        # ICMP (ping)
        ip protocol icmp accept
        ip6 nexthdr icmpv6 accept
        
        # SSH
        tcp dport 22 accept
        
        # HTTP/HTTPS
        tcp dport { 80, 443 } accept
        
        # تسجيل ورفض
        log prefix "nftables-drop: " drop
    }
    
    chain forward {
        type filter hook forward priority 0; policy drop;
    }
    
    chain output {
        type filter hook output priority 0; policy accept;
    }
}
hljs bash
# تطبيق الإعداد
sudo nft -f /etc/nftables.conf

# فحص الصياغة (بدون تطبيق)
sudo nft -c -f /etc/nftables.conf

مكافئات القواعد

قواعد NAT

hljs bash
# iptables NAT
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.10:80
# مكافئ nftables NAT
table ip nat {
    chain prerouting {
        type nat hook prerouting priority -100;
        tcp dport 80 dnat to 192.168.1.10:80
        tcp dport 443 dnat to 192.168.1.10:443
    }
    
    chain postrouting {
        type nat hook postrouting priority 100;
        oif eth0 masquerade
    }
}

تحديد المعدل

# تحديد معدل nftables
chain input {
    tcp dport 22 limit rate 3/minute burst 5 packets accept
    tcp dport 22 drop
}

مجموعات IP

# مجموعات nftables (مدمجة)
table inet filter {
    set blocked_ips {
        type ipv4_addr
        elements = { 1.2.3.4, 5.6.7.8 }
    }
    
    chain input {
        type filter hook input priority 0; policy drop;
        ip saddr @blocked_ips drop
        tcp dport 22 accept
    }
}

الترحيل من iptables إلى nftables

التحويل التلقائي

hljs bash
# تثبيت أداة التحويل
sudo apt install iptables-nftables-compat -y

# تحويل قواعد iptables الموجودة
iptables-save | iptables-restore-translate -f /etc/nftables.conf
ip6tables-save | ip6tables-restore-translate >> /etc/nftables.conf

خطوات الترحيل اليدوي

hljs bash
# 1. نسخ احتياطي لقواعد iptables
sudo iptables-save > /root/iptables-backup.txt

# 2. إنشاء إعداد nftables
sudo nano /etc/nftables.conf

# 3. اختبار الإعداد
sudo nft -c -f /etc/nftables.conf

# 4. تفعيل nftables
sudo systemctl enable nftables
sudo nft -f /etc/nftables.conf

# 5. تعطيل iptables
sudo systemctl disable iptables
sudo systemctl stop iptables

# 6. التحقق من القواعد
sudo nft list ruleset

متى تستخدم أيًا منهما؟

استخدم iptables:

  • الأنظمة القديمة (CentOS 7, Ubuntu 18.04)
  • سكريبتات iptables موجودة
  • الفريق مألوف مع iptables

استخدم nftables:

  • التثبيتات الجديدة (Ubuntu 20.04+, Debian 10+)
  • إدارة IPv4 و IPv6 معًا
  • صياغة أنظف
  • تحديثات قواعد ذرية

منذ Ubuntu 20.04 و Debian 10، أمر iptables يشغّل فعليًا غلاف iptables-nft — أي يستخدم nftables في الخلفية. استخدم iptables-legacy لـ iptables الحقيقي.

الخلاصة

nftables يحل محل iptables في أنظمة Linux الحديثة. يوفر صياغة أكثر اتساقًا وإدارة موحدة لـ IPv4/IPv6 وأداءً أفضل. اختر nftables للتثبيتات الجديدة؛ خطّط للترحيل للبنية التحتية الحالية لـ iptables.