回忆之城
生命在于折腾
posts - 575,comments - 9,trackbacks - 0

这里只列出比较常用的参数,详细的请查看man iptables

1、查看
iptables -nvL –line-number

-L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数
-n 不对ip地址进行反查,加上这个参数显示速度会快很多
-v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口
–line-number 显示规则的序列号,这个参数在删除或修改规则时会用到

2、添加
添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。

当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.4          0.0.0.0/0

添加一条规则到尾部:

[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

再插入一条规则到第三行,将行数直接写到规则链的后面:

[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

查看:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.3          0.0.0.0/0
4    DROP       all  --  192.168.1.4          0.0.0.0/0
5    DROP       all  --  192.168.1.5          0.0.0.0/0

可以看到192.168.1.3插入到第三行,而原来的第三行192.168.1.4变成了第四行。

3、删除
删除用-D参数

删除之前添加的规则(iptables -A INPUT -s 192.168.1.5 -j DROP):

[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP

有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number找出该条规则的行号,再通过行号删除规则。

[root@test ~]# iptables -nv --line-number
iptables v1.4.7: no command specified
Try `iptables -h' or 'iptables --help' for more information.
[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.3          0.0.0.0/0

删除第二行规则

[root@test ~]# iptables -D INPUT 2

4、修改
修改使用-R参数

先看下当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    DROP       all  --  192.168.1.5          0.0.0.0/0

将第三条规则改为ACCEPT:

[root@test ~]# iptables -R INPUT 3 -j ACCEPT

再查看下:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.1.1          0.0.0.0/0
2    DROP       all  --  192.168.1.2          0.0.0.0/0
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

第三条规则的target已改为ACCEPT。


http://ns.35.com/?p=211

修复后的规则列表:
Chain RH-Firewall-1-INPUT (2 references)target prot opt source destination
 ACCEPT all -- anywhere anywhere
 ACCEPT icmp -- anywhere anywhere icmp any
 ACCEPT esp -- anywhere anywhere
 ACCEPT ah -- anywhere anywhere
 ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
 ACCEPT udp -- anywhere anywhere udp dpt:ipp
 ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:https
 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

INPUT链是系统默认带的链,RH-Firewall-1-INPUT是系统管理员自己定义添加的链。INPUT链一般是父链,像RH-Firewall-1-INPUT这样自定义的链都是一些特定功能的链,可以加入到主链INPUT中。

如:
分别有RH-Firewall-1-INPUT和RH-IDS-1-INPUT两个链,分别完成防火墙和入侵检测功能,先将两个链加入到主链INPUT中。
iptables -A INPUT -j RH-Firewall-1-INPUT
iptables -A INPUT -j RH-IDS-1-INPUT

这样报文会先进入INPUT链,再由INPUT链进入RH-Firewall-1-INPUT处理,最后进入RH-IDS-1-INPUT链处理。

从规则列表中看,RH-Firewall-1-INPUT链的功能是用于防护系统本身的防火墙策略,本系统可以接收所有IP包、icmp包、esp包、ah包、udp的组播包,访问ipp服务的包、开启了状态检测功能,开放ssh、https、http三个服务。


 

posted on 2014-03-17 17:47 回忆之城 阅读(3977) 评论(0)  编辑 收藏 引用 所属分类: unix/linux
只有注册用户登录后才能发表评论。