Master iptables: Essential Command Syntax and Practical Examples
This article breaks down the fundamental iptables command syntax, explains each option and parameter, provides practical examples for adding, inserting, listing, deleting, and managing firewall rules, and covers common modules, backup, and restoration techniques for effective Linux network security.
iptables command basic syntax
iptables [-t table] command [chain] [match] [-j target]
Below is a breakdown of iptables command components:
-t table
Specifies the table to use; options are filter, nat, mangle. If omitted, the default is the filter table.
command parameter
Specifies the operation to perform on the rule. Common commands include: -A Append: adds a rule at the end.
iptables -A INPUT -j DROP # Reject all incoming traffic (as the last rule)
# If -t is not specified, the filter table is used by default -IInsert: inserts a rule at a specified position.
iptables -I INPUT 2 -s 10.10.10.1 -j ACCEPT # Allow host 10.10.10.1 to access the machine
# Insert as the second rule in the INPUT chain of the filter table -LList: displays the rule list.
Common options:
n: show numeric IPs and ports
v: verbose output
x: disable automatic unit conversion
--line-number: show rule numbers
iptables -L -n -v --line-number-DDelete: removes a rule from the list.
iptables -D INPUT 2
# Delete the second rule in the INPUT chain of the filter table -PPolicy: sets the default policy for a chain.
iptables -P INPUT DROP
# Set the default policy of the INPUT chain in the filter table to DROP -FFlush: clears all rules.
iptables -F INPUT # Clear rules in INPUT chain of filter table
iptables -F # Clear all chains in filter table
iptables -t nat -F PREROUTING # Clear PREROUTING chain in NAT table
iptables -t nat -F # Clear all chains in NAT tableNote: -F clears rules but does not affect the default policy set with -P. In production, if the default policy is DROP, running iptables -F can lock you out because it removes ACCEPT rules while leaving the DROP policy.
-ZZero: resets counters for the specified chain (or all chains if omitted).
iptables -Z INPUT
# Reset counters on INPUT chain of filter tableMatch parameters
Match by network interface: -i Matches incoming interface (commonly used with nat table). -o Matches outgoing interface.
-i eth0 # Match packets entering via eth0
-o eth1 # Match packets leaving via eth1Match source and destination addresses: -s Matches source address (IP, subnet, or hostname; empty matches any). -d Matches destination address.
iptables -A INPUT -s 10.10.10.10 -j DROP
iptables -A OUTPUT -d www.baidu.com -j DROPMatch protocol type: -p Matches protocol (tcp, udp, icmp, etc.).
iptables -A INPUT -s 10.10.10.10 -p icmp -j DROPMatch source and destination ports: --sport Matches source port (single or range). --dport Matches destination port.
--sport 23 # Source port 23
--sport 2000:3000 # Source ports 2000-3000
--sport :2000 # Source ports up to 2000
--sport 1000: # Source ports 1000 and aboveNote: --sport and --dport must be used together with -p.
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Allow external traffic to local port 80Target actions
-j ACCEPTAccepts the packet without further processing.
iptables -A INPUT -j ACCEPT
# Allow all incoming traffic -j DROPDrops the packet, preventing it from passing.
iptables -A FORWARD -s 10.10.10.10 -j DROP
# Block packets from 10.10.10.10 -j SNATSource NAT: can translate to a single IP or an IP pool.
# Single IP
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to 18.18.18.18
# IP pool
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to 18.18.18.18-18.18.18.28 -j DNATDestination NAT: can translate to a single IP or an IP pool.
# Single IP
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.1.1
# IP pool
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.1.1-192.168.1.10 -j MASQUERADEDynamic SNAT for interfaces with changing IP addresses.
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
# Masquerade source addresses on eth0iptables common extension modules
State matching:
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# Allow established connectionsMAC address matching:
iptables -A INPUT -m mac --mac-source xx:xx:xx:xx:xx:xx -j DROP
# Drop packets from specified MAC addressRate limiting:
iptables -A FORWARD -d 192.168.1.1 -m limit --limit 50/s -j ACCEPT
iptables -A FORWARD -d 192.168.1.1 -j DROP
# Limit to 50 packets per second, then drop excesslimit matches at a specified rate; to enforce a limit you need a subsequent DROP rule.
Multiport matching:
iptables -A INPUT -p tcp -m multiport --dports 22,53,80,443 -j ACCEPT
# Allow access to ports 22,53,80,443Note: this option must be used together with -p.
iptables rule backup and restore
When iptables commands are executed, rules reside only in memory and are not saved to a file; after a reboot they disappear. After confirming the rules, save them with:
service iptables save
# Saves to /etc/sysconfig/iptablesTo back up, copy the file: cp /etc/sysconfig/iptables /opt/myipt.rule To restore:
iptables-restore < /opt/myipt.rule
service iptables saveSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
