iptables
Overview
The iptables
command is a user-space utility for configuring Linux kernel firewall rules. It controls network packet filtering, NAT, and packet mangling through the netfilter framework.
Syntax
iptables [options] -t table -A chain rule-specification
iptables [options] -t table -D chain rule-specification
iptables [options] -t table -L [chain]
Common Options
Option | Description |
---|---|
-A chain |
Append rule to chain |
-D chain |
Delete rule from chain |
-I chain |
Insert rule in chain |
-L |
List rules |
-F |
Flush all rules |
-P chain target |
Set default policy |
-t table |
Specify table |
-j target |
Jump to target |
-p protocol |
Protocol (tcp, udp, icmp) |
-s source |
Source address |
-d destination |
Destination address |
--dport port |
Destination port |
--sport port |
Source port |
-i interface |
Input interface |
-o interface |
Output interface |
Tables
Table | Purpose |
---|---|
filter |
Packet filtering (default) |
nat |
Network Address Translation |
mangle |
Packet alteration |
raw |
Connection tracking exemption |
Chains
Chain | Description |
---|---|
INPUT |
Incoming packets |
OUTPUT |
Outgoing packets |
FORWARD |
Forwarded packets |
PREROUTING |
Before routing decision |
POSTROUTING |
After routing decision |
Key Use Cases
- Firewall configuration
- Network security
- Port blocking/allowing
- NAT configuration
- Traffic filtering
Examples with Explanations
Example 1: List Current Rules
iptables -L -n -v
Shows all rules with packet counts and no DNS resolution
Example 2: Allow SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Allows incoming SSH connections on port 22
Example 3: Block IP Address
iptables -A INPUT -s 192.168.1.100 -j DROP
Blocks all traffic from specific IP address
Example 4: Allow HTTP and HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
Allows web traffic on ports 80 and 443
Basic Firewall Setup
Set default policies:
iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT
Allow loopback:
iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT
Allow established connections:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Common Rules
Allow ping:
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
Allow specific subnet:
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
Rate limiting:
iptables -A INPUT -p tcp --dport 22 -m limit --limit 3/min -j ACCEPT
NAT Configuration
SNAT (Source NAT):
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 203.0.113.1
DNAT (Destination NAT):
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.10:8080
Masquerading:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Port Forwarding
Forward external port to internal:
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80 iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 80 -j ACCEPT
Advanced Filtering
Connection tracking:
iptables -A INPUT -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
Time-based rules:
iptables -A INPUT -p tcp --dport 80 -m time --timestart 09:00 --timestop 17:00 -j ACCEPT
String matching:
iptables -A INPUT -p tcp --dport 80 -m string --string "malware" -j DROP
Performance Analysis
- Kernel-level filtering (fast)
- Rules processed sequentially
- First match wins
- Can impact network performance
- Optimize rule order
Best Practices
- Always have a backup plan
- Test rules before applying
- Use specific rules over general ones
- Document your rules
- Regular rule auditing
Rule Management
Save rules:
iptables-save > /etc/iptables/rules.v4
Restore rules:
iptables-restore < /etc/iptables/rules.v4
Delete specific rule:
iptables -D INPUT 3 # Delete rule number 3
Logging
Log dropped packets:
iptables -A INPUT -j LOG --log-prefix "DROPPED: " iptables -A INPUT -j DROP
Log specific traffic:
iptables -A INPUT -p tcp --dport 22 -j LOG --log-prefix "SSH: "
Scripting Applications
Firewall script:
#!/bin/bash # Flush existing rules iptables -F iptables -X # Set default policies iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # Allow loopback iptables -A INPUT -i lo -j ACCEPT # Allow established connections iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow SSH iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Save rules iptables-save > /etc/iptables/rules.v4
Security Applications
DDoS protection:
iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
Block port scanning:
iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP iptables -A INPUT -m recent --name portscan --set -j LOG --log-prefix "Portscan: "
Troubleshooting
- Check rule syntax before applying
- Use -v for verbose output
- Test connectivity after changes
- Keep backup of working rules
- Use logging for debugging
Integration Examples
With fail2ban:
# fail2ban creates iptables rules automatically fail2ban-client status sshd
With monitoring:
# Monitor dropped packets iptables -L -n -v | grep DROP
Common Mistakes
- Locking yourself out via SSH
- Wrong rule order
- Forgetting to save rules
- Not testing rules
- Overly permissive rules
Migration to nftables
Modern systems use nftables:
# Translate iptables rules
iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
Backup and Recovery
Backup current rules:
iptables-save > iptables-backup-$(date +%Y%m%d).rules
Emergency reset:
iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -F
Performance Optimization
- Put most common rules first
- Use specific matches
- Avoid unnecessary logging
- Use connection tracking efficiently
- Consider rule consolidation