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:

Bash
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 filter is 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 drop on input: anything not explicitly allowed is refused. That's default deny, the only sane starting point.
  • ct state established,related accept lets replies to your own connections back in. Without it, your server can't even finish an apt update.
  • iif "lo" accept allows 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

Bash
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:

Bash
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:

Bash
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

Bash
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

  1. Decide who manages the firewall: ufw, firewalld or nftables. Pick one.
  2. Write the file, check it with nft -c, and load it behind a five-minute dead man's switch.
  3. Test SSH and your sites from outside, then cancel the switch.
  4. If Docker is involved, stop and read its docs before you flush anything.

The kidnapper has been apprehended. The ransom note is now a grocery list.