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.

The short version

Packet flow diagram

iptables packet flow A packet enters, passes PREROUTING and a routing decision. Packets for this host go through INPUT to a local process. Packets for another host go through FORWARD and POSTROUTING. Packets from a local process go through OUTPUT and POSTROUTING. Packet in PREROUTINGraw · mangle · nat Routing decision INPUTmangle · filter Local process FORWARDmangle · filter OUTPUTraw · mangle · nat · filter POSTROUTINGmangle · nat Packet out for another hostfrom this host

Inside each box the tables are listed in the order they run. Three paths exist:

Packet isPath
Addressed to this serverPREROUTING → INPUT → local process
Passing through this serverPREROUTING → FORWARD → POSTROUTING
Sent by this serverOUTPUT → 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

TableUsed forChains
filterAllow or block packets. Default table.INPUT, FORWARD, OUTPUT
natRewrite source or destination address and port: port forwarding, masquerading.PREROUTING, OUTPUT, POSTROUTING (INPUT on recent kernels)
mangleChange packet fields or set marks, for example TTL, TOS or MARK.All five
rawExempt packets from connection tracking.PREROUTING, OUTPUT
securitySELinux 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.

© PacketUtils. Tools run in your browser. Review every command before using it in production.HomeAboutContactPrivacy PolicyGitHub