Common iptables Command Cheatsheet

By adminGeneral

iptables is the standard Linux firewall tool. This cheatsheet provides the most commonly used iptables commands for server administration.

View Current Rules

iptables -L -n -v
iptables -L -n -v --line-numbers

Allow/Block Connections

# Allow SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allow HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Allow HTTPS
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Block an IP
iptables -A INPUT -s 192.168.1.100 -j DROP

Save and Restore Rules

# Save rules
iptables-save > /etc/iptables.rules
# Restore rules
iptables-restore < /etc/iptables.rules

Flush All Rules

iptables -F
iptables -X
iptables -Z