Zum Hauptinhalt springen
Zurück zur Kategorie

iptables vs nftables: Vergleich und Migrationsanleitung

Umfassender Vergleich von iptables und nftables, nftables-Syntax, Migrationsanleitung, häufige Regeläquivalente und wann welches Firewall-Tool verwendet werden sollte.

Lesezeit: 13 min Sicherheit
iptablesnftablesfirewalllinuxnetzwerksicherheitpaketfilterungnetfilterfirewall-regeln

Inhaltsverzeichnis

iptables vs nftables: Vergleich und Migrationsanleitung

Jahrelang war iptables das Standard-Tool für Paketfilterung unter Linux. nftables ist jedoch seit 2014 im Linux-Kernel enthalten und ersetzt iptables in modernen Distributionen. Diese Anleitung vergleicht beide Tools, erklärt die nftables-Syntax und führt Sie durch die Migration.

Vergleichstabelle

Merkmaliptablesnftables
Erste Version19982014
SyntaxKomplex, inkonsistentKonsistent, lesbar
IPv4/IPv6Separate ToolsEinzelnes Tool
LeistungRegelweise AuswertungOptimiert
Atomare UpdatesNeinJa
Standard (Ubuntu 20.04+)NeinJa
Standard (Debian 10+)NeinJa

iptables Grundkonzepte

hljs bash
# Aktuelle iptables-Regeln anzeigen
sudo iptables -L -n -v
sudo iptables -L -n -v --line-numbers

# Grundlegende Regeln
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

# Regeln speichern
sudo iptables-save > /etc/iptables/rules.v4

nftables Grundkonzepte

In nftables ist die Struktur: Tabelle → Kette → Regel

hljs bash
# nftables installieren
sudo apt install nftables -y
sudo systemctl enable nftables
sudo systemctl start nftables

# Aktuelle Regeln anzeigen
sudo nft list ruleset

nftables Konfigurationsdatei

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

# Bestehende Regeln löschen
flush ruleset

# inet-Familie deckt IPv4 und IPv6 ab
table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        
        # Loopback-Schnittstelle erlauben
        iif lo accept
        
        # Bestehende Verbindungen erlauben
        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
        
        # Protokollieren und ablehnen
        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
# Konfiguration anwenden
sudo nft -f /etc/nftables.conf

# Syntax prüfen (ohne Anwenden)
sudo nft -c -f /etc/nftables.conf

Regeläquivalente

NAT-Regeln

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-Äquivalent
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
    }
}

Rate Limiting

# nftables Rate Limiting
chain input {
    tcp dport 22 limit rate 3/minute burst 5 packets accept
    tcp dport 22 drop
}

IP-Sets

# nftables Sets (eingebaut)
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
    }
}

Migration von iptables zu nftables

Automatische Konvertierung

hljs bash
# Konvertierungstool installieren
sudo apt install iptables-nftables-compat -y

# Bestehende iptables-Regeln konvertieren
iptables-save | iptables-restore-translate -f /etc/nftables.conf
ip6tables-save | ip6tables-restore-translate >> /etc/nftables.conf

Manuelle Migrationsschritte

hljs bash
# 1. Bestehende iptables-Regeln sichern
sudo iptables-save > /root/iptables-backup.txt

# 2. nftables-Konfiguration erstellen
sudo nano /etc/nftables.conf

# 3. Konfiguration testen
sudo nft -c -f /etc/nftables.conf

# 4. nftables aktivieren
sudo systemctl enable nftables
sudo nft -f /etc/nftables.conf

# 5. iptables deaktivieren
sudo systemctl disable iptables
sudo systemctl stop iptables

# 6. Regeln überprüfen
sudo nft list ruleset

Wann welches verwenden?

iptables verwenden:

  • Legacy-Systeme (CentOS 7, Ubuntu 18.04)
  • Bestehende iptables-Skripte vorhanden
  • Team ist mit iptables vertraut

nftables verwenden:

  • Neue Installationen (Ubuntu 20.04+, Debian 10+)
  • IPv4 und IPv6 gemeinsam verwalten
  • Sauberere Syntax gewünscht
  • Atomare Regelaktualisierungen erforderlich

Seit Ubuntu 20.04 und Debian 10 führt der Befehl iptables tatsächlich den iptables-nft-Wrapper aus — er verwendet also nftables im Hintergrund. Verwenden Sie iptables-legacy für echtes iptables.

Fazit

nftables ersetzt iptables in modernen Linux-Systemen. Es bietet konsistentere Syntax, einheitliche IPv4/IPv6-Verwaltung und bessere Leistung. Wählen Sie nftables für neue Installationen; planen Sie eine Migration für bestehende iptables-Infrastruktur.