poradniki
iptables vs nftables – praktyczne porównanie i migracja krok po kroku
Różnice: iptables vs nftables
| Aspekt | iptables | nftables | Uwagi |
|---|---|---|---|
| Powstanie / status | 1998; utrzymanie (legacy) | ~2014→; aktywnie rozwijany | Nowe funkcje trafiają do nftables |
| Narzędzia CLI | Wiele binarek: iptables, ip6tables, arptables, ebtables | Jedno narzędzie: nft | Mniej złożoności |
| Backend jądra | x_tables (legacy) | nf_tables (VM w jądrze) | Nowoczesna architektura |
| Obsługa protokołów | Osobne narzędzia dla IPv4/IPv6/ARP/bridge | Jednolite w jednym narzędziu | Mniej duplikacji reguł |
| Rodziny / tabele | filter, nat, mangle, raw | ip, ip6, inet, arp, bridge | inet łączy IPv4 i IPv6 |
| Składnia i model | Imperatywne komendy | Deklaratywny DSL | Krótsze reguły |
| Zbiory i mapy | Wymaga ipset | Wbudowane sets, maps | Efektywne listy IP |
| Wydajność | Sekwencyjne przetwarzanie | Bytecode w jądrze | Lepsza skala |
| Migracja | Źródło | Cel | Narzędzia iptables-translate |
Jak sprawdzić backend systemu
iptables -V
# Jeśli zobaczysz "(nf_tables)", iptables korzysta z backendu nftables.
lsmod | grep nf_tables
sudo nft list ruleset
Instalacja i uruchomienie nftables
# Debian/Ubuntu
sudo apt update && sudo apt install -y nftables
sudo systemctl enable --now nftables
# RHEL/CentOS/Alma/Rocky
sudo dnf install -y nftables
sudo systemctl enable --now nftables
Podstawy składni nftables
sudo nft add table inet filter
sudo nft add chain inet filter input { type filter hook input priority 0 \; }
sudo nft add rule inet filter input ct state established,related accept
sudo nft add rule inet filter input tcp dport {22,80,443} accept
sudo nft add rule inet filter input drop
Migracja reguł iptables → nftables
Pojedyncza reguła
# iptables
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# tłumaczenie
iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
# wynik (nftables)
nft add rule ip filter INPUT tcp dport 22 counter accept
Cały ruleset
iptables-save > iptables.rules
iptables-restore-translate < iptables.rules > nftables.rules
sudo nft -f nftables.rules
sudo nft list ruleset
Eksport i import
sudo nft list ruleset > /etc/nftables/ruleset.conf
sudo systemctl restart nftables
Praktyczny plan migracji
- Utwórz kopię zapasową reguł iptables:
iptables-save > /root/backup.rules - Uruchom usługę nftables.
- Przetłumacz reguły:
iptables-restore-translate < /root/backup.rules > /root/nftables.rules - Załaduj i sprawdź:
sudo nft -f /root/nftables.rules sudo nft list ruleset
Korzyści po migracji
- Jedno narzędzie nft zamiast wielu binarek.
- Spójna, deklaratywna składnia.
- Lepsza wydajność i skalowalność.
- Łatwiejsza automatyzacja w Ansible/Puppet.
