Debian 12 and Ubuntu 22.04+ ship nftables as the default backend, with iptables-nft providing a compatibility shim. That shim lets old rules keep working, but it hides everything nftables does well — atomic updates, sets, maps, named chains, and a saner syntax. This article walks through a deliberate migration that ends with native nft rules in /etc/nftables.conf, verified equivalent to the iptables policy you started with.
How to verify
Check what backend is actually in use, and what the kernel sees:
sudo update-alternatives --display iptables
sudo nft list ruleset
sudo iptables-save | head -30
sudo iptables -V # look for "(nf_tables)" vs "(legacy)"
sudo lsmod | grep -E 'nf_tables|x_tables'
If iptables -V says (nf_tables), your existing rules are already being translated to nftables at runtime. That is the launchpad, not the destination.
What’s happening
iptables exposes a fixed table/chain structure (filter, nat, mangle) and matches packets one rule at a time. nftables replaces this with user-defined tables and chains, atomic ruleset updates, and first-class data structures (sets, maps, verdict maps) that let one rule match thousands of entries in O(log n). The iptables-nft shim translates legacy commands into nftables expressions and stores them in tables named ip filter, ip nat, etc. — but it does so verbosely, with one rule per legacy rule. A native rewrite typically halves the rule count and removes whole classes of bugs (rule-ordering races, non-atomic flushes mid-update).
The migration risk is real: a mistyped nft flush ruleset removes everything in one transaction, including SSH access. The standard mitigations are a serial console (or KVM), an at job that re-enables the old ruleset after 5 minutes, and a staging host that mirrors production.
The procedure
- Snapshot the current iptables ruleset and the running nftables view.
sudo iptables-save > /root/iptables.$(date +%F).rules && sudo ip6tables-save > /root/ip6tables.$(date +%F).rules && sudo nft list ruleset > /root/nft.$(date +%F).rules. Commit all three to your config repo. - Translate rules with the built-in converter.
sudo iptables-restore-translate -f /root/iptables.$(date +%F).rules > /root/translated.nft. Read the output —iptables-restore-translateis good but not perfect; it sometimes splits a single iptables rule into multiple nft statements that you can collapse using a set. - Refactor the translated rules to use sets and named chains. Replace
tcp dport 80 accept; tcp dport 443 accept;withtcp dport { 80, 443 } accept. Group SSH bastions into a named set:set ssh_bastions { type ipv4_addr; elements = { 203.0.113.10, 203.0.113.11 } }thenip saddr @ssh_bastions tcp dport 22 accept. - Stage the new ruleset in
/etc/nftables.confbut do not enable it yet. Validate syntax withsudo nft -c -f /etc/nftables.conf. The-cflag check-runs without applying. - Schedule a rollback before you reload.
echo "sudo nft -f /root/nft.$(date +%F).rules" | sudo at now + 5 minutes. If you lose SSH after the reload, the rollback fires automatically and you regain access. - Apply atomically.
sudo systemctl reload nftables(orsudo nft -f /etc/nftables.conf). nftables swaps the entire ruleset in one transaction — there is no window where a half-loaded policy is enforced. - Cancel the rollback once you have re-verified SSH, your monitoring scrape, and one production traffic path.
sudo atqthensudo atrm <jobid>. - Switch the alternatives to native nft tooling for any operator who still types
iptables.sudo update-alternatives --set iptables /usr/sbin/iptables-nft. Plan to remove the legacyiptables-legacypackages after a soak period.
Common pitfalls
- The compatibility shim writes to
ip filtertables, while a hand-written ruleset typically usesinet filter(which covers both IPv4 and IPv6). Mixing the two means two parallel rule sets — pick one and stick with it. iptables-restore-translatedoes not handle every match cleanly —recent,string, and exoticmarkexpressions often need manual rewrites.- nftables sets are typed. A set declared
type ipv4_addrrejects an IPv6 element at load time; usetype inet_proto . ipv4_addrfor multi-key sets. nftdoes not auto-persist rules. Withoutsystemctl enable nftablesand the ruleset in/etc/nftables.conf, a reboot returns to an empty policy.- The
nftablespackage on Debian sources rules from/etc/nftables.confonly; on Ubuntu, the systemd unit reads/etc/nftables.conftoo but Docker also writes its own tables —nft flush rulesetwill break container networking until Docker restarts.
In the engagements we run, firewall migrations are scheduled with the same rigour as a database failover — rollback timer, change window, post-change scrape of dependent services. That bench discipline lives in Managed Operations, and we apply it whether the migration is iptables-to-nftables or anything else that touches the kernel data path.