Chinaunix首页 | 论坛 | 博客
  • 博客访问: 537856
  • 博文数量: 119
  • 博客积分: 3167
  • 博客等级: 中校
  • 技术积分: 1215
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-20 21:21
文章分类

全部博文(119)

文章存档

2015年(21)

2012年(4)

2011年(1)

2007年(11)

2006年(50)

2005年(32)

分类: LINUX

2005-12-20 21:49:03

iptables 使用中遇到的

1. 注意指定包的流向
shell > iptables -P INPUT DROP
shell > iptables A INPUT -p icmp -j ACCEPT
shell > ping 192.168.0.1 [timeout]
shell > ping 192.168.0.1 [timeout]

shell > iptables A INPUT -p icmp -j ACCEPT
shell > iptables A INPUT -i eth1 -p icmp -j ACCEPT
win cmd > ping 192.168.0.1  [ok]
shell > ping 192.168.0.1 [timeout]

shell > iptables -A INPUT -i lo -p icmp -j ACCEPT
win cmd > ping 192.168.0.1  [ok]
shell > ping 192.168.0.1 [ok]

比如
netstat -ntal
tcp        0      0 192.168.0.1:53          0.0.0.0:*               LISTEN
tcp        0      0 219.140.183.152:53      0.0.0.0:*               LISTEN

INPUT POLICY 为 DROP
iptables -A INPUT -i eth1 -p tcp --dport domain -j ACCEPT
同上 ,  与eth1相连的机器的DNS请求可以得到回应 但是
host localhost
找不到DNS server
iptables -A INPUT -i lo -p tcp --dport domain -j ACCEPT [ OK ]

所以 一般会这样定义一条规则

shell > iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT [这种包最多所以放在最前面]
shell > iptables -A INPUT -i lo -m state --state NEW -j ACCEPT  [匹配本机主动发出去的包]


2. 规则中I 与 A 的区别
iptables -A INPUT -s 192.168.0.0/24 -p icmp -j DROP
iptables -A INPUT -s 192.168.0.127/24 -p icmp -j ACCEPT
pc127 > ping 192.168.0.1 [timeout]

iptables -A INPUT -s 192.168.0.0/24 -p icmp -j DROP
iptables -I INPUT -s 192.168.0.127/24 -p icmp -j ACCEPT
pc127 > ping 192.168.0.1 [OK]


3. 使用limit
iptables -A INPUT -i eth1 -p icmp --icmp-typp echo-request -m limit --limit 5/minute -j ACCEPT
令牌桶算发 初始就有5个令牌 所以开始时看不出来limit

iptables -A INPUT -i eth1 -p icmp --icmp-type echo-request -m limit --limit-burst 10 --limit 2/second -j ACCEPT
在超过10个包后 每秒2个包

iptables -A INPUT -i eth0 -p icmp --icmp-type echo-request -m limit --limit-burst 10 --limit 2/second -j ACCEPT
最好在这个包的后面加上
iptables -A INPUT -i eth0 -p icmp DROP 
虽然默认规则是DROP,这里添加这条规则可以避免不是echo-request的icmp包还要继续走后面的规则 , 在定义其他规则的时候一样需要

这样做

 

4. 清空所有规则
iptables -Z  清零
iptables -X  清除所有自定义的chains
iptables -F  清除所有的rule
iptables -t nat -Z 
iptables -t nat -X 
iptables -t nat -F 
.....mangle...
看起来好像清除了所有规则  但是  上面的命令并不改变默认规则 所以 需要自己再定义默认的POLICY
iptables -P INPUT ACCEPT
....


5. 使用time动态规则 需要补丁
iptables -A INPUT -i eth0 -p tcp --dport 22 -m time --timestart 22:00 --timestop 23:00 --days Mon --datestart

2005:12:1:00:00 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 22 -j DROP
051201开始每个星期一的晚上10到11点从eth0可以连ssh

阅读(1366) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~