An iptables cheat sheet is useful for managing firewall rules on a Linux system. Here’s a iptables cheat sheet:
iptables Cheat Sheet
View Rules
View Current Rules:
iptables -L
View Detailed Rules:
iptables -L -v
Flush Rules
Flush All Rules:
iptables -F
Default Policies
Set Default Policies:
iptables -P INPUT <policy>
iptables -P OUTPUT <policy>
iptables -P FORWARD <policy>
- Replace
<policy>
withACCEPT
,DROP
, orREJECT
.
Create Rules
Allow Incoming Connections on a Port:
iptables -A INPUT -p tcp --dport <port> -j ACCEPT
Allow Outgoing Connections on a Port:
iptables -A OUTPUT -p tcp --dport <port> -j ACCEPT
Allow Incoming SSH:
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Delete Rules
Delete Rule by Number:
iptables -D INPUT <rule_number>
Port Forwarding
Forward Port 80 to 8080:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
Stateful Firewall
Allow Related and Established Connections:
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
Logging
Log Dropped Packets:
iptables -A INPUT -j LOG --log-prefix "Dropped: "
Save and Restore Rules
Save Rules:
iptables-save > /etc/iptables/rules.v4
Restore Rules:
iptables-restore < /etc/iptables/rules.v4
IPv6 Rules
View IPv6 Rules:
ip6tables -L
Allow Incoming SSH (IPv6):
ip6tables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables Service
Start iptables Service:
service iptables start
Stop iptables Service:
service iptables stop
Restart iptables Service:
service iptables restart
Clear iptables Service
Clear and Reset iptables Service:
iptables -F
This cheat sheet provides basic commands for managing iptables rules on a Linux system. Always be cautious when configuring firewall rules, especially on production systems, to avoid accidental lockouts or security vulnerabilities.