Cheat sheets

Cheat sheet

iptables and nftables cheat sheet

Linux firewall and NAT rules on one page: iptables, nft and conntrack.

iptables

Lists and changes the kernel's firewall and NAT rules, with a counter on each rule of what it matched.

iptables -L -n -v
Lists every filter rule with how many packets and bytes each matched.
iptables -S FORWARD
Prints the FORWARD chain as the commands that would recreate it.
iptables -A FORWARD -i officeNetwork -s 10.0.1.40 -j DROP
A standard access list line: drops whatever 10.0.1.40 sends in on the office card (a card named after its network).
iptables -R FORWARD 2 -o serverNetwork -s 10.0.1.40 -j DROP
Replaces line 2 with the same deny, checked out on the server card instead.
iptables -t nat -L -n -v --line-numbers
Lists the NAT rules, numbered, with their counters.
iptables -I INPUT -p tcp --dport 22 -j DROP
Drops every SSH connection to this device, silently.
iptables -I FORWARD -d 10.0.2.10 -p tcp --dport 80 -j REJECT --reject-with tcp-reset
Refuses forwarded web connections to 10.0.2.10 with a TCP reset.
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
Lets replies to connections already allowed through: the heart of a stateful firewall.

Every iptables option, with real output

nft

Lists and changes nftables rules, sets and counters, the successor of iptables; also filters frames on a bridge.

nft list set bridge netlab-security dhcp-bindings
Shows the switch's DHCP snooping bindings: port, IP and MAC of each lease, with the time left.
nft list counter bridge netlab-security arp-inspection
Shows how many ARP frames ARP inspection has dropped.
nft list set bridge netlab-security port-security-victim
Shows the secure MAC addresses port victim has learned.
nft flush set bridge netlab-security port-security-victim
Forgets the port's secure addresses, as Cisco's clear port-security.
nft list ruleset
Prints every table, chain and rule: the whole firewall.
nft -a list ruleset
Prints the ruleset with each rule's handle, the number you delete it by.
nft add table inet filter
Makes a table for IPv4 and IPv6 together (the inet family).
nft add chain inet filter input '{ type filter hook input priority 0; policy drop; }'
Makes an input chain that sees packets for this device and drops whatever no rule accepts.

Every nft option, with real output

conntrack

Lists and edits the kernel's table of tracked flows: what a stateful firewall and NAT remember.

conntrack -L -p udp
Lists the UDP flows the router tracks, with their NAT mappings and timers.
conntrack -L -p tcp
Lists the TCP connections, each with its four addresses: as sent, and as the reply must look.
conntrack -D -p udp -s 10.0.2.10
Forgets one device's UDP flows, so its next packets are mapped under the current rules.
cat /proc/net/nf_conntrack
The same table without the tool.

Every conntrack option, with real output

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab