Packet filtering with nftables
Where netfilter hooks sit in the packet path, how nftables tables, chains and rules fit together, and a default-deny server ruleset built on conntrack.
Sockets and listening ports with ssRouting and the default gateway/proc, /sys and tuning with sysctl
Packet filtering with nftables
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Netfilter is the kernel framework that lets code inspect packets at fixed hooks: prerouting, input, forward, output and postrouting. nftables is the current way to attach rules to those hooks.
- A packet for this machine passes prerouting then input. A forwarded packet passes prerouting, forward, postrouting. A locally generated one passes output then postrouting.
- nftables has tables (a namespace with a family such as `inet`), chains (base chains attach to a hook with a type, priority and policy) and rules (matches plus a verdict).
- A default-deny input chain needs three things before any service rule: accept `established,related` through conntrack, drop `invalid`, and accept loopback.
- On current distributions the `iptables` command is usually `iptables-nft`, a compatibility layer that writes nftables rules. `nft list ruleset` shows everything, whichever tool wrote it.
- `nft -f` loads a whole file atomically and `nft -c -f` checks it without applying, which is how to change a remote firewall without locking yourself out.
The kernel decides
When a packet arrives, the kernel decides what happens to it long before any program sees it. It checks whether the packet is for this machine, whether it should be forwarded, or whether it should be dropped. A firewall on Linux is not a separate program sitting in the path. It is rules loaded into the kernel that take part in that decision.
The machinery is netfilter: a set of fixed points in the kernel’s network code, called hooks, where registered code can look at a packet and return a verdict. Think of checkpoints in an airport. Arriving passengers pass one desk, transit passengers another, departing ones a third, and each desk can wave someone through or turn them back.
The five hooks
The nftables wiki’s hook page lays out the paths:
incoming ──► prerouting ──► (routing decision) ──► input ──► local process
│
└──► forward ──► postrouting ──► out
local process ──► output ──► postrouting ──► out
- Traffic to this machine passes prerouting, then input.
- Traffic routed through it passes prerouting, forward, then postrouting. That only happens when forwarding is on, as in the routing lesson.
- Traffic from this machine passes output, then postrouting.
Several things can hook the same point, ordered by priority number, lower first. The wiki lists connection tracking at -200, destination NAT at -100, ordinary filtering at 0 and source NAT at 100. So by the time a filter chain runs, connection tracking has already labelled the packet.
Tables, chains, rules
nftables organises rules in three levels.
A table is a namespace with a family. The wiki lists ip, ip6, inet (both IPv4 and IPv6), arp, bridge and netdev. The nft man page says that if you leave the family out, ip is assumed, so write inet for a server and cover both protocols at once.
A chain holds rules. A base chain is attached to a hook and declares a type (filter, nat or route), a hook, a priority and a policy. The man page gives accept as the default policy, with drop the alternative. A regular chain has no hook and is only reached by a jump from another chain, which is how you keep a large ruleset readable.
A rule is a list of matches followed by a statement, most often a verdict: tcp dport 22 accept.
A default-deny server ruleset
This is my own minimal ruleset, built from the pieces the nftables wiki’s server example uses:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
ct state vmap { established : accept, related : accept, invalid : drop }
iifname "lo" accept
meta l4proto { icmp, ipv6-icmp } accept
tcp dport { 22, 80, 443 } accept
}
chain forward {
type filter hook forward priority filter; policy drop;
}
}
Read it top to bottom, as the kernel does. The policy is drop, so anything not accepted dies. The first rule uses conntrack, the kernel’s table of connections it has seen. established packets belong to a connection already allowed, and related ones are tied to one, such as an ICMP error about it. That one line is what lets replies to the server’s own outgoing connections back in. Without it apt update and DNS lookups break. invalid packets fit no known connection and are dropped. Loopback is accepted so local services can talk to each other. ICMP and ICMPv6 are accepted because IPv6 cannot work without neighbour discovery, and path-MTU discovery needs ICMP too. Then the services. There is no output chain, so outgoing traffic is accepted.
I did not load this, since there is no Linux machine here. The syntax follows the nft man page and the wiki’s server example.
Changing it without locking yourself out
The wiki’s atomic-replacement page says nft -f swaps the whole ruleset “in one atomic operation”, so there is never a moment with half the rules loaded. The nft man page describes -c as checking commands “without actually applying the changes”. So the safe loop is:
sudo nft -c -f /etc/nftables.conf # syntax and semantics check
sudo nft -f /etc/nftables.conf # apply atomically
sudo nft list ruleset # see what the kernel really has
On a remote server keep a second session open, or a console, until a new SSH login works. Debian’s nftables.service loads /etc/nftables.conf at boot. RHEL’s loads /etc/sysconfig/nftables.conf. Enable the service, or the rules vanish at reboot like everything else in this group.
nftables and iptables
iptables was the previous generation, with separate tools and kernel tables per family. Most distributions now ship iptables-nft, which accepts the old syntax and writes nftables rules underneath. The wiki notes that the old implementation survives as iptables-legacy. In practice, run nft list ruleset to see all rules in one place, translate old scripts rather than mixing the two, and expect Docker and other tools to add their own tables. The front ends that write these rules for you, ufw and firewalld, come in the firewall front ends lesson.
About the video
The embed is the first 16 minutes, up to the “Anonymous sets” chapter, of LinuxCloudHacks’s “Protecting Incoming Traffic with Nftables”, November 2023. Its chapters build an input filter from nothing: testing a port with netcat, creating tables and chains, a first rule, counters, matching connections by state, and named counters. The clip stops before sets and concatenations, which are useful later but not needed to understand a ruleset. It does not draw the hook diagram, which is why this lesson does. I judged it from its title, chapters and description. English was judged from the English auto-caption track. Facts were checked against the nftables wiki (hooks, families, atomic replacement, server ruleset) and the nft man page on netfilter.org, on 2026-11-28.
Where this fits
Rules decide what gets through. To see what actually crossed the wire, capture it: capturing traffic with tcpdump.
Check yourself
Your input chain has `policy drop` and accepts only TCP port 22. You can SSH in, but `apt update` on the server times out. Why?
The replies from the mirror arrive on the input hook and match no accept rule. Add `ct state established,related accept` so replies to connections the server opened are let back in.
On which hook would you filter traffic passing through a Linux router between two other networks?
The forward hook. Forwarded packets never pass input or output, which only see traffic to and from the router itself.
Why put rule changes in a file and load it with `nft -f` instead of typing `nft add rule` lines one by one over SSH?
The file is applied atomically, so there is never a half-built ruleset, and you can check it first with `nft -c -f`. A sequence of single commands can drop your own session halfway through.
What does the `inet` family give you that `ip` does not?
One table that sees both IPv4 and IPv6, so you write a rule once instead of keeping two parallel rulesets in step.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.