iptables常用的规则配置方法

上一篇 / 下一篇  2017-03-21 16:04:23 / 个人分类:linux

iptables -F         # 清空表规则,默认filter表
iptables -t nat -F     # 清空nat表
iptables -A INPUT -p tcp --dport 22 -j ACCEPT    # 允许TCP的22端口访问
iptables -I INPUT -p udp --dport 53 -j ACCEPT    # 允许UDP的53端口访问,插入在第一条
iptables -A INPUT -p tcp --dport 22:25 -j ACCEPT # 允许端口范围访问
iptables -D INPUT -p tcp --dport 22:25 -j ACCEPT # 删除这条规则

# 允许多个TCP端口访问
iptables -A INPUT -p tcp -m multiport --dports 22,80,8080 -j ACCEPT  
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT   # 允许192.168.1.0段IP访问
iptables -A INPUT -s 192.168.1.10 -j DROP       # 对1.10数据包丢弃
iptables -A INPUT -i eth0 -p icmp -j DROP       # eth0网卡ICMP数据包丢弃,也就是禁ping

# 允许来自lo接口,如果没有这条规则,将不能通过127.0.0.1访问本地服务
iptables -A INPUT -i lo -j ACCEPT 
  
# 限制并发连接数,超过30个拒绝    
iptables -I INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 30 -j REJECT 
  
# 限制每个IP每秒并发连接数最大3个
iptables -I INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT           
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT

# iptables服务器作为网关时,内网访问公网
iptables –t nat -A POSTROUTING -s [内网IP或网段] -j SNAT --to [公网IP]      

# 访问iptables公网IP端口,转发到内网服务器端口          
iptables –t nat -A PREROUTING -d [对外IP] -p tcp --dport [对外端口] -j DNAT --to [内网IP:内网端口] 

# 本地80端口转发到本地8080端口  
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

# 允许已建立及该链接相关联的数据包通过            
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT    

# ASDL拨号上网                
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o pppo -j MASQUERADE                   

iptables -P INPUT DROP  # 设置INPUT链缺省操作丢弃所有数据包,只要不符合规则的数据包都丢弃。注意要在最后设置,以免把自己关在外面!

TAG:

 

评分:0

我来说两句

日历

« 2024-03-26  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 11708
  • 日志数: 7
  • 建立时间: 2016-11-09
  • 更新时间: 2017-03-28

RSS订阅

Open Toolbar