Skip to main content

iptables/nftables — Firewall Rule Management

iptables is the traditional management tool for the Linux kernel netfilter firewall, while nftables is its modern replacement. This cheatsheet covers core operations for both: rule CRUD, NAT configuration, port forwarding, rule persistence, and advanced matching (rate limiting, string matching, connection tracking).

Updated: 2026-07-17·33 commands

Overview

iptables is the user-space utility program that allows a system administrator to configure the IP packet filter rules of the Linux kernel firewall. This cheatsheet covers basic rules, NAT, forwarding, and persistence.

Quick Start

``bash iptables -L -v -n # List current rules iptables -P INPUT DROP # Set default policy iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH ``

Refer to the command table below for detailed usage.

Basic Rules(19)

CommandLevel
iptables -L -v -n --line-numbers
List all rules with details, line numbers, and no DNS resolution
Basic
iptables -A
Append a rule to the end of a chain
Basic
iptables -I
Insert a rule at the beginning or a specific position
Basic
iptables -D
Delete a specific rule
Basic
iptables -F
Flush all rules from a specific chain
Basic
iptables -P
Set the default policy for a chain
Basic
iptables -p
Match protocol (tcp/udp/icmp/all)
Basic
iptables --dport
Match destination port
Basic
iptables -s
Match source IP address
Basic
iptables -j ACCEPT
Allow matching packets through
Basic
iptables -j DROP
Silently drop matching packets
Basic
iptables -j REJECT
Reject matching packets with an error response
Intermediate
ip6tables
IPv6 firewall rule management
Intermediate
nft list ruleset
List the entire nftables ruleset
Intermediate
nft add rule
Add a rule to an nftables chain
Intermediate
nft add table
Create an nftables table
Intermediate
nft add chain
Create an nftables chain
Expert
nft delete rule
Delete an nftables rule
Intermediate
nft flush ruleset
Flush all nftables rules
Intermediate

Advanced(6)

CommandLevel
iptables -j LOG
Log matching packets to syslog without blocking
Intermediate
iptables -m state --state
Match by connection state (legacy state module)
Intermediate
iptables -m conntrack --ctstate
Match by connection tracking state (recommended)
Intermediate
iptables -m limit
Rate limiting match (prevents DOS/brute force)
Expert
iptables -m recent
Dynamic match based on recent connection history
Expert
iptables -m string --string --algo
Match by packet payload (string filtering)
Expert

NAT(5)

CommandLevel
iptables -t nat -A PREROUTING
Add a DNAT rule in the NAT table PREROUTING chain
Intermediate
iptables -t nat -A POSTROUTING
Add a SNAT/MASQUERADE rule in the NAT table POSTROUTING chain
Intermediate
iptables -t nat -A PREROUTING -j DNAT
Destination NAT (port forwarding)
Intermediate
iptables -t nat -A POSTROUTING -j SNAT
Source NAT (fixed egress IP)
Intermediate
iptables -t nat -A POSTROUTING -j MASQUERADE
Source masquerade (dynamic egress IP)
Intermediate

Forwarding(1)

CommandLevel
iptables -t filter -A FORWARD
Add a forwarding control rule in the filter table FORWARD chain
Intermediate

Persistence(2)

CommandLevel
iptables-save
Export current iptables rules to stdout
Intermediate
iptables-restore
Restore iptables rules from file (atomic operation)
Intermediate

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-17.