Linux防火墙规则的查看、添加、删除和修改

简介: 这里只列出比较常用的参数,详细的请查看man iptables   1、查看   iptables -nvL –line-number -L查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数。

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

如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
目录
相关文章
|
3月前
|
Linux 网络安全
Linux - firewall-cmd 命令添加端口规则不生效排查
Linux - firewall-cmd 命令添加端口规则不生效排查
156 0
|
1月前
|
存储 安全 网络协议
使用 firewall-cmd 管理 Linux 防火墙端口
本文将介绍如何使用 firewall-cmd 工具在 Linux 系统中进行简单端口管理,包括开放、查询、关闭等操作。通过实例展示相关命令的用法,希望能对大家有所帮助。
|
1月前
|
资源调度 算法 Linux
Linux进程/线程的调度机制介绍:详细解析Linux系统中进程/线程的调度优先级规则
Linux进程/线程的调度机制介绍:详细解析Linux系统中进程/线程的调度优先级规则
64 0
|
7天前
|
Linux 网络安全
linux如何关闭防火墙
记住,关闭防火墙可能会降低系统安全性,请在真正需要的情况下关闭,并确保你在网络环境中采取其他安全措施。
15 0
|
1月前
|
运维 Linux 应用服务中间件
Centos7如何配置firewalld防火墙规则
Centos7如何配置firewalld防火墙规则
44 0
|
3月前
|
监控 网络协议 Linux
防火墙规则动态管理器 - firewalld
【1月更文挑战第11天】
51 0
|
4月前
|
网络协议 Linux 网络安全
linux服务器防火墙的开启及关闭
linux服务器防火墙的开启及关闭
210 1
|
4月前
|
安全 Linux 网络安全
linux的防火墙
Linux的防火墙是一种网络安全工具,用于保护Linux系统免受网络攻击。
64 0
|
4月前
|
存储 网络协议 Linux
Linux加强篇008-使用Iptables与Firewalld防火墙
山重水复疑无路,柳暗花明又一村
309 0
Linux加强篇008-使用Iptables与Firewalld防火墙
|
3月前
|
监控 网络协议 安全
Linux中防火墙的简单使用方法
防火墙是一种网络安全设备或软件,用于监控和控制网络流量,以保护计算机网络免受未经授权的访问、恶意攻击和未经授权的数据传输。防火墙通过筛选网络流量并根据预定义的规则集来允许或阻止特定类型的通信。
150 0