nftables, one chain per page
Twenty-eight lessons on what nftables is: a subsystem of the Linux kernel providing filtering and classification of network packets, configured via the user-space utility nft and replacing the legacy iptables component of Netfilter; the law of one chain per page; the table, the first input chain with a drop policy; the hook, the type, the priority and the policy a base chain needs; add and insert, the handle, replace and delete, the position; the loopback chain, established and related, the trusted set, the limited chain; the counter, the verdict strip, the syntax error, the rehearsal with --check; the nat chain, the file-loaded chain, the flushed chain, the families; the iptables neighbor, the firewalld and ufw front-ends; McHardy 2008, March 2009, Neira Ayuso 2012, 16 October 2013, Linux 3.13 on 19 January 2014, one honest hour.
One chain, one page, one honest run. That is the shape of this book.
nftables, one chain per page
Twenty-eight lessons on what nftables is: a subsystem of the Linux kernel providing filtering and classification of network packets, configured via the user-space utility nft and replacing the legacy iptables component of Netfilter; the law of one chain per page; the table, the first input chain with a drop policy; the hook, the type, the priority and the policy a base chain needs; add and insert, the handle, replace and delete, the position; the loopback chain, established and related, the trusted set, the limited chain; the counter, the verdict strip, the syntax error, the rehearsal with --check; the nat chain, the file-loaded chain, the flushed chain, the families; the iptables neighbor, the firewalld and ufw front-ends; McHardy 2008, March 2009, Neira Ayuso 2012, 16 October 2013, Linux 3.13 on 19 January 2014, one honest hour.
Set in Space Grotesk, Inter and JetBrains Mono (SIL Open Font License).
An honest book about its demos: nft v1.0.9 (nftables v1.0.9, Old Doc Yak #3) ran on the machine that built this book. Every ruleset command ran INSIDE scratch network namespaces (nftdemo181, nftdemo181b and nftdemo181c), created first with ip netns add and deleted last with ip netns del; the host ruleset was never listed or modified. Every quoted output line in these pages (the version banner, every listing, every error line) was captured from those real runs, quoted verbatim from the captured run logs. No nft output was invented. Every other fact is as the primary source states it, read during this build: the nft(8) man page rendered on the build machine and the Wikipedia articles Nftables, Netfilter, iptables, Firewalld and Uncomplicated Firewall, cached verbatim beside this book. No facts come from Reddit, Stack Overflow, vendor blogs or commercial sites. Every lesson that describes a practice exercise carries the label 'this book's plan', never posing as a captured fact. Independent, not affiliated with the Netfilter project or the Wikimedia Foundation.
Your purchase is for personal use only. You do not have redistribution rights: please do not share, resell, or republish this book or its pages.
© 2026 Steve Hodgkiss. All rights reserved. Personal use only; no redistribution rights.
Edition 1.0 · stevehodgkiss.net
Contents
Per a real local run captured at dispatch, nft add chain inet demo181 input { type filter hook input priority 0; policy drop; } ran inside the scratch namespace nftdemo181, and the listing printed it back with the words type filter hook input priority filter; policy drop;. Per nft(8), for base chains, type, hook and priority parameters are mandatory.
The input chain
First the law: nftables v1.0.9 (Old Doc Yak #3) ran on this machine inside a scratch network namespace; no output was invented.
The first chain, as captured: nft add chain inet demo181 input { type filter hook input priority 0; policy drop; }, listed back as type filter hook input priority filter; policy drop;. Per nft(8), the chain is created as a base chain and hooked up to the networking stack.
One line of input, one base chain with a drop policy: the shape every later page hangs from.
With a drop policy in place, which rule must you add before you can reach the box again?
Per nft(8), the priority parameter accepts a signed integer value or a standard priority name which specifies the order in which chains with the same hook value are traversed; the ordering is ascending, lower values first, with the standard names raw -300, mangle -150, dstnat -100, filter 0, security 50 and srcnat 100. Per a real local run captured at dispatch, priority 0 was printed back as priority filter, and nft -y printed priority 0.
The priority
Per nft(8), The priority parameter accepts a signed integer value or a standard priority name, and The ordering is ascending, i.e. lower priority values have precedence over higher ones.
The standard names: raw -300, mangle -150, dstnat -100, filter 0, security 50, srcnat 100. A real local run confirmed the rounding: added with priority 0, listed as priority filter, and nft -y printed priority 0 again.
Same hook, two chains: the lower number is asked first.
Where does a chain that must log a packet sit relative to the filter chain?
Per a real local run captured at dispatch, the wrong form add rule inet demo181b input tcp dport 443 accept position 3 failed with Error: syntax error, unexpected position, expecting end of file or newline or semicolon, while the right form add rule inet demo181c input position 3 tcp dport 443 accept exited 0 and the listing showed tcp dport 443 accept after tcp dport 80 accept; per nft(8), the rule is appended unless a location is specified, in which case it is inserted after the specified rule.
The position
A real local run, both ways. Wrong: add rule inet demo181b input tcp dport 443 accept position 3, answered Error: syntax error, unexpected position, expecting end of file or newline or semicolon.
The right order, add rule inet demo181c input position 3 tcp dport 443 accept, exited 0. The listing showed tcp dport 443 accept after tcp dport 80 accept. Per nft(8), the rule then is inserted after the specified rule.
Position names the rule you land after; the keyword comes first.
Why put the position keyword before the rule rather than after the verdict?
Per a real local run captured at dispatch, nft add rule inet demo181b input tcp dport 443 limit rate 10/minute accept was listed back as tcp dport 443 limit rate 10/minute burst 5 packets accept; per nft(8), a limit statement matches at a limited rate using a token bucket filter, and with packet-based limit, the bucket holds exactly burst packets, by default five.
The limited chain
A real local run: nft add rule inet demo181b input tcp dport 443 limit rate 10/minute accept, listed back as tcp dport 443 limit rate 10/minute burst 5 packets accept. The input never said burst.
Per nft(8), A limit statement matches at a limited rate using a token bucket filter, and With packet-based limit, the bucket holds exactly burst packets, by default five; The burst value influences the bucket size, i.e. jitter tolerance.
A rate, not a ban: the port stays open, and a flood cannot use it.
What does a bucket of five let a client do that a strict rate would not?
Per a real local run captured at dispatch, nft --check add rule inet demo181 input udp dport 53 accept exited 0 and the listing of the chain after it was unchanged, still four rules, while nft --check add rule inet demo181 input bogus match accept exited 1 with Error: No symbol type information, and in the third namespace the short form nft -c add rule inet demo181c input udp dport 53 accept exited 0; per nft(8), -c, --check checks commands validity without actually applying the changes.
The rehearsal
A real local run: nft --check add rule inet demo181 input udp dport 53 accept exited 0, and nft list chain inet demo181 input right after showed the same four rules. The same check on the bogus rule exited 1.
Per nft(8), -c, --check Check commands validity without actually applying the changes. Per this book's plan, every change rehearses with --check before it touches a chain that matters.
A check that changes nothing is the right first step before any late edit.
What would you always rehearse with --check before running it on a chain that matters?
Per a real local run captured at dispatch, a file beginning with flush ruleset and holding table inet demo181f with one input chain was first checked with nft --check -f - and then loaded with nft -f -, and nft list ruleset after it showed only table inet demo181f with its three rules; per nft(8), by design, list ruleset command output may be used as input to nft -f, the nft-equivalent of iptables-save and iptables-restore.
The file-loaded chain
A real local run: nft --check -f -, then nft -f -, both exit 0. The file began with flush ruleset and held table inet demo181f { with one input chain.
After the load, nft list ruleset showed only table inet demo181f: the flush had cleared the three tables there before. Per nft(8), By design, list ruleset command output may be used as input to nft -f.
Per this book's plan, a chain that matters is written down, checked, then loaded.
Why is a chain you can reload from a file safer than one typed at a prompt?
Per this book's plan, one honest hour of practice is: check nft --version, create a scratch namespace with ip netns add, add one table and one input chain with a drop policy, add the loopback rule, the established rule and one port, list it with the handle flag, rehearse an edit with the check flag, flush, delete the table, and finish with ip netns del; every one of these commands ran on the build machine inside the namespaces nftdemo181, nftdemo181b and nftdemo181c, and none touched the host ruleset.
One honest hour
Per this book's plan, one honest hour: check the banner with nft --version, then ip netns add a namespace before anything else. Add one table and one input chain with a drop policy, then the letting-in rules.
List it with nft -a, rehearse an edit with nft --check, then nft flush chain, delete the table and finish with ip netns del. Eight moves, each first run here in a namespace.
One hour in a namespace: a wall you can build, read, edit and throw away.
Book the hour this week; which of the eight moves will surprise you most?