Skip to content

Migrating from iptables to nftables without losing your firewall

A staged migration from iptables to nftables on Debian and Ubuntu — converting rules, validating equivalence, and keeping rollback at arm's reach.

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

  1. 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.
  2. 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-translate is good but not perfect; it sometimes splits a single iptables rule into multiple nft statements that you can collapse using a set.
  3. Refactor the translated rules to use sets and named chains. Replace tcp dport 80 accept; tcp dport 443 accept; with tcp 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 } } then ip saddr @ssh_bastions tcp dport 22 accept.
  4. Stage the new ruleset in /etc/nftables.conf but do not enable it yet. Validate syntax with sudo nft -c -f /etc/nftables.conf. The -c flag check-runs without applying.
  5. 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.
  6. Apply atomically. sudo systemctl reload nftables (or sudo nft -f /etc/nftables.conf). nftables swaps the entire ruleset in one transaction — there is no window where a half-loaded policy is enforced.
  7. Cancel the rollback once you have re-verified SSH, your monitoring scrape, and one production traffic path. sudo atq then sudo atrm <jobid>.
  8. 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 legacy iptables-legacy packages after a soak period.

Common pitfalls

  • The compatibility shim writes to ip filter tables, while a hand-written ruleset typically uses inet filter (which covers both IPv4 and IPv6). Mixing the two means two parallel rule sets — pick one and stick with it.
  • iptables-restore-translate does not handle every match cleanly — recent, string, and exotic mark expressions often need manual rewrites.
  • nftables sets are typed. A set declared type ipv4_addr rejects an IPv6 element at load time; use type inet_proto . ipv4_addr for multi-key sets.
  • nft does not auto-persist rules. Without systemctl enable nftables and the ruleset in /etc/nftables.conf, a reboot returns to an empty policy.
  • The nftables package on Debian sources rules from /etc/nftables.conf only; on Ubuntu, the systemd unit reads /etc/nftables.conf too but Docker also writes its own tables — nft flush ruleset will 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.