Skip to main content
iptables vs nftables: A Practical Linux Firewall Guide

iptables vs nftables: A Practical Linux Firewall Guide

KU
· Founder & Chief Editor
• 8 min read•Updated •

iptables still works, but nftables is what every modern distro ships by default now. Here is how the two actually differ and how to migrate a real ruleset.

Key takeaways

  • Use nftables for new Linux firewalls.
  • It handles IPv4 and IPv6 in one ruleset, loads files atomically, and matches against sets instead of long rule lists.
  • On current distros the iptables command usually runs as iptables-nft on the same nf_tables backend, so existing scripts keep working while you migrate with iptables-restore-translate.

Write new Linux firewall rules in nftables. It covers IPv4 and IPv6 in one inet table, loads a whole file atomically with nft -f, and uses sets instead of long linear rule lists. Keep iptables syntax only where existing scripts depend on it. On current distros, iptables is usually iptables-nft, a front end to the same nf_tables backend.

Verified against

nftables 1.1.7 (released 2026-09-01), against the netfilter.org nft(8) manual page and the nftables wiki pages on moving from iptables and on ruleset tracing. Checked October 2026.

Check which backend you actually have before reasoning about anything else:

Terminal
iptables -V
iptables v1.8.x (nf_tables)

(nf_tables) means you're on the compatibility layer: the old commands and scripts keep working, but the rules land in nf_tables underneath. (legacy) means the old x_tables backend. Understanding the real framework matters the moment you need something the compatibility shim doesn't cover, or you're debugging why a rule behaves differently than it did on an older box.

Why the kernel moved on

Classic iptables evaluates fixed tables (filter, nat, mangle, raw), each with its own built-in chains, and rules are matched top to bottom, so a ruleset with thousands of entries does more work per packet as it grows. nftables replaces this with a single in-kernel virtual machine: rules compile to bytecode, tables and chains are user-defined rather than fixed, and lookups can use actual data structures (sets, maps) instead of linear scans.

The practical differences that matter day to day:

iptablesnftables
Rule evaluationLinear scan per tableCompiled bytecode, can use sets/maps
Tables/chainsFixed tables (filter, nat, mangle, raw)User-defined, only what you declare
IPv4 + IPv6Separate tools (iptables, ip6tables)One syntax, one ruleset (inet)
Rule syntaxOne rule per -A commandMultiple rules in one file block
Atomic reloadsPer table via iptables-restoreWhole ruleset via nft -f
Default on current distrosCompatibility layer (iptables-nft)Native

Reading the same rule in both syntaxes

A rule allowing established SSH traffic and dropping new connections from anywhere else, in iptables:

Terminal
sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j DROP

The same intent in nftables, with no separate conntrack module to load:

Terminal
sudo nft add rule inet filter input tcp dport 22 ct state established,related accept
sudo nft add rule inet filter input tcp dport 22 drop

The inet family in nftables handles IPv4 and IPv6 in the same table, so there's no more maintaining parallel iptables and ip6tables rulesets that drift apart because someone forgot to update one of them. To see how any single iptables rule maps across, iptables-translate prints the nft equivalent without applying anything:

Terminal
iptables-translate -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT
nft add rule ip filter INPUT tcp dport 22 ct state new counter accept

A minimal nftables ruleset

nftables rulesets are usually written as a file and loaded atomically, not built up rule by rule on the command line:

/etc/nftables.conf
flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;

        ct state established,related accept
        ct state invalid drop
        iif "lo" accept

        tcp dport 22 accept
        tcp dport { 80, 443 } accept
        ip protocol icmp accept
        ip6 nexthdr icmpv6 accept

        # log then drop everything else, rate-limited so it can't fill the disk
        limit rate 5/minute log prefix "nft-drop: "
    }

    chain forward {
        type filter hook forward priority 0; policy drop;
    }

    chain output {
        type filter hook output priority 0; policy accept;
    }
}
Terminal
sudo nft -c -f /etc/nftables.conf   # -c: check only, apply nothing
sudo nft -f /etc/nftables.conf      # load atomically, no partial-rule window
sudo systemctl enable --now nftables

The flush ruleset line at the top makes the file the whole truth: loading it replaces everything in one transaction instead of appending. The -c flag is worth building into any deploy pipeline that touches firewall rules: the man page describes it as checking command validity "without actually applying the changes," so a typo can't leave the box half configured. Keep a console or out-of-band session open the first time you load a policy drop ruleset over SSH.

Migrating an existing iptables ruleset

