Lesson 5 of 9 · 27m
nftables: Build a Firewall You Can Reason About
Tables, chains, sets, stateful rules, NAT, counters, and atomic changes — written as an operating model rather than a pile of inherited iptables commands.
nftables rewards operators who think in packet flow and punishes copy-paste archaeology. It is not 'iptables with new syntax'; it is a small virtual machine for packet classification with explicit hooks, priorities, state matching, and atomic replacement. Your job is to make policy legible six months later, not merely to make a port open today.
Start with structure
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
ct state invalid drop
iifname "lo" accept
ip protocol icmp accept
ip6 nexthdr icmpv6 accept
tcp dport 22 ip saddr 192.0.2.0/24 accept
}
chain forward { type filter hook forward priority filter; policy drop; }
}The default policy is part of the security boundary. If input is accept, every additional rule is decoration. Put exceptions in explicit rules and make unexpected traffic fall off the end.
Use sets for changing populations
set admin_nets { type ipv4_addr; flags interval; elements = { 192.0.2.0/24, 198.51.100.8/29 } }
tcp dport 22 ip saddr @admin_nets acceptSets keep operations separate from policy. Adding an administrator network is data maintenance, not firewall redesign. It also avoids the classic twenty near-identical SSH rules whose order nobody remembers.
Stateful inspection is the default posture
ct state established,related accept is the reason replies work without mirroring every service rule. If you remove it, your firewall becomes an exercise in manually reconstructing transport behavior. Inspect connection tracking and the live ruleset during incidents with narrowly scoped commands such as conntrack -L -p tcp --dport 443 and nft list ruleset. On high-connection hosts, full conntrack dumps are expensive; narrow them whenever possible.
NAT belongs in its own mental box
table ip nat { chain postrouting { type nat hook postrouting priority srcnat; policy accept; oifname "eth0" masquerade } }Masquerade rewrites outbound source addresses, usually for hosts behind dynamic external interfaces. NAT does not imply permission. Forwarding policy, routing, interface configuration, and net.ipv4.ip_forward are separate requirements. Operators lose hours by fixing only one of four.
Change the ruleset atomically and measure hits
Apply and inspect with nft -f /etc/nftables.conf followed by nft list ruleset. Prefer editing a complete declarative file to mutating individual rules interactively. Add counters to important boundaries:
tcp dport 22 ip saddr @admin_nets counter accept
counter drop comment "input default drop"Counters tell you whether packets are even reaching the chain. Zero hit counters are evidence, not proof; use them to form the next test.
Field notes
- Rule order matters within a chain, but chain priority determines where hooks process relative to other registered base chains.
inettables handle IPv4 and IPv6 together, but address-family-specific expressions still must match the right family.- Docker and Kubernetes install their own rules. Inspect the whole ruleset before claiming the host firewall is empty.
nft list rulesetis the truth. Distribution wrappers and firewall managers can disagree with it.- A permissive final
acceptcan neutralize every carefully written drop above it. Read chains from top to bottom.
Was this lesson helpful?