iptables
iptables is the command-line tool for configuring the Linux kernel's netfilter packet filter. It organizes rules into tables and chains that decide whether each network packet is accepted, dropped, or rewritten.
Understanding iptables
What Is iptables?
iptables writes firewall rules into the kernel's netfilter framework. Frontends such as ufw and firewalld generate iptables or nftables rules for you, but knowing iptables directly is essential for debugging, for working with Docker, and for systems where automatic tools create unexpected rules. On many modern distributions the iptables command is a compatibility layer over nftables.
How Tables, Chains, and Rules Fit Together
Packets pass through chains of rules in order. The first matching rule decides the packet's fate. If none match, the chain's default policy applies.
filter table allow or block INPUT packets destined for this host OUTPUT packets leaving this host FORWARD packets routed through this hostnat table address translation (PREROUTING, POSTROUTING)mangle table packet modification iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/8 -j ACCEPT | | | | | append chain protocol dest port source actionActions: ACCEPT, DROP (silent), REJECT (sends error), LOG (continues)How to Build a Safe Baseline Ruleset
Add the allow rules first and set the DROP policy last, or you cut off your own SSH session.
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTsudo iptables -A INPUT -i lo -j ACCEPTsudo iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/8 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # Only now set the restrictive defaultsudo iptables -P INPUT DROP # Inspect, with line numberssudo iptables -L INPUT -n -v --line-numbers # Delete by line numbersudo iptables -D INPUT 3 # Persist across reboot (Debian/Ubuntu)sudo apt install iptables-persistentsudo iptables-save | sudo tee /etc/iptables/rules.v4How iptables Works With Docker
Docker inserts its own rules, so published container ports can bypass rules you add to INPUT. Put restrictions for container traffic in the DOCKER-USER chain, which is evaluated before Docker's rules.
sudo iptables -L DOCKER-USER -n -vTroubleshooting iptables
| Symptom | Command | What to Check |
|---|---|---|
| All traffic blocked | sudo iptables -L -n |
DROP policy, no allow rules |
| Rules lost on reboot | iptables-save |
Not persisted |
| Docker ports exposed | iptables -L DOCKER-USER |
Rules in wrong chain |
| Connections dropping | conntrack -C |
Conntrack table full |
Common MistakeSetting
-P INPUT DROPover SSH before adding an SSH allow rule. The session is cut instantly and you need console access to recover.
SecurityTest changes from a second terminal before closing your current session. On remote hosts, schedule an automatic rollback (for example
iptables-restorefrom a saved file withat) before applying risky changes.
Rememberiptables rules live in memory and vanish on reboot unless you save and restore them.
Frequently Asked Questions
How do tables and chains work together?
Tables group rules by purpose (filter, nat, mangle) and chains are the points in a packet's path (INPUT, OUTPUT, FORWARD and so on). A packet traverses the relevant chain top to bottom and the first matching rule decides, otherwise the default policy applies.
What is the difference between DROP and REJECT?
DROP silently discards the packet so the sender waits and times out. REJECT discards it and sends back an error so the sender fails fast. DROP gives away less, REJECT is friendlier for internal services.
Is iptables still relevant now that nftables exists?
Yes. nftables is the newer framework, but many systems still use the iptables command, often implemented on top of nftables, and Docker and Kubernetes tooling still emit iptables-style rules.