← All 181 books nftables, one chain per page
One chain per page

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.


Steve Hodgkiss 7 lessons

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

Contents


Part 1 · The chain and its table
The input chain4
Part 2 · What a base chain needs
The priority5
Part 3 · Rules inside a chain
The position6
Part 4 · Classic chain shapes
The limited chain7
Part 5 · The chain's last word
The rehearsal8
Part 6 · The nat and the file
The file-loaded chain9
Part 7 · The long view
One honest hour10

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.

nftables companion · No. 01
the proof

The input chain

a real local run, quoted honestly

the proof: one chain, added here, listed back verbatimnft add chain inet demo181 input { type filter hook input priority 0; policy drop; }type filter hook input priority filter; policy drop;nft list table inet demo181created as a base chainpolicy drop: the letting-in rules come firstscope: inside the namespace nftdemo181,the host ruleset never touchedone line in, one base chain with a drop policy

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.

ONE HONEST CHAIN

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.

nftables companion · No. 02
the order

The priority

lower numbers run first

the ladder: lower numbers run firstraw-300ip, ip6, inet: all hooksmangle-150ip, ip6, inet: all hooksdstnat-100ip, ip6, inet: preroutingfilter0ip, ip6, inet, arp, netdev: all hookssecurity50ip, ip6, inet: all hookssrcnat100ip, ip6, inet: postroutingtype filter hook input priority filter; policy drop;nft -y list chain inet demo181b inputraw, mangle, dstnat, filter, security, srcnat: ascending order

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.

ONE HONEST CHAIN

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.

nftables companion · No. 03
the placement

The position

after the rule with that handle

position: after the rule with that handleright: the keyword before the ruleadd rule inet demo181c input position 3 tcp dport 443 acceptError: syntax error, unexpected position, expecting end of file or newline or semicolontcp dport 80 accept# new generation 6 by process 1972502 (nft)tcp dport 443 acceptthe keyword comes before the match; the rule lands after handle 3

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.

ONE HONEST CHAIN

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.

nftables companion · No. 04
the pace

The limited chain

ten a minute, five in hand

the limit: a token bucket at ten a minuteburst 5nft add rule inet demo181b input tcp dport 443 limit rate 10/minute accepttcp dport 443 limit rate 10/minute burst 5 packets acceptthe input never said bursttoken bucket filterburst 5 by defaultjitter tolerance10/minutea rate, not a ban:the port stays openten a minute, five in hand

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.

ONE HONEST CHAIN

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.

nftables companion · No. 05
the rehearsal

The rehearsal

check it without applying it

check: validate the command, apply nothingpassed, exit 0nft --check add rule inet demo181 input udp dport 53 acceptthe chain after: still four rulesfailed, exit 1nft --check add rule inet demo181 input bogus match acceptError: No symbol type informationnft -c add rule inet demo181c input udp dport 53 acceptthe short form, also exit 0a check that changes nothingis the right first steprehearse with the check flag before the chain that matters

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.

ONE HONEST CHAIN

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.

nftables companion · No. 06
the file

The file-loaded chain

a whole ruleset from stdin

the file: a whole ruleset, checked, then loadedflush rulesettable inet demo181f {chain input {type filter hook input priority 0; policy drop;iif "lo" acceptct state established,related acceptnft --check -f -checked first, exit 0nft -f -loaded, exit 0; one table lefta chain that matters is written down,checked, then loadedthe nft-equivalent of iptables-save and iptables-restore

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.

ONE HONEST CHAIN

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.

nftables companion · No. 07
the handoff

One honest hour

a practice plan, labelled as such

one honest hour: eight moves, planned and labelled1check bannernft --version2add namespaceip netns add3add a tableone family, one name4add input chainpolicy drop5letting-in rulesloopback, established6list with -athe handles7rehearsenft --check8flush, deleteip netns delthis book's plan: eight moves, one hour, every line traced to a runnamespaces:nftdemo181, nftdemo181b, nftdemo181cone hour of honest runs beats a year of guessing

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.

ONE HONEST CHAIN

Book the hour this week; which of the eight moves will surprise you most?

Index

Index


One honest hour10
The file-loaded chain9
The input chain4
The limited chain7
The position6
The priority5
The rehearsal8