25 个常用的 Linux iptables 规则【转】

转自

25 个常用的 Linux iptables 规则 - 文章 - 伯乐在线
http://blog.jobbole.com/108468/

# 1. 删除所有现有规则

iptables -F

# 2. 设置默认的 chain 策略

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

# 3. 阻止某个特定的 IP 地址

BLOCK_THIS_IP="x.x.x.x"
iptables -A INPUT -s "$BLOCK_THIS_IP" -j DROP

# 4. 允许全部进来的(incoming)SSH

iptables -A INPUT -i eth0 -p tcp --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 5. 只允许某个特定网络进来的 SSH

iptables -A INPUT -i eth0 -p tcp -s 192.168.200.0/ --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 6. 允许进来的(incoming)HTTP

iptables -A INPUT -i eth0 -p tcp --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 7. 多端口(允许进来的 SSH、HTTP 和 HTTPS)

iptables -A INPUT -i eth0 -p tcp -m multiport --dports ,, -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -m multiport --sports ,, -m state --state ESTABLISHED -j ACCEPT

# 8. 允许出去的(outgoing)SSH

iptables -A OUTPUT -o eth0 -p tcp --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 9. 允许外出的(outgoing)SSH,但仅访问某个特定的网络

iptables -A OUTPUT -o eth0 -p tcp -d 192.168.101.0/ --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 10. 允许外出的(outgoing) HTTPS

iptables -A OUTPUT -o eth0 -p tcp --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 11. 对进来的 HTTPS 流量做负载均衡

iptables -A PREROUTING -i eth0 -p tcp --dport  -m state --state NEW -m nth --counter  --every  --packet  -j DNAT --to-destination 192.168.1.101:
iptables -A PREROUTING -i eth0 -p tcp --dport -m state --state NEW -m nth --counter --every --packet -j DNAT --to-destination 192.168.1.102:
iptables -A PREROUTING -i eth0 -p tcp --dport -m state --state NEW -m nth --counter --every --packet -j DNAT --to-destination 192.168.1.103:

# 12. 从内部向外部 Ping

iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

# 13. 从外部向内部 Ping

iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT

# 14. 允许环回(loopback)访问

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# 15. 允许 packets 从内网访问外网

# if eth1 is connected to external network (internet)
# if eth0 is connected to internal network (192.168..x)
iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT

# 16. 允许外出的  DNS

iptables -A OUTPUT -p udp -o eth0 --dport  -j ACCEPT
iptables -A INPUT -p udp -i eth0 --sport -j ACCEPT

# 17. 允许 NIS 连接

# rpcinfo -p | grep ypbind ; This port is  and
#iptables -A INPUT -p tcp --dport -j ACCEPT
#iptables -A INPUT -p udp --dport -j ACCEPT
#iptables -A INPUT -p tcp --dport -j ACCEPT
#iptables -A INPUT -p udp --dport -j ACCEPT
#iptables -A INPUT -p tcp --dport -j ACCEPT
#iptables -A INPUT -p udp --dport -j ACCEPT

# 18. 允许某个特定网络 rsync 进入本机

iptables -A INPUT -i eth0 -p tcp -s 192.168.101.0/ --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 19. 仅允许来自某个特定网络的 MySQL 的链接

#iptables -A INPUT -i eth0 -p tcp -s 192.168.200.0/ --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
#iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 20. 允许 Sendmail 或 Postfix

iptables -A INPUT -i eth0 -p tcp --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 21. 允许 IMAP 和 IMAPS

#iptables -A INPUT -i eth0 -p tcp --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
#iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT
#iptables -A INPUT -i eth0 -p tcp --dport -m state --state NEW,ESTABLISHED -j ACCEPT
#iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 22. 允许 POP3 和 POP3S

#iptables -A INPUT -i eth0 -p tcp --dport  -m state --state NEW,ESTABLISHED -j ACCEPT
#iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT
#iptables -A INPUT -i eth0 -p tcp --dport -m state --state NEW,ESTABLISHED -j ACCEPT
#iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 23. 防止 DoS 攻击

iptables -A INPUT -p tcp --dport  -m limit --limit /minute --limit-burst  -j ACCEPT

# 24. 设置 422 端口转发到 22 端口

iptables -t nat -A PREROUTING -p tcp -d 192.168.102.37 --dport  -j DNAT --to 192.168.102.37:
iptables -A INPUT -i eth0 -p tcp --dport -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport -m state --state ESTABLISHED -j ACCEPT

# 25. 为丢弃的包做日志(Log)

iptables -N LOGGING
iptables -A INPUT -j LOGGING
iptables -A LOGGING -m limit --limit /min -j LOG --log-prefix "IPTables Packet Dropped: " --log-level
iptables -A LOGGING -j DROP
上一篇:前端程序员:月薪 5K 到 5 万,我干了啥(转)


下一篇:iptables 简单介绍及应用 Linux防火墙