nftables From Zero: A Linux Firewall You Can Actually Read
iptables rules read like a ransom note made from magazine letters. An nftables firewall reads like a list. Build a default-deny ruleset in one file, test it with a five-minute dead man's switch, and avoid the Docker trap.
Silas Voss
4 min read
Have you ever read an iptables ruleset? Forty lines of -A INPUT -p tcp -m conntrack --ctstate that look like a ransom note made of letters cut from different magazines. [Cut to a detective holding up a printout. "The kidnapper wants... port 443... and a stateful match." His partner nods grimly. "Classic."]
An nftables firewall reads like a list. nftables has been the kernel's packet-filtering framework for years, and on modern Debian, Ubuntu and Fedora even the iptables command is often a translation layer on top of it. Today we write a small default-deny firewall from scratch, in one file, in a way that can't lock you out.
Before you start: who's in charge here?
Only one thing should manage your firewall. Check whether something already is:
sudo ufw status
sudo systemctl is-active firewalld
If ufw or firewalld is active, either keep using it (both are fine, honestly) or switch it off before you go on. Two firewall managers on one box is like two people driving one car. Someone ends up in a ditch.
The ruleset, in one file
Put this in /etc/nftables.conf:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
meta l4proto { icmp, ipv6-icmp } accept
tcp dport 22 accept
tcp dport { 80, 443 } accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
Read it top to bottom, and notice that you can:
table inet filteris one table covering both IPv4 and IPv6. In the iptables days you kept two separate rulesets and forgot to update one of them. Everyone did. Nobody talks about it.policy dropon input: anything not explicitly allowed is refused. That's default deny, the only sane starting point.ct state established,related acceptlets replies to your own connections back in. Without it, your server can't even finish anapt update.iif "lo" acceptallows loopback, because programs on the box talk to each other.- ICMP is allowed. Blocking all ping and ICMP breaks things like path MTU discovery, and IPv6 really doesn't work without ICMPv6. Don't let anyone tell you "stealth mode" is a security feature.
- Port 22 for SSH, and 80 and 443 for the web. The
{ }is a set: one rule, several ports.
Check it before you load it
sudo nft -c -f /etc/nftables.conf
-c means check only. It parses the file and reports mistakes without changing a thing.
The dead man's switch
Here's the move that separates professionals from people who drive to the data centre. Before loading a new firewall over SSH, schedule its undo:
sudo systemd-run --on-active=5m /usr/sbin/nft flush ruleset
sudo nft -f /etc/nftables.conf
In five minutes, systemd wipes the ruleset, and with no rules at all, everything is allowed again. If your new rules lock you out, wait five minutes and walk back in. If everything works, cancel the timer before it fires:
systemctl list-timers 'run-*'
sudo systemctl stop run-XXXX.timer
(Use the name list-timers shows you.) I've used this trick more times than I'll admit in writing.
Make it stick
sudo systemctl enable --now nftables
sudo nft list ruleset
The nftables service loads /etc/nftables.conf at boot. list ruleset shows what's actually loaded right now. Always trust that over the file.
The Docker trap
If the box runs Docker, read this twice. Docker writes its own firewall rules to make container networking and published ports work. The flush ruleset line at the top of our file wipes those too, and container networking stops until Docker is restarted. Also, ports published with -p can bypass rules you'd expect to apply, because that traffic goes through the forward path, not input.
On a Docker host, don't flush everything. Keep your rules in their own table, flush just that table, and read Docker's own documentation on packet filtering first. A firewall that surprises you is worse than one you understand.
Add things the readable way
To open a port later, edit the file and reload. Don't stack up one-off live commands that disappear at reboot. Some examples:
tcp dport 5432 ip saddr 10.0.0.0/24 accept
tcp dport 22 ip saddr { 203.0.113.10, 198.51.100.0/24 } accept
The first allows Postgres only from your private network. The second allows SSH only from addresses you name (and you'd delete the open tcp dport 22 accept line). Each one is a sentence, not a ransom note.
What to do today
The kidnapper has been apprehended. The ransom note is now a grocery list.
Security
More like this
Security
Who Did That? Tracing sudo and Logins on a Linux Server
The config changed overnight and nobody did it. Everyone says so. The logs disagree. How to read sudo logs and login records, record whole sudo sessions, watch key files with auditd, and why the logs shouldn't only live on the box.
Security
What fail2ban Actually Stops (and the Things It Was Never Going To)
fail2ban is the bouncer who throws out anyone who gets their name wrong three times. Useful, and not a security strategy. How to set up its SSH jail, check who it has banned, and know what it can't protect you from.
Security
How to Read a CVE Without Panicking: CVSS, KEV and EPSS Explained
A 9.8 CVE drops on a Friday and your group chat catches fire. Before you cancel the weekend, learn how to read a CVE: what the score measures, what it doesn't, and the two lists that tell you whether anyone is actually attacking it.