Skip to content

📋 Cheat Sheet

Rule Operations

OperationCommand
List all rulesiptables -L -v -n --line-numbers
List a specific chainiptables -L INPUT -v -n
List a specific tableiptables -t nat -L -v -n
Append a ruleiptables -A INPUT -p tcp --dport 80 -j ACCEPT
Insert at line 1iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT
Delete a rule by numberiptables -D INPUT 3
Flush all rulesiptables -F
Flush rules and delete custom chainsiptables -F && iptables -X
Set default policyiptables -P INPUT DROP
Reset countersiptables -Z
Save rulesiptables-save > /etc/iptables/rules.v4
Restore rulesiptables-restore < /etc/iptables/rules.v4

Common Match Conditions

ConditionSyntaxExample
Protocol-p tcp/udp/icmp-p tcp
Destination port--dport port--dport 80
Multiple ports-m multiport --dports-m multiport --dports 80,443
Source IP-s IP/CIDR-s 192.168.1.0/24
Destination IP-d IP/CIDR-d 10.0.0.50
Network interface-i interface-i eth0
Connection state-m conntrack --ctstate-m conntrack --ctstate NEW,ESTABLISHED
Rate limit-m limit --limit-m limit --limit 5/min
Connection limit-m connlimit --connlimit-above-m connlimit --connlimit-above 10
IP set-m set --match-set-m set --match-set blacklist src
TCP flags--tcp-flags--tcp-flags SYN,ACK,FIN SYN

Common Targets

TargetPurposeExample
ACCEPTAllow the packet-j ACCEPT
DROPSilently drop the packet-j DROP
REJECTReject and notify the peer-j REJECT --reject-with tcp-reset
LOGLog, then continue matching-j LOG --log-prefix "DROP: "
DNATDestination NAT / port forwarding-j DNAT --to 10.0.0.5:8080
SNATSource NAT / shared public IP-j SNAT --to 1.2.3.4
MASQUERADEDynamic SNAT for dial-up or dynamic IPs-j MASQUERADE
REDIRECTRedirect to a local port-j REDIRECT --to-port 8080
MARKMark packets for policy routing-j MARK --set-mark 1
RETURNReturn to the calling chain-j RETURN

iptables vs nftables vs firewalld

Featureiptablesnftablesfirewalld
EraClassic v4Newer generationHigh-level wrapper
SyntaxVerboseCleanerSimple
PerformanceLinear matchingSet matching, O(1)Uses nftables underneath
Persistenceiptables-savenft list rulesetPersistent by design
Best forOlder systems, Docker hostsNew systems, high performanceDesktops, simple servers
Learning value⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

💡 Tip: iptables is the foundation. Even if you eventually use nftables or firewalld, understanding tables, chains, matches, and targets makes all firewall tools much easier to reason about.