iptables Command
The iptables command is the primary tool for configuring the Linux kernel firewall (netfilter). It allows you to create rules to filter, modify, and route network packets, providing essential network security functionality.
Syntax
iptables [-t table] -I chain [rulenum] rule-specification
iptables [-t table] -R chain rulenum rule-specification
iptables [-t table] -D chain rulenum
iptables [-t table] -S [chain [rulenum]]
iptables [-t table] {-F|-L|-Z} [chain [rulenum]]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target
iptables [-t table] -E old-chain-name new-chain-name
Description
The iptables command configures the Linux kernel firewall by manipulating netfilter rules. It operates on different tables (filter, nat, mangle, raw) and chains (INPUT, OUTPUT, FORWARD) to control packet flow.
Key concepts:
- Tables - Different rule categories (filter, nat, mangle, raw)
- Chains - Rule sequences (INPUT, OUTPUT, FORWARD, custom)
- Rules - Conditions and actions for packet processing
- Targets - Actions to take (ACCEPT, DROP, REJECT, etc.)
- Matches - Criteria for rule matching
Tables and Chains
| Table | Purpose | Built-in Chains |
|---|---|---|
filter |
Packet filtering (default) | INPUT, OUTPUT, FORWARD |
nat |
Network Address Translation | PREROUTING, OUTPUT, POSTROUTING |
mangle |
Packet alteration | PREROUTING, OUTPUT, INPUT, FORWARD, POSTROUTING |
raw |
Connection tracking exemption | PREROUTING, OUTPUT |
Common Options
| Option | Description |
|---|---|
-A, --append |
Append rule to end of chain |
-I, --insert |
Insert rule at specific position |
-D, --delete |
Delete rule from chain |
-R, --replace |
Replace rule at specific position |
-L, --list |
List rules in chain |
-F, --flush |
Delete all rules in chain |
-P, --policy |
Set default policy for chain |
-N, --new-chain |
Create new user-defined chain |
-X, --delete-chain |
Delete user-defined chain |
-Z, --zero |
Zero packet and byte counters |
Examples
Basic Operations
List all rules
Shows all rules in the filter table
List rules with line numbers
Displays rules with line numbers for easy reference
List rules in specific table
Shows rules in the NAT table
Show rules with packet counts
Displays verbose output with packet and byte counters
Basic Filtering Rules
Allow incoming SSH
Allows incoming SSH connections on port 22
Allow incoming HTTP and HTTPS
Allows incoming web traffic on ports 80 and 443
Block specific IP address
Blocks all incoming traffic from 192.168.1.100
Allow traffic from specific subnet
Allows all traffic from the 192.168.1.0/24 subnet
Allow loopback traffic
Allows all loopback interface traffic
Stateful Filtering
Allow established connections
Allows packets for established and related connections
Allow new SSH connections
Allows new SSH connections and established responses
Advanced Rules
Rate limiting
Limits SSH connections to 3 per minute with burst of 3
Port range
Allows TCP traffic on ports 1000-2000
Multiple ports
Allows traffic on multiple specific ports
Time-based rules
Allows HTTP traffic only during business hours
NAT Rules
SNAT (Source NAT)
Changes source IP to 203.0.113.1 for outgoing packets
MASQUERADE
Masquerades outgoing packets (dynamic SNAT)
DNAT (Destination NAT)
Redirects incoming port 80 traffic to 192.168.1.100:8080
Policy and Management
Set default policies
Sets default policies: drop input/forward, accept output
Delete specific rule
Deletes rule number 3 from INPUT chain
Insert rule at specific position
Inserts SSH rule at position 1 (top of chain)
Flush all rules
Removes all rules from all tables
Saving and Restoring Rules
Save current rules
Saves current rules to file
Restore rules from file
Restores rules from saved file
Common Firewall Scenarios
Basic web server firewall
Router/Gateway configuration
Common Use Cases
When to Use iptables
- Server Security - Protect servers from unauthorized access
- Network Segmentation - Control traffic between network segments
- NAT/Routing - Set up network address translation and routing
- Traffic Shaping - Control bandwidth and connection limits
- Intrusion Prevention - Block malicious traffic patterns