Essay
Learn to Read iptables, for the Love of Everything
What the ticket saysPort 8080 closed.
What the dashboard saysCompliant.
What iptables saysACCEPT tcp dpt:8080 from 0.0.0.0/0
The conversation goes like this. The firewall is blocking something. What do the rules say? Nobody knows. They know what the dashboard says, or what the ticket said, or what they meant when they typed it. The rules are right there. Nobody has looked.
You don't need to write iptables from memory. You need to be able to read it. That takes about ten minutes.
Print it properly
Don't use -L on its own. It does DNS lookups, hides the interfaces and leaves out the counters. Use this.
sudo iptables -L INPUT -n -v --line-numbersOr this, which prints the rules the way they were entered. It's easier to read once you know the flags.
sudo iptables -SRead it top to bottom
An example. Yours will be longer and worse.
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 120K 98M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 312 18720 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
3 41 2460 ACCEPT tcp -- * * 10.0.0.0/8 0.0.0.0/0 tcp dpt:22
4 9813 588K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
6 2204 132K DROP all -- * * 0.0.0.0/0 0.0.0.0/0The first line names the chain and its policy. The policy is what happens to a packet that matches nothing. Here it's DROP, which is correct. If it says ACCEPT, the chain allows everything it doesn't explicitly block.
Rules are checked in order. The first match wins and the packet stops there. Rule 6 drops everything, so anything appended after it does nothing. This is the most common firewall bug there is. Someone adds an allow rule with -A, it lands under the drop, and they spend an hour blaming the network.
The columns: pkts and bytes are how often the rule has matched. target is what happens. in and out are interfaces. source and destination are who. The last column is everything else, such as the port.
Rule 1 allows replies on connections that are already open. Rule 2 allows loopback. Rule 3 allows SSH, but only from 10.0.0.0/8. Rule 4 allows HTTPS from anywhere. Rule 5 allows port 8080 from anywhere and has never matched a packet, so someone opened it for something that no longer exists. Delete it.
The counters are the point
A rule with zero packets is dead or unreachable. A DROP rule with a large count is doing work, and you should know what it's dropping. Zero the counters with -Z, wait a day, read them again. That's an audit. It cost nothing.
What's hiding
That was one chain in one table. FORWARD handles traffic passing through the machine, which matters on anything that routes or runs containers. Port forwards live in a separate table.
sudo iptables -t nat -SDocker writes its own rules, and traffic to a published container port is diverted before your INPUT rules see it. Docker's documentation says so. A port you believe is closed can be open to the world because a container published it. Docker provides a DOCKER-USER chain for rules of your own. Find out now, not during the incident.
IPv6 has its own rules under ip6tables. If you locked down IPv4 and never looked at IPv6, you have half a firewall.
On a current distribution, iptables is probably a compatibility layer over nftables. The real ruleset is this.
sudo nft list rulesetufw and firewalld are front ends. They generate rules. When they disagree with what you expected, the generated rules are what's true.
Why this matters
Segmentation is on every list of basics, including mine. It exists only if the rules say so. A diagram is not a control. A ticket is not a control. The ruleset is.
An AI will explain a ruleset to you, and it's decent at it. You still need to be able to tell when it's wrong, on the one machine where being wrong matters.
Ten minutes. Go read your firewall.