Firewalls on Linux: One Filter, Four Front Doors

The last post got the machine talking. This one decides what it is allowed to say, and to whom — because a server with a public address does not sit quietly waiting to be discovered. Put one on the internet and it will be port-scanned within minutes, by people who are not curious so much as systematic. A firewall is the difference between “only the things I chose are reachable” and “whatever happened to be listening”.

One filter, four front doors

Linux firewalling looks like a crowded field — iptables, nftables, ufw, firewalld — and newcomers reasonably assume these are four competing firewalls they must choose between. They are not. The kernel contains exactly one packet filter, called netfilter. Everything else in that list is a different way of talking to it.

NameWhat it actually is
netfilterThe packet filter, in the kernel. The only one. You never speak to it directly.
iptablesThe legacy userspace tool. On a modern distro it is usually a shim that quietly writes nftables rules.
nft (nftables)The modern replacement for iptables. Same kernel machinery, far saner syntax.
ufw“Uncomplicated Firewall” — Ubuntu’s friendly front-end. Writes rules for you.
firewalldThe Red Hat / Alma / Fedora front-end, organised around zones.

If that shape feels familiar, it should: it is exactly the netplan-versus-NetworkManager-versus-networkd situation from the last post, wearing a different hat. The skill is not learning all four — it is working out which one is in charge of this machine before you type anything, because rules written with a tool that is not driving will sit there having no effect at all.

How a packet gets judged

Netfilter inspects traffic at a few fixed points, and for a server you mostly care about three. INPUT is traffic addressed to this machine — the one that matters most. OUTPUT is what the machine sends. FORWARD is traffic passing through it, which only applies if the box is routing for someone else.

Within a chain, the rules are a list, and the list is read top to bottom until something matches. First match wins; nothing below it is consulted. If the packet reaches the bottom without matching anything, the chain’s default policy decides its fate — and on a sensibly configured machine that policy is DROP. That is the same implicit-deny-at-the-end you met at the bottom of every Cisco access list.

Which is easier watched than described:

How a packet is judged packet arrives Established or related? conntrack · the fast path yes 1 ALLOW tcp/22 match 2 ALLOW tcp/80,443 3 ALLOW icmp Default policy nothing matched · deny DROP ACCEPT in flight tcp/22 SSH — matches rule 1 tcp/3306 MySQL — matches nothing
Two packets, same rule list. The first matches rule 1 and leaves immediately; the second is tested against every rule, matches none, and falls off the end into the default policy.

The keystone: stateful filtering

Look again at the first box in that animation, because it is doing more work than it appears to. When your server fetches a package update, the reply has to get back in — but you emphatically do not want a rule saying “allow inbound traffic from port 443”, since that would let anyone in the world who sets their source port to 443 walk straight past you.

The answer is that netfilter remembers connections. A subsystem called conntrack keeps a table of conversations already in progress, and the very first rule on almost every real firewall says: if this packet belongs to an ESTABLISHED connection, or is RELATED to one, accept it and stop. Replies to things you started are let back in because they are recognised, not because a port was left open.

Where you have seen this before

A table of conversations in flight, consulted so that return traffic can be matched back to something you started — that is the same idea as the NAT translation table from the NAT post, doing a different job. One remembers conversations so it can rewrite them; the other remembers them so it can trust them. Both are why “allow nothing inbound” is a workable policy rather than an absurd one.

This is also why the fast path matters for performance: the overwhelming majority of packets on a busy server are part of existing connections, and they are accepted by rule one without ever being compared against the rest of the list.

ufw, in practice

On Ubuntu you will almost certainly use ufw, and the whole point of it is that a sane baseline is about four commands. Deny everything inbound, allow everything outbound, then open only what you actually need:

$ sudo ufw default deny incoming
$ sudo ufw default allow outgoing
$ sudo ufw limit 22/tcp          # SSH, rate-limited against brute force
$ sudo ufw allow 80,443/tcp      # the web server
$ sudo ufw enable
$ sudo ufw status verbose
TaskCommand
Open a portufw allow 8080/tcp
Open it only to one networkufw allow from 10.0.0.0/8 to any port 22 proto tcp
Rate-limit (6 attempts / 30s, then block)ufw limit 22/tcp
List with numbers, so you can deleteufw status numbered
Delete rule 3ufw delete 3
Turn it offufw disable

ufw limit is worth knowing about: it allows the port normally, but starts dropping connections from an address that opens more than six in thirty seconds. On a box with SSH exposed to the internet that quietly disposes of the background noise of credential-stuffing without you running anything extra.

Allow SSH before you enable, not after

You can see it coming by now. Run ufw default deny incoming followed by ufw enable on a box you are SSHed into, without having allowed port 22 first, and the connection dies mid-sentence — along with your route back in. Ufw does warn you, and the warning is easy to click past.

This is now the third time this exact shape has come up: Cisco has reload in 10, netplan has netplan try, and ufw has… a prompt and your own good sense. So build your own net — before you enable anything, schedule the undo: sudo systemd-run --on-active=10min ufw disable. If you are still connected in ten minutes, cancel it. If you are not, the box lets you back in without a drive.

Checking your work honestly

A firewall is one of the few things where “it looks right” is actively misleading, because the failure mode is silent and invisible from the inside. Three different questions, three different tools:

QuestionHow to answer it
What does the front-end believe?ufw status verbose
What is actually in the kernel?sudo nft list ruleset
What is even listening to be exposed?ss -tulpn
What does it look like from outside?nmap — from a different machine

That last row is the one people skip, and it is the only one that actually proves anything. Testing a firewall from the machine it is running on tells you nothing: loopback traffic never crosses it. Scan from somewhere else, or you are just reading your own homework back to yourself. ss -tulpn is the useful companion — a service bound only to 127.0.0.1 was never reachable in the first place, firewall or no firewall.

One thing to be aware of

If this machine runs containers, be warned that publishing a container port can write its own rules and sail straight past the front-end you just configured — so ufw status can say “deny” while the container is cheerfully reachable from the internet. It deserves a proper explanation rather than a footnote, and it needs a few container concepts first, so it is getting its own post in the containers series.

The one-sentence version

There is one packet filter in the kernel — netfilter — and iptables, nftables, ufw and firewalld are just different ways of writing to it, so your first job is finding out which one is driving; rules are read top to bottom until one matches, and whatever reaches the bottom meets the default policy, which should be DROP; a stateful first rule accepting ESTABLISHED,RELATED is what makes “deny all inbound” livable; and you have not tested a firewall until you have tested it from another machine. The commands are in the Firewall Cheatsheet.

That is the Linux series on its feet: a machine you can navigate, control, stock with software, keep running, put on a network, and now defend. Which raises the question of what you actually run on it — and the answer, increasingly, is not “software installed on the box” at all. Next: containers, and why a container is emphatically not a tiny virtual machine.