当前位置: 移动技术网 > 网络运营>服务器>Linux > linux防火墙iptables规则的查看、添加、删除和修改方法总结

linux防火墙iptables规则的查看、添加、删除和修改方法总结

2019年05月24日  | 移动技术网网络运营  | 我要评论

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。

5、永久生效

service iptables save

service iptables restart

以上就是本文介绍的对linux防火墙iptables规则进行查看、添加、删除和修改的操作

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网