iptables Tables and Chains Explained
iptables sorts rules into tables, which say what a rule does, and chains, which say where in a packet's path the rule runs. Once you know which chain a packet passes through, you know where your rule belongs.
- Table = purpose:
filterallows or blocks,natrewrites addresses,mangleedits or marks packets,rawskips connection tracking,securityis for SELinux labels. - Chain = hook point:
PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING. - Most rules you write live in the
filtertable, usually inINPUT. That is the default when you leave out-t.
Packet flow diagram
Inside each box the tables are listed in the order they run. Three paths exist:
| Packet is | Path |
|---|---|
| Addressed to this server | PREROUTING → INPUT → local process |
| Passing through this server | PREROUTING → FORWARD → POSTROUTING |
| Sent by this server | OUTPUT → POSTROUTING |
After PREROUTING the kernel looks at the destination address and decides whether the packet is for this host or must be forwarded.
The five tables
| Table | Used for | Chains |
|---|---|---|
filter | Allow or block packets. Default table. | INPUT, FORWARD, OUTPUT |
nat | Rewrite source or destination address and port: port forwarding, masquerading. | PREROUTING, OUTPUT, POSTROUTING (INPUT on recent kernels) |
mangle | Change packet fields or set marks, for example TTL, TOS or MARK. | All five |
raw | Exempt packets from connection tracking. | PREROUTING, OUTPUT |
security | SELinux security labels. Rarely used outside SELinux setups. | INPUT, FORWARD, OUTPUT |
In what order do the tables run?
Inside any one chain the order is raw, mangle, nat, filter, security. A packet only visits the chains on its own path, so this is an order per chain, not one pipeline for everything.
This explains a common surprise with port forwarding. DNAT happens in PREROUTING, before the routing decision, so rules in the filter table's FORWARD chain already see the translated destination address and port. SNAT and masquerading happen later, in POSTROUTING.
How rules in a chain are matched
Rules are checked from top to bottom. A terminating target such as ACCEPT, DROP or REJECT ends the search. Non-terminating targets such as LOG and MARK do their job and the packet keeps going. If no rule decides, the chain's policy (ACCEPT or DROP) applies, which is why -I (insert at a position) and -A (append at the end) give different results. For a worked example see allowing a port from specific IPs.
Look at each table
sudo iptables -L -n -v --line-numbers sudo iptables -t nat -L -n -v sudo iptables -t mangle -L -n -v sudo iptables -t raw -L -n -v sudo iptables-save
Without -t you see the filter table. iptables-save prints every table in one listing, with each table starting at a line such as *filter or *nat and ending at COMMIT. That is the same format used to save rules permanently.
iptables and nftables
On most current distributions the iptables command talks to nftables underneath (iptables-nft). The tables and chains above behave the same way. To see which backend you have, see iptables: command not found.
Next steps
Build a rule with the iptables command generator, open SSH without locking yourself out with allow SSH safely, follow open port 80 on Ubuntu, or forward a port with DNAT. On Oracle Cloud the security list filters traffic as well.
FAQ
What is the difference between a table and a chain in iptables?
A table groups rules by purpose, such as filtering or address translation. A chain is a point in the packet's path, such as INPUT or FORWARD, where the rules of a table run.
Which iptables table is the default?
The filter table. Commands without -t work on it.
In what order do iptables tables run?
Within each chain: raw, mangle, nat, filter, security. A packet only passes the chains on its own path.
Does the order of rules matter?
Yes. Rules are checked from top to bottom and the first rule with a terminating target decides. If none matches, the chain policy applies.