Don't hand-translate a large ruleset. Use the conversion tool and review the output:

Terminal
sudo iptables-save > /tmp/current-rules.iptables
iptables-restore-translate -f /tmp/current-rules.iptables > /tmp/current-rules.nft
grep -n '^#' /tmp/current-rules.nft     # anything left as a comment did not translate
sudo nft -c -f /tmp/current-rules.nft

iptables-restore-translate ships with the iptables source and produces an nftables ruleset from an iptables-save dump, but always read the output before loading it. Two things it won't fix for you:

  • Rule order semantics. If your original ruleset relied on jump targets across tables in a specific order, a naively translated chain can produce a different effective policy. Walk through the translated file logically, not just syntactically.
  • Matches with no equivalent. A rule using a niche -m match module the translator can't express comes out as a commented line rather than an active rule. That is what the grep above is for; a missing comment-check is how a deny rule quietly disappears.

Also don't run both worlds at once. The nftables wiki warns against using the nft tools and the legacy iptables tools at the same time, because mixing the x_tables and nf_tables subsystems produces surprising results.

Verifying and debugging live

Terminal
sudo nft list ruleset                  # the full active config, in nft syntax
sudo nft list chain inet filter input  # just one chain
sudo nft monitor                       # ruleset change events: tables, chains, rules added or deleted

Plain nft monitor shows changes to the ruleset, which is useful for catching another tool (Docker, a firewall daemon) rewriting your rules behind you. To see which rules a packet actually hits, enable tracing for the traffic you care about and watch trace events:

Terminal
sudo nft add rule inet filter input tcp dport 22 meta nftrace set 1
sudo nft monitor trace

The man page says monitor trace prints only events for packets with nftrace enabled, so the first rule is required. Delete it when you're done; tracing everything on a busy host is noisy. For the counters view, add counter to the rules you care about and read them with nft list ruleset.

When to still reach for iptables syntax

If you're running the iptables-nft compatibility layer, your existing iptables/iptables-save/iptables-restore scripts, Ansible playbooks, and CI checks keep working unchanged; they're translated to nftables rules under the hood. There's no requirement to rewrite everything immediately. The case for migrating the syntax itself is when you're touching a ruleset anyway (new box, new deploy pipeline) and want atomic reloads, IPv4/IPv6 in one file, or a ruleset large enough that set-based lookups replace long chains of near-identical rules.

If you manage firewall rules across many hosts rather than one box's local iptables/nftables config, that's a different layer entirely; see zero-trust service-to-service auth with SPIFFE and SPIRE for identity-based access control instead of IP-based rules, and the broader Linux troubleshooting guide for the rest of the diagnostic toolkit this pairs with.

Questions people ask

Is iptables deprecated?

The iptables syntax isn't going away soon, but the legacy x_tables backend is the past. On current distros the iptables command is usually iptables-nft, which accepts the old syntax and writes nf_tables rules. Run iptables -V: (nf_tables) confirms the compatibility layer. New rulesets should be written in native nftables, and old scripts can keep running until you next touch them.

How do I convert iptables rules to nftables?

Dump the live rules with sudo iptables-save > rules.txt, convert them with iptables-restore-translate -f rules.txt > ruleset.nft, then grep the output for lines starting with #, which mark rules that didn't translate. Validate with sudo nft -c -f ruleset.nft before loading with sudo nft -f ruleset.nft. For a single rule, iptables-translate prints the nft equivalent without applying it.

Can I use iptables and nftables at the same time?

You can, but you shouldn't mix the nft tools with the legacy iptables tools. The nftables wiki warns that combining the x_tables and nf_tables subsystems causes unexpected results. Using iptables-nft alongside nft is safer because both write to nf_tables, but keep one owner per table so two tools never fight over the same rules.

The call we'd make

New box, new ruleset: write it in native nftables syntax from the start, since atomic reloads and one file for IPv4/IPv6 are worth it. Existing box with a working iptables ruleset and no active pain: leave the compatibility layer alone rather than migrating for its own sake. Either way, validate with nft -c before loading, and confirm with nft monitor trace, not guesswork, that a rule is doing what you think it's doing.

Sources

Explore topics:LinuxSecurity
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). iptables vs nftables: A Practical Linux Firewall Guide. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/iptables-vs-nftables-linux-firewall-guide

Markdown

[iptables vs nftables: A Practical Linux Firewall Guide](https://www.devopsness.com/blog/iptables-vs-nftables-linux-firewall-guide) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

607 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.