WenRou's Blog
iptables规则的查看、添加、删除和修改
2014-12-28 温柔哥
22号主机重启后也不能访问,但是ssh可以登录,一半应该是防火墙的问题。如果你先麻烦,直接清除所有规则即可,命令是  iptables -F

一下子就恢复了访问。

但是有时候我可能已经有了其他规则,只是需要修改其中的一条或者几条,那该如何办呢?





这里只列出比较常用的参数,详细的请查看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



总结:

语法是: iptables -D chain rulenum [options]

    其中: chain 是链的意思,就是INPUT FORWARD 之类的定语

    rulenum 是该条规则的编号。从1开始。可以使用iptables -L INPUT --line-numbers列出指定的链的规则的编号来。



    所以,例如上面要删除一个INPUT链的规则的话可以这样:

    iptables -D INPUT 3

    意思是删除第3条规则。



    还有第二种方法

    第二种办法是 -A 命令的映射,不过用-D替换-A。当你的链中规则很复杂,而你不想计算它们的编号的时候这就十分有用了。

    也就是说,你如何一开始时用iptables -A.... 语句定义了一个规则,那么删除此条规则时直接用 -D 来代替- A, 其余的都不变即可,而不需要什么编号了。



    ======================



    说一下上面的 --line-numbers 选项,如下面的命令:

iptables -L INPUT --line-numbers  列出INPUT 链所有的规则

num  target    prot opt source              destination        

1    REJECT    tcp  --  anywhere            anywhere            tcp dpt:microsoft-ds reject-with icmp-port-unreachable 

2    REJECT    tcp  --  anywhere            anywhere            tcp dpt:135 reject-with icmp-port-unreachable 

3    REJECT    tcp  --  anywhere            anywhere            tcp dpt:netbios-ssn reject-with icmp-port-unreachable 

4    REJECT    udp  --  anywhere            anywhere            udp dpt:microsoft-ds reject-with icmp-port-unreachable 

5    REJECT    udp  --  anywhere            anywhere            udp dpt:135 reject-with icmp-port-unreachable 

...

...

    删除指定的第4行规则:iptables -D INPUT 4

[root@tp ~]# iptables -F        清除预设表filter中的所有规则链的规则

[root@tp ~]# iptables -X        清除预设表filter中使用者自定链中的规则





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。
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容