iptables
Linux · Network · reference only
What it does
Configures the kernel's packet filtering rules: what traffic comes in, goes out or is forwarded. It is the base of many Linux firewalls.
Syntax
iptables [-t tabla] orden cadena [reglas] -j acción
Options and parameters
-LLists the rules of one chain or of all.
-nShows numeric addresses and ports, without resolving names.
-A cadenaAppends a rule to the end of the chain.
-D cadenaRemoves a rule, by its number or its text.
-FFlushes all the rules of the chain or of all.
-P cadena acciónChanges a chain's default policy.
-t tablaChooses the table: filter, nat, mangle or raw.
-j acciónWhat to do with the packet: ACCEPT, DROP, REJECT, LOG...
Usage examples
sudo iptables -L -nShows all rules with numbers.
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPTAllows incoming connections to port 22.
sudo iptables -D INPUT 3Removes rule number 3 from the INPUT chain.
sudo iptables -SShows the rules in the format they would be written.
Notes and common mistakes
- A wrong command can lock you out: e.g. -P INPUT DROP without a rule allowing SSH.
- Rules are lost on reboot unless you save them with iptables-persistent or another tool.
- On Debian 12 iptables uses the nftables backend underneath.