iptables is the netfilter administration tool for IPv4 packet filtering, network address translation (NAT), and packet mangling on Linux systems. This page provides a comprehensive low-level reference for iptables rule construction, target actions, match extensions, and advanced filtering techniques.
Comprehensive low-level iptables reference: rule construction, all match extensions (TCP flags, state, limit, multiport, iprange, string, recent, time, owner), NAT targets, complete rulesets, and nftables migration. For DNS and IPv6 firewall configuration, see DNS and Firewall and IPv6. For IANA port assignments, see IANA Internet Standards.
# Reject with specific ICMP typeiptables-AINPUT-ptcp--dport22-jREJECT--reject-withtcp-reset
iptables-AINPUT-picmp-jREJECT--reject-withicmp-host-unreachable
iptables-AINPUT-pudp--dport53-jREJECT--reject-withicmp-port-unreachable
# Common flag combinations for security# Block null scans (no flags set)iptables-AINPUT-ptcp--tcp-flagsALLNONE-jDROP
# Block XMAS scans (FIN, PSH, URG)iptables-AINPUT-ptcp--tcp-flagsALLFIN,PSH,URG-jDROP
# Block SYN/RST combination (invalid)iptables-AINPUT-ptcp--tcp-flagsSYN,RSTSYN,RST-jDROP
# Allow established connectionsiptables-AINPUT-ptcp--tcp-flagsALLACK,RST,SYN,FIN-mstate--stateESTABLISHED-jACCEPT
# Allow SYN for new connectionsiptables-AINPUT-ptcp--syn-mstate--stateNEW-jACCEPT
# Classic state match (iptables)iptables-AINPUT-mstate--stateESTABLISHED,RELATED-jACCEPT
# Modern conntrack match (nftables-compatible)iptables-AINPUT-mconntrack--ctstateESTABLISHED,RELATED-jACCEPT
# States:# NEW - First packet of new connection# ESTABLISHED - Part of existing connection# RELATED - New connection related to existing (FTP data, ICMP error)# INVALID - Packet that conntrack cannot process# UNTRACKED - Packet marked to bypass conntrack (raw table)
# Match multiple ports efficientlyiptables-AINPUT-ptcp-mmultiport--dports80,443,8080-jACCEPT
iptables-AINPUT-ptcp-mmultiport--sports22,80,443-jACCEPT
# Maximum 15 ports per rule
# Match a range of IPs without CIDRiptables-AINPUT-miprange--src-range192.168.1.10-192.168.1.50-jACCEPT
iptables-AINPUT-miprange--dst-range10.0.0.1-10.0.0.100-jDROP
# SSH brute force protectioniptables-AINPUT-ptcp--dport22-mstate--stateNEW\-mrecent--set--namessh_attempts
iptables-AINPUT-ptcp--dport22-mstate--stateNEW\-mrecent--update--seconds60--hitcount4--namessh_attempts-jDROP
iptables-AINPUT-ptcp--dport22-mstate--stateNEW-jACCEPT
# Parameters:# --set - Add source IP to list# --update - Update existing entry# --seconds N - Time window# --hitcount N - Maximum hits in window# --name listname - Named list# --rcheck - Check without updating# --remove - Remove matching entry
# Allow SSH only during business hoursiptables-AINPUT-ptcp--dport22-mtime\--timestart09:00--timestop17:00\--weekdaysMon,Tue,Wed,Thu,Fri\--datestart2024-01-01--datestop2024-12-31\-jACCEPT
iptables-AINPUT-ptcp--dport22-jDROP
# Allow specific user to access networkiptables-AOUTPUT-mowner--uid-owner1000-jACCEPT
# Allow specific groupiptables-AOUTPUT-mowner--gid-owner1000-jACCEPT
# Match by process IDiptables-AOUTPUT-mowner--pid-owner1234-jACCEPT
#!/bin/bash# Enable IP forwardingsysctl-wnet.ipv4.ip_forward=1# NAT for LAN to WANiptables-tnat-APOSTROUTING-oeth0-jMASQUERADE
# Forward establishediptables-AFORWARD-mconntrack--ctstateESTABLISHED,RELATED-jACCEPT
# Forward from LANiptables-AFORWARD-ieth1-oeth0-jACCEPT
# Port forwarding exampleiptables-tnat-APREROUTING-ptcp--dport8080-jDNAT--to-destination192.168.1.100:80
iptables-AFORWARD-ptcp-d192.168.1.100--dport80-jACCEPT