Skip to main content

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.

◈ DIAGRAM
filter table allow or block
INPUT packets destined for this host
OUTPUT packets leaving this host
FORWARD packets routed through this host
nat 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 action
Actions: 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.

Bash
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/8 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Only now set the restrictive default
sudo iptables -P INPUT DROP
# Inspect, with line numbers
sudo iptables -L INPUT -n -v --line-numbers
# Delete by line number
sudo iptables -D INPUT 3
# Persist across reboot (Debian/Ubuntu)
sudo apt install iptables-persistent
sudo iptables-save | sudo tee /etc/iptables/rules.v4

How 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.

Bash
sudo iptables -L DOCKER-USER -n -v

Troubleshooting 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 Mistake

Setting -P INPUT DROP over SSH before adding an SSH allow rule. The session is cut instantly and you need console access to recover.

Security

Test changes from a second terminal before closing your current session. On remote hosts, schedule an automatic rollback (for example iptables-restore from a saved file with at) before applying risky changes.

Remember

iptables 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.

Found a mistake, an outdated step or a broken link?