Introduction
The migration from iptables to nftables fails most often at the second step: folks run iptables-translate on their rules, paste the output into /etc/nftables.conf, and then discover that stateful rules, NAT chains, and address sets do not translate cleanly. The syntax you get back from iptables-translate is correct but incomplete, and reloading it naively is how people lock themselves out: a flush ruleset; followed by the translated rules leaves the box with whatever policies the translator emitted and no rule that guarantees your SSH session survives. The translator does emit chain policies, but it cannot know which network path you are connected from. The reliable path on a production web server is to back up the current ruleset, translate every rule twice (once with iptables-translate and once by hand), check the file with nft -c before loading it, then reload it in a way that survives a dropped SSH session. This article walks through that process on a Debian 12 or Ubuntu 22.04 host with a typical LAMP or nginx setup.
The examples below assume a server that accepts SSH on 22, HTTP on 80, HTTPS on 443, and that runs a few upstream services that need NAT masquerade on the egress interface. Everything here is reproducible on any distro that ships nftables 0.9 or newer, which covers Debian 10+, Ubuntu 20.04+, RHEL 8+, and their derivatives.
What actually changes between iptables and nftables
The mental model is the biggest adjustment. iptables groups rules inside four fixed tables per protocol family (filter, nat, mangle, raw) and you invoke a separate command for each family (iptables, ip6tables, arptables, ebtables). nftables uses one userland binary (nft) and a single kernel subsystem. Tables are named by you, not the kernel, and their family is declared on creation: ip, ip6, inet (both v4 and v6), arp, bridge, or netdev. A chain is not implicitly tied to a hook any more. You declare the hook and priority yourself when you create a base chain, and you can have several base chains on the same hook with different priorities.
Match extensions become first-class expressions. What was -m multiport --dports 80,443 in iptables is tcp dport { 80, 443 } in nftables, where the braces are an inline set that the kernel stores as a hash lookup. What was -m state --state ESTABLISHED,RELATED becomes ct state established,related, and the conntrack expression can now be combined with less syntactic ceremony. Counters and logging are standalone statements, not jump targets, so -j LOG turns into log with optional prefix, and -j ACCEPT is replaced by the terminal statement accept.
Set membership, which required ipset and a separate kernel module in iptables, is part of the base language. You can declare a named set at table scope and reference it from multiple rules, which is what makes nftables practical for a ruleset with ten whitelisted ranges and two hundred blocked IPs.
Running iptables --version on a modern system usually prints something like iptables v1.8.7 (nf_tables). That means iptables is already a compatibility frontend over the nf_tables kernel API. If your output says (legacy) instead, you are on the old kernel subsystem, and the migration will actually change the packet path. Check with update-alternatives --display iptables to see which backend is active.
Preparing the server for nftables
Before touching anything, confirm that the kernel has the nf_tables stack and that the userspace tools are installed. On Debian and Ubuntu:
apt install nftablesnft --versionuname -rlsmod | grep nf_tablesA modern nf_tables module line means you are fine. If lsmod returns nothing and nft list ruleset errors out, load the module manually with modprobe nf_tables and check /etc/modules-load.d/nftables.conf does not need to be created. On RHEL 8 and later, the same packages come through dnf install nftables.
The kernel version matters for feature availability. nftables for Debian 10, Ubuntu 20.04+, and RHEL 8 ships versions that support the full core syntax. For the socket match and synproxy statement you need a kernel newer than the minimum distribution version. synproxy became available in Linux 5.3, socket transparent (tcp redirection) was enabled in nftables with kernel 4.18. If you see Operation not supported on a particular rule, it means the kernel does not implement that statement (no userspace workaround).
Also confirm the persistence unit is present. On Debian and Ubuntu the package installs nftables.service, which reads /etc/nftables.conf at boot. On RHEL, it is nftables.service too, but the config path is /etc/sysconfig/nftables.conf. Verify with systemctl cat nftables before relying on either path.
Backing up the existing ruleset
Save both formats. The iptables backup goes into a .rules file you can restore with iptables-restore, and the nft dump captures whatever nftables state already exists (it may be empty on a fresh iptables-legacy system).
iptables-save > /root/iptables-backup-$(date +%F).rulesip6tables-save > /root/ip6tables-backup-$(date +%F).rulesnft list ruleset > /root/nft-pre-migration-$(date +%F).nftCopy those three files off the host. If the migration goes sideways and SSH locks you out, you will want them on your laptop, not on the box you cannot reach. A quick scp /root/*backup-* admin@jumpbox:/backups/$(hostname)/ will do, and it takes ten seconds.
Also capture the live interface names and any policy-based routing that depends on the firewall:
ip -br addrip route show table all | head -50ss -tlnpThe ss output is your list of listening ports, which is the actual contract the new ruleset must honor. If a ruleset drops a port that something is listening on, you will notice within minutes; if it opens a port that nothing listens on, you will notice in the next audit.
Translating rules with iptables-translate
The translate tool is the fastest way to get a first draft. It parses the iptables command line and prints the nftables equivalent as a single nft add rule invocation. Run it against a representative rule before you batch-process your saved file, so you know the output format you are about to deal with.
iptables-translate -t filter -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPTThe output is:
nft add rule ip filter INPUT tcp dport 22 ct state new,established counter acceptNotice two things. First, the family is ip, not inet, because the original rule was an iptables rule (IPv4 only). If you had used ip6tables-translate, the family would be ip6. Second, iptables-translate inserts a counter statement even for rules that did not have one. This is the default behavior in iptables v1.8.x: counters do not affect packet processing but make rulesets far easier to audit. If you do not want them, remove the word counter in a wrapper script.
For a NAT rule, the output is more interesting:
iptables-translate -t nat -A POSTROUTING -o eth0 -j MASQUERADEPrints:
nft add rule ip nat postrouting oifname "eth0" counter masqueradeThe ip nat postrouting chain only exists if you have already created a table named nat in family ip with a postrouting chain of type nat and hook postrouting. The translate tool does not emit the chain creation command. This is the trap that catches most first-time migrations: you copy the rule, run it, and get Error: Could not process rule: No such file or directory.
For a multiport rule:
iptables-translate -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPTPrints:
nft add rule ip filter input tcp dport { 80, 443, 8080 } counter acceptThe braces are an anonymous set. If the ports change often, consider a named set instead, which I will cover below.
To batch-translate a whole saved ruleset, iptables-restore-translate is the tool. It reads the output of iptables-save on stdin and prints a full nftables script to stdout:
iptables-restore-translate -f /root/iptables-backup-2026-12-05.rules > /root/translated.nftThe result is a set of add table, add chain, and add rule lines, in the right order. The translated file preserves the iptables polices (e.g., policy accept or policy drop) and does not contain a flush ruleset at the top. Loading it via nft -f is additive, but only if you are starting with an empty or an existing ruleset that matches the imported policies. If your existing firewall already has rules you want to replace, wrap the translation in a message that first loads the exported tables, then replaces the rules with the new ones as part of a unified transaction. A safer approach is to build the final ruleset in a separate file, test it with nft -c, and then reload it. The iptables-restore-translate output gives you a solid starting point, but you still need to edit it into a proper /etc/nftables.conf with explicit chain policies and a top-level flush ruleset if you want a clean cut-over.
Manual translation patterns you will hit
Some rules translate cleanly but read awkwardly, and some translate into something you should restructure before it goes live. The table below covers the common cases with the exact syntax on both sides.
| iptables | nftables |
|---|---|
-p tcp --dport 80 | tcp dport 80 |
-p udp --dport 53 | udp dport 53 |
-m multiport --dports 80,443 | tcp dport { 80, 443 } |
-m state --state ESTABLISHED,RELATED | ct state established,related |
-m state --state INVALID | ct state invalid |
-m conntrack --ctstate NEW | ct state new |
-s 10.0.0.0/8 ! -d 10.0.0.5 | ip saddr 10.0.0.0/8 ip daddr != 10.0.0.5 |
-m limit --limit 3/min | limit rate 3/minute |
-m comment --comment "SSH" | comment "SSH" |
-j LOG --log-prefix "DROP:" | log prefix "DROP:" |
-j REJECT --reject-with tcp-reset | reject with tcp reset |
-j MASQUERADE | masquerade (in postrouting chain of type nat) |
-j DNAT --to-destination 10.0.0.5:8080 | dnat to 10.0.0.5:8080 |
-j SNAT --to-source 203.0.113.10 | snat to 203.0.113.10 |
Two of these deserve special attention because they change behavior if you get them wrong.
The ! negation operator moves from the flag position to the expression. In iptables, ! -s 10.0.0.0/8 negates the source. In nftables, ip saddr != 10.0.0.0/8 negates the source, but ip saddr 10.0.0.0/8 ip daddr != 10.0.0.5 negates only the destination. If you simply drop the ! and expect the same behavior, you will allow traffic you meant to block. Always rewrite negated rules by hand and test them with a single nft command before batching.
The REJECT --reject-with variants map to reject with and a different keyword. icmp-port-unreachable becomes icmp port unreachable, and tcp-reset becomes tcp reset in some contexts, but be aware that the exact translation depends on the rule family and protocol. If you write the iptables keyword in nftables (for example icmp-port-unreachable) the parser reports a syntax error.
Rate limiting is slightly different too. iptables -m limit --limit 3/minute --limit-burst 5 becomes limit rate 3/minute burst 5 packets. The default burst in nftables is 5 packets, so you can usually omit it, but be explicit if you are matching an existing policy.
Logging is a statement, not a target. You put log prefix "INPUT DROP: " immediately before the terminal action, and the log message includes the prefix verbatim. Be aware that iptables-translate drops the --log-level N option from a -j LOG rule, so if you relied on --log-level 4 (syslog warning) you must add level warn manually; nftables accepts the same syslog levels (emerg, alert, crit, err, warn, notice, info, debug) and defaults to info. (If you omit the level entirely, the default is info.)
A complete nftables ruleset for a web server
The translated rules are a starting point. The file that actually runs on the server should be a hand-written /etc/nftables.conf that consolidates the four iptables tables into a single inet table, so IPv4 and IPv6 share one ruleset. That is one of the practical wins of nftables: no more parallel iptables and ip6tables files that drift out of sync.
#!/usr/sbin/nft -f
flush ruleset
table inet filter { set admin_nets { type ipv4_addr flags interval elements = { 198.51.100.0/24, 203.0.113.42/32 } }
chain input { type filter hook input priority filter; policy drop;
iif lo accept ct state established,related accept ct state invalid drop
ip protocol icmp accept ip6 nexthdr icmpv6 accept
tcp dport 22 ip saddr @admin_nets accept tcp dport 22 ct state new limit rate 4/minute accept
tcp dport { 80, 443 } accept
counter log prefix "INPUT DROP: " level warn counter drop }
chain forward { type filter hook forward priority filter; policy drop; }
chain output { type filter hook output priority filter; policy accept; }}
table ip nat { chain postrouting { type nat hook postrouting priority srcnat; policy accept; oifname "eth0" masquerade }}Two details in that file matter more than the rest. The policy drop on the input chain means the ruleset is deny-by-default. That is the right posture for a web server, but it means a single missing accept rule takes the whole box off the network. The anti-lockout rule for SSH (the tcp dport 22 ip saddr @admin_nets accept line) has to be there before you load the file, or the SSH session you are using to load it will be your last.
The counter log before counter drop produces one log line per dropped packet. On a busy server, that can fill the disk. On a web server with a noisy scanner hitting it, it will. Either drop the log line or pair it with limit rate 10/minute on the log statement itself. For an initial migration, I would leave the log in for the first hour, then strip it once the ruleset has proven itself.
The set admin_nets is a named set with flags interval, which lets you use CIDR ranges rather than individual IPs. To add an admin IP later, you edit the file and reload, or you use nft add element inet filter admin_nets { 192.0.2.10 } on the live ruleset. The latter is useful during incident response; the former is what you should have in version control.
One thing to notice: the set is declared type ipv4_addr, so the SSH allow rule only matches IPv4 sources even though the chain lives in the inet family. If you administer the server over IPv6, declare a second set with type ipv6_addr and add a matching rule (tcp dport 22 ip6 saddr @admin_nets_v6 accept). The inet family does not make an IPv4-typed set apply to IPv6 traffic.
Validating before cutover
nft has a check mode that parses and evaluates the file without applying it. This is the single most important step in the migration and it takes half a second:
nft -c -f /etc/nftables.confAn empty output means the file parses and the kernel accepts the rule set. Any error printed here will fail identically when you load the file for real, so it is worth running until clean. Common failures are Operation not supported (kernel too old for a specific statement), No such file or directory (a chain or set referenced before it is defined), and syntax error (usually a missing ; at the end of a chain declaration).
Beyond syntax, validate the behavior. Open a second SSH session and keep it idle while you test. Then, in your first session, load the ruleset with nft -f /etc/nftables.conf and immediately verify three things from the second session:
ss -tlnpcurl -sS -o /dev/null -w "%{http_code}\n" http://127.0.0.1/nft list ruleset | head -40The ss output should show the expected listeners. The local curl exercises the loopback accept and confirms the web server is reachable. The nft list ruleset output should match the file you just loaded. If any of the three disagree, restore the backup for the backend you were actually running before doing anything else: nft flush ruleset followed by nft -f /root/nft-pre-migration-2026-12-05.nft on an nftables host, or iptables-restore < /root/iptables-backup-2026-12-05.rules on a legacy host.
To test that the drop policy actually drops, use a client outside the admin set:
hping3 -S -p 23 203.0.113.10 -c 3A 100% packet loss result is what you want. If the response is flags=SA, the telnet port is open and your policy is not in effect. That could mean the table family is wrong (you loaded into ip but the packets are hitting inet), the chain hook is wrong, or a conflicting ruleset is still loaded. The next section covers each.
Validation also means checking conntrack, since the whole ct state machinery depends on the conntrack module:
conntrack -L | headcat /proc/sys/net/netfilter/nf_conntrack_maxIf the conntrack tool (conntrack -L) fails (e.g., with EPERM or ENOENT), check whether the module is loaded with lsmod | grep nf_conntrack. Load it with modprobe nf_conntrack if needed, and add nf_conntrack to /etc/modules-load.d/nftables.conf to make it persistent.
Cutover and rollback
Loading a working ruleset is one thing. Making it survive a reboot is another. On Debian and Ubuntu, systemctl enable --now nftables wires /etc/nftables.conf into the boot sequence, but you should test the boot path before you trust it:
nft flush rulesetsystemctl start nftablesnft list ruleset | headThe flush ruleset here is a deliberate abort of the current live state. Do it from a KVM console or an out-of-band SSH session, not from a single connection you care about. If the reload fails, you will want the fallback path ready:
systemctl stop nftablesiptables-restore < /root/iptables-backup-2026-12-05.rulesOn hosts where iptables is still the legacy backend, both subsystems can be loaded at once and the kernel will consult both. That is why some migrations “work” without removing iptables rules, until they do not. To be certain only one is active:
update-alternatives --display iptables# expect: link points to nft backend (e.g. /usr/sbin/iptables-nft or /usr/sbin/iptables-legacy)iptables -S | headIf iptables -S prints anything and nft list ruleset also prints rules, you have two rule sources. On a modern system that is normal (iptables is a frontend and its rules are stored in nftables underneath), so the two views will match. On a legacy system, they will not, and you have to pick one. Disable the other’s service and remove the package if you want to commit to nftables:
systemctl disable --now netfilter-persistent iptables-persistentapt purge iptables-persistentDo not purge iptables itself on Debian; the nft backend lives in the same package.
Troubleshooting common migration failures
Error: Could not process rule: Operation not supported on a specific rule, while the rest of the file loads, means the kernel does not implement the statement. This is common with synproxy on kernels older than 5.3, and with socket transparent on kernels older than 4.18. Check the actual kernel with uname -r and either drop the rule or upgrade the kernel. There is no userspace workaround.
Error: Could not process rule: No such file or directory almost always means the table or chain you are adding a rule to has not been created in the current nft transaction. nftables applies a file atomically, so order matters: define tables, then chains, then rules. If you are pasting single rules into a shell, nft list tables will show what actually exists. A common cause is adding a rule with -t nat semantics to a table you named nat in family inet instead of family ip. In the inet family NAT is supported since kernel 5.2, but addresses must be prefixed with ip or ip6 (e.g. dnat ip to 10.0.2.2).
Error: syntax error, unexpected '}' on a chain block usually means a missing semicolon after policy drop (or policy accept). Chain declarations require ; between parameters. The error message points at the wrong line more often than not, so read the block as a whole.
Rules load but packets still pass. Three causes to check in order. First, is the chain hooked to the right priority? priority filter is 0, which is where the old filter table used to be. If you copied a rule from a tutorial that used priority 0 and a second chain with priority 0 also exists, both run, and the second one’s policy wins on a miss. Second, is the family correct? A rule in family ip does not see IPv6 traffic; if your server resolves to a v6 address, clients will bypass the v4 ruleset entirely. Third, is a legacy iptables ruleset still loaded? See the previous section.
NAT stops working after the switch. Check sysctl net.ipv4.ip_forward. nftables does not toggle it. Then check nft list chain ip nat postrouting; the chain type must be nat and the hook must be postrouting. If you see the rule but no traffic, the interface name in oifname "eth0" may not match the actual egress interface, which you can confirm with ip route get 8.8.8.8.
iptables-restore and nft show different rulesets after reboot. You likely have both netfilter-persistent and nftables.service enabled. systemctl disable --now netfilter-persistent will resolve it, and on Debian 12 you should also verify /etc/network/if-pre-up.d/iptables is not present.
Trade-offs worth knowing before you commit
nftables uses less CPU per packet on large rulesets. The kernel evaluates rules from pre-parsed expressions instead of invoking a separate match module for each rule, and this matters on a box with a thousand rules. For a web server with fifty rules, the difference is noise.
The atomic replace is a real operational win. nft -f /etc/nftables.conf swaps the whole ruleset in one transaction, so live traffic sees either the old rules or the new rules, never a partial set. iptables-restore is close to atomic but has edge cases with NAT state, and it depends on iptables-save being pasted back correctly.
The learning curve is steeper than iptables, and the error messages point at the wrong line often enough to slow debugging. Copy-pasting rules from documentation written for iptables is a common source of silent failures, especially around negation and NAT chain declarations.
Compatibility is the largest practical concern. Some older tooling (e.g., fail2ban before 1.0, certain Docker versions before 20.10, and a long tail of vendor scripts) assumes iptables commands will work. On a modern host, iptables-nft keeps those tools functional, and you can see their rules in nft list ruleset because iptables-nft stores its rules in the nftables kernel tables. The key practical concern is that legacy resolver hubs and container runtimes manage their own firewall state; you may need to disable their NAT injection or opt into nftables-specific APIs depending on the software version. The migration window is where operational surprise lives.
The cut-over itself should be reversible for at least a week. Keep the iptables backup, keep iptables-restore accessible on the host, and do not remove the iptables package until you have rebooted cleanly twice with nftables as the sole manager. On most Debian and Ubuntu hosts that is a two-hour window if everything goes right and a two-day window if the first reload drops SSH, which is why the console session in the cutover section is not optional.