Display status
1 | iptables -L -n -v |
Display specific INPUT or OUTPUT chain rules
1 | iptables -L INPUT -n -v |
Stop/Start/Restart iptables
1 | service iptables stop |
Delete specific rule
1 | # find line-number of rule |
Save current friewall rules
1 | service iptables save |
Delete ALL Existing Rules
1 | iptables --flush |
Setup Default Policies
The defaul policy after flush is ACCEPT. Should block all with DROP at first.1
2
3
4iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables -L -v -n
ONLY block incoming traffic
1 | iptables -P INPUT DROP |
Block Remote IP/sub-network
1 | iptables -A INPUT -s "192.168.1.100" -j DROP |
Block Incoming Port
1 | iptables -A INPUT -p tcp --dport 80 -j DROP |
Block Outgoing IP/sub-network
1 | iptables -A OUTPUT -d 75.126.153.206 -j DROP |
Block Facebook.com
1 | host -t a www.facebook.com |
DROP/ACCEPT traffic on MAC address
1 | iptables -A INPUT -m mac --mac-source 00:0F:EA:91:04:08 -j DROP |
Allow All Incoming/Outgoing SSH
1 | iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT |
ONLY Allow Incoming/Outgoing SSH from a Specific Network
1 | iptables -A INPUT -i eth0 -p tcp -s 192.168.1.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT |
Allow Incoming/Outgoing HTTP/HTTPS
1 | iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT |
Allow All Incoming SSH/HTTP/HTTPS
1 | iptables -A INPUT -i eth0 -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPT |
Block Incoming Ping
1 | iptables -A INPUT -p icmp --icmp-type echo-request -j DROP |
Allow Incoming Ping
1 | iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT |
Allow Outgoing Ping
1 | iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT |
Allow Loopback Access
1 | iptables -A INPUT -i lo -j ACCEPT |
Allow Intranet to Internet
eth0: intranet
eth1: internet1
iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
Allow outgoing DNS access
1 | iptables -A OUTPUT -p udp -o eth0 --dport 53 -j ACCEPT |
Allow rsync from a Specific Network
1 | iptables -A INPUT -i eth0 -p tcp -s 192.168.2.0/24 --dport 873 -m state --state NEW,ESTABLISHED -j ACCEPT |
Allow MySQL connection from a Specific Network
1 | iptables -A INPUT -i eth0 -p tcp -s 192.168.3.0/24 --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT |
Allow SMTP traffic
1 | iptables -A INPUT -i eth0 -p tcp --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT |
Allow POP3 traffic
1 | iptables -A INPUT -i eth0 -p tcp --dport 110 -m state --state NEW,ESTABLISHED -j ACCEPT |
1 | #pop3 security |
Open range of Port
1 | iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 7000:7010 -j ACCEPT |
Open range of IP
1 | iptables -A INPUT -p tcp --destination-port 80 -m iprange --src-range 192.168.1.100-192.168.1.200 -j ACCEPT |
Prevent DoS attack
1 | iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT |
- m limit: This uses the limit iptables extension
- limit 25/minute: This limits only maximum of 25 connection per minute. Change this value based on your specific requirement
- limit-burst 100: This value indicates that the limit/minute will be enforced only after the total number of connection have reached the limit-burst level.
Port Forwarding
ex: incoming ssh connection can come from both port 22 and 422.1
2
3
4
5#allow incoming tcp on port 422
iptables -A INPUT -i eth0 -p tcp --dport 422 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 422 -m state --state ESTABLISHED -j ACCEPT
#forward port 422 to port 22
iptables -t nat -A PREROUTING -p tcp -d 192.168.1.10 --dport 422 -j DNAT --to 192.168.1.10:22
Drop private network addresses on public interface
1 | iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP |
Common allowed ports
1 | ## open port ssh tcp port 22 ## |
Restrict the Number of Parallel Connections To a Server Per Client IP
1 | # allow 3 ssh connections per IP |
- connlimit-above 3 : Match if the number of existing connections is above 3.
- connlimit-mask 24 : Group hosts using the prefix length. For IPv4, this must be a number between (including) 0 and 32.