Iptables & Netfilter Firewall Commands Cheat Sheet
Production Linux Netfilter / iptables firewall command matrix covering chains, port allow/drop rules, rate limiting, NAT masquerade, and persistent rule management with 1-click copy.
Inspection & Listing (4 commands)
sudo iptables -L -n -v
List all active rules across standard chains with numeric IP addresses, ports, and packet/byte counters.
sudo iptables -L INPUT -n -v --line-numbers
Lists incoming (INPUT) rules with sequential index numbers needed for precise rule deletion.
sudo iptables -t nat -L -n -v
Inspects rules in the Network Address Translation (NAT) table, including PREROUTING and POSTROUTING.
sudo iptables -Z
Zeroes all packet and byte counters across all chains to monitor new network traffic bursts.
Filtering & Hardening (6 commands)
sudo iptables -P INPUT DROP
Sets default inbound traffic policy to DROP all packets unless explicitly permitted by subsequent rules.
sudo iptables -P FORWARD DROP
Sets default forwarding traffic policy to DROP, preventing unauthorized packet routing through server.
sudo iptables -A INPUT -i lo -j ACCEPT
Allows all local loopback traffic (127.0.0.1) essential for local IPC, database connections, and daemons.
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
Allows established and related responses to existing outbound connections (DNS lookups, web downloads).
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Allows incoming SSH management connections on TCP port 22.
sudo iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
Allows incoming HTTP (80) and HTTPS (443) web traffic in a single atomic multiport rule.
Security & Anti-DDoS (5 commands)
sudo iptables -A INPUT -s 198.51.100.42 -j DROP
Instantly blocks all incoming network packets originating from specific attacker IP address.
sudo iptables -A INPUT -s 203.0.113.0/24 -j DROP
Blocks entire CIDR /24 subnet (256 addresses) from accessing any service on this server.
sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set
Tracks new SSH connection attempts per source IP address in recent memory table.
sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
Drops SSH connection attempts if source IP exceeds 4 new connection requests within 60 seconds.
sudo iptables -A INPUT -p tcp --syn -m limit --limit 2/s --limit-burst 6 -j ACCEPT
Mitigates TCP SYN flood attacks by rate limiting new SYN packets to 2 per second with 6 burst.
NAT & Port Forwarding (5 commands)
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
Transparently redirects incoming traffic on standard port 80 to unprivileged backend service on 8080.
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Enables dynamic NAT IP masquerading on WAN interface eth0 for internal VPN or container subnets.
sudo iptables -D INPUT 3
Deletes specific rule at line number 3 in the INPUT chain identified via --line-numbers.
sudo iptables -F && sudo iptables -X
Flushes all existing rules and deletes all custom user chains. Resets firewall to defaults.
sudo iptables-save | sudo tee /etc/iptables/rules.v4
Saves current runtime firewall configuration to persistent disk file reloaded on system boot.