nftables

On this page 7

nftables is the Linux kernel’s packet classification framework and its user-space tool nft, used to define firewall filtering, network address translation and packet mangling rules. In Linux networking, it is the successor to iptables, ip6tables, arptables and ebtables, and it unifies them in one syntax and one kernel subsystem.

nftables has been part of the kernel since version 3.13 and is the default firewall backend on current Debian, Ubuntu, Fedora and Red Hat Enterprise Linux releases. Higher-level tools such as firewalld and UFW can generate nftables rules, so many systems use it even when administrators never type an nft command.

How nftables processes packets

nftables builds on the Netfilter hooks inside the kernel network stack. A packet passes hooks such as prerouting, input, forward, output and postrouting, and at each hook the kernel runs any base chain registered there. Each chain contains rules that are evaluated in order until a verdict is reached.

The key design change compared with iptables is a small virtual machine in the kernel. The nft tool compiles rules into bytecode, and the kernel executes that bytecode. New matches therefore do not require new kernel modules for every protocol field, which keeps the kernel side simpler and more generic.

Rules are organized in a clear hierarchy. The table explains each level of the nftables object model.

ObjectRoleExample
TableContainer tied to an address familytable inet filter
Base chainChain attached to a hook with a priority and policytype filter hook input priority 0; policy drop
Regular chainJump target for rule organizationchain ssh_rules
RuleMatch expressions plus a verdicttcp dport 22 accept
Set or mapNamed collection used in lookupsset blocked { type ipv4_addr; }
The nftables object hierarchy

Changes are applied as atomic transactions. Loading a whole rule file with nft -f replaces the ruleset in one step, so there is never a moment when half of the new rules are active, which was a known risk with sequential iptables commands.

Address families, chain types and hooks

Each table belongs to an address family, which decides what kind of traffic it sees. The inet family is the most practical choice for host firewalls because one table covers both IPv4 and IPv6.

FamilyTraffic coveredTypical use
ipIPv4 onlyLegacy-style IPv4 rules
ip6IPv6 onlyIPv6-specific policies
inetIPv4 and IPv6Host and gateway firewalls
arpARP framesARP filtering
bridgeFrames crossing a Linux bridgeFiltering between bridged ports
netdevIngress and egress on one deviceEarly filtering and DDoS mitigation
nftables address families

Base chains also have a type: filter for accepting or dropping packets, nat for address translation and route for rerouting locally generated traffic. The priority value orders chains on the same hook; lower numbers run first, and standard names such as filter (0) and dstnat (-100) make the order readable.

Sets, maps and stateful rules

Sets and maps are the feature that most improves on iptables. A set lookup uses hash or interval data structures, so matching one address against ten thousand entries costs roughly the same as matching against ten. In iptables, a list of ten thousand addresses meant ten thousand rules evaluated one after another.

Verdict maps go further by mapping a key directly to an action, for example sending each destination port to its own chain. Sets can have timeouts and dynamic updates, which allows rules such as adding an address that exceeds a connection rate to a temporary block list.

Stateful filtering relies on connection tracking. The common rule ct state established,related accept early in the input chain lets reply traffic through, while ct state invalid drop discards malformed packets. Counters, limits and quotas can be attached to rules to measure or rate-limit traffic.

nftables compared with iptables and UFW

nftables, iptables and UFW are often discussed together, but they work at different levels of the firewall stack. The table clarifies how they relate.

ToolLevelRule modelCurrent status
nftablesKernel framework and nft toolTables, chains, sets, bytecodeDefault backend
iptables-legacyKernel x_tables and toolFixed tables, linear rulesMaintained for compatibility
iptables-nftiptables syntax on nftablesTranslated into nftables objectsCommon transition layer
UFWFront endSimple allow and deny commandsGenerates rules for the backend
firewalldFront end with zonesZones and servicesUses nftables by default
nftables and neighboring firewall tools

The tool iptables-translate converts single iptables rules into nftables syntax, and iptables-restore-translate converts saved rule files. Mixing legacy iptables rules and native nftables rules on one host is possible but confusing, because both are evaluated and either can drop a packet.

Practical configuration and common mistakes

Most distributions load the persistent ruleset from /etc/nftables.conf through the nftables.service unit. A safe workflow is to edit the file, test it with nft -c -f /etc/nftables.conf, and only then apply it.

MistakeConsequencePrevention
Policy drop before allowing SSHRemote administrator locked outTest with a timed rollback or console access
No established,related ruleReplies to outgoing traffic blockedAccept established traffic first
Forgetting flush rulesetOld and new rules mergedBegin the file with flush ruleset
Separate ip and ip6 tablesIPv6 left unfilteredUse the inet family
Docker and nftables conflictsContainer traffic bypasses rulesReview rules created by the runtime
Frequent nftables mistakes

nft list ruleset shows the complete active configuration, and nft monitor streams rule changes and traced packets. Enabling meta nftrace set 1 on selected traffic is the most precise way to see which rule produced a verdict.

Explained without jargon

A computer connected to a network receives a constant stream of small data packages. nftables is the gatekeeper that inspects each package. It decides whether to let it in, send it elsewhere or throw it away.

The gatekeeper follows a written rule book. A good rule book is short and clear. nftables lets administrators keep long lists, such as thousands of blocked addresses, in one quick lookup table instead of thousands of separate rules.

A typical use case

A small hosting provider runs a public web server that must accept HTTP, HTTPS and SSH, while dropping everything else and blocking addresses that repeatedly attack the login service.

The administrator writes one inet table with an input chain whose policy is drop. It accepts established and related traffic, loopback traffic, ports 80 and 443, and SSH only from the office network. A dynamic set named attackers with a one-hour timeout collects addresses that open more than ten SSH connections per minute.

The file is checked with nft -c, applied atomically, and enabled through nftables.service. The result is one readable ruleset for IPv4 and IPv6, and the block list grows and shrinks automatically without manual cleanup.