linux系统中firewalld防火墙管理工具firewall-cmd(CLI命令行)

linux系统中firewalld防火墙拥有基于CLI(命令行界面)和基于GUI(图形用户界面)两种管理方式

相较于传统的防火墙管理配置工具,firewalld支持动态更新技术并加入了区域(zone)的概念。简单来说,区域就是firewalld预先准备了几套防火墙策略集合(策略模板)

用户可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换。

firewalld中常见的区域名称(默认为public)以及相应的策略规划如下图:(linux就该这么学p160)

 

终端管理工具CLI(命令行界面)

firewall-cmd是firewalld防火墙配置管理工具的CLI(命令行界面)版本。firewall-cmd命令的参数一般都是长格式的。

firewall-cmd命令中使用的参数以及作用

 

 

        与linux系统中其他的防火墙策略配置工具一样,使用firewalld配置的防火墙策略默认为运行时模式,又称为当前生效模式,而且随着系统的重启会失效。如果想让配置策略一致存在,就需要使用永久模式了。

方法就是在用firewall-cmd命令正常设置防火墙策略是添加 --permanent参数,这样配置的防火墙策略就可以永久生效了。但是,永久生效模式有一个不近人情的特点,就是使用它设置的策略只有在系统重启

之后才能自动生效。如果想让配置的策略立即生效,需要手动执行firewall-cmd  --reload命令。

 

1、查看firewalld服务当前所使用的区域:

[linuxprobe@linuxprobe Desktop]$ firewall-cmd --get-default-zone 
public

 

2、查询eno16777728网卡在firewalld服务中的区域:

[linuxprobe@linuxprobe Desktop]$ ifconfig | head -n 3  ## 查看网卡名称
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.13  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::20c:29ff:feab:7b00  prefixlen 64  scopeid 0x20<link>
[linuxprobe@linuxprobe Desktop]$ firewall-cmd --get-zone-of-interface=eno16777728  ## 查看网卡在firewalld服务中的区域
public

 

3、把firewalld服务中eno16777728网卡的默认区域修改为external,并在系统重启后生效

[root@linuxprobe ~]# firewall-cmd --get-zone-of-interface=eno16777728  ## 查看网卡在firewalld服务中的区域
public
[root@linuxprobe ~]# firewall-cmd --permanent --zone=external --change-interface=eno16777728  ##将网卡的默认区域永久修改为external,但是不能立即生效(需要重启系统生效,或者firewall-cmd --reload)
success
[root@linuxprobe ~]# firewall-cmd --get-zone-of-interface=eno16777728  ## 查看网卡在firewalld服务中的区域,仍然为public
public
[root@linuxprobe ~]# firewall-cmd --get-zone-of-interface=eno16777728 --permanent ## 加--permanent,变为external(或者重启)
external

 

4、修改firewalld服务的默认区域

[root@linuxprobe ~]# firewall-cmd --get-default-zone ## 查看当前firewalld服务所在的区域
public
[root@linuxprobe ~]# firewall-cmd --set-default-zone=external   ## 设为external
success
[root@linuxprobe ~]# firewall-cmd --get-default-zone   ## 查看
external 
[root@linuxprobe ~]# firewall-cmd --set-default-zone=home  ## 设为home
success
[root@linuxprobe ~]# firewall-cmd --get-default-zone   ## 查看
home
[root@linuxprobe ~]# firewall-cmd --set-default-zone=trusted 
success
[root@linuxprobe ~]# firewall-cmd --get-default-zone 
trusted
[root@linuxprobe ~]# firewall-cmd --set-default-zone=drop 
success
[root@linuxprobe ~]# firewall-cmd --get-default-zone 
drop
[root@linuxprobe ~]# firewall-cmd --set-default-zone=public 
success
[root@linuxprobe ~]# firewall-cmd --get-default-zone 
public

 

5、启动/关闭firewalld防火墙服务的应急状况模式,阻断一切网络连接(当远程控制服务时慎用):

[root@linuxprobe ~]# firewall-cmd --panic-on  ## 开启
success
[root@linuxprobe ~]# firewall-cmd --panic-off  ## 关闭
success

 

6、查询public区域是否允许请求SSH和HTTPS协议的流量:

[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=ssh  ## 允许
yes
[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=https  ## 拒绝
no
[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=dhcpv6-client   ## 允许
yes
[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=ipp-client   ## 拒绝
no

 

7、把firewalld服务中请求HTTPS协议的流量设置为永久允许,并立即生效

[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=https ## 查看当前是否允许
no
[root@linuxprobe ~]# firewall-cmd --permanent --zone=public --add-service=https  ## 加--permanent设置为永久允许
success
[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=https  ## 查看,未立即生效
no
[root@linuxprobe ~]# firewall-cmd --reload   ## 执行reload
success
[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=https  ## 立即生效
yes

 

8、把firewalld服务中请求HTTP协议的流量设置为永久拒绝,并立即生效:

[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=http  ## 查看当前http在firewalld服务中的状态,为拒绝
no
[root@linuxprobe ~]# firewall-cmd --zone=public --add-service=http --permanent ## 设置为永久允许
success
[root@linuxprobe ~]# firewall-cmd --reload   ## 使其立即生效
success
[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=http  ## 查看是否生效,是
yes
[root@linuxprobe ~]# firewall-cmd --zone=public --remove-service=http --permanent  ## 设置为永久拒绝
success
[root@linuxprobe ~]# firewall-cmd --reload   ## 使其立即生效
success
[root@linuxprobe ~]# firewall-cmd --zone=public --query-service=http  ## 查看是否生效,是
no

 

9、把在firewalld服务中访问8080和8081端口的流量策略设置为允许,但仅限当前生效:

[root@linuxprobe ~]# firewall-cmd --zone=public --list-ports ## 列出当前的端口
[root@linuxprobe ~]# firewall-cmd --zone=public --add-port=8080-8081/tcp  ## 增加端口
success
[root@linuxprobe ~]# firewall-cmd --zone=public --list-ports   ## 列出端口
8080-8081/tcp

 

10、把原本访问本机888端口的流量转发到22端口,且要求当前和长期均有效:

测试修改前的情况:

[root@linuxprobe ~]# ifconfig | head -n 5  ## 查看当前主机的IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.13  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::20c:29ff:feab:7b00  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ab:7b:00  txqueuelen 1000  (Ethernet)
        RX packets 319  bytes 31578 (30.8 KiB)
[root@linuxprobe ~]# ifconfig | head -n 5  ## 登录另一台主机,并查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.14  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::d7fe:9dfc:42ec:c255  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ef:86:f2  txqueuelen 1000  (Ethernet)
        RX packets 562  bytes 68551 (66.9 KiB)
[root@linuxprobe ~]# ssh -p 888 192.168.3.13  ## 测试端口为888的远程控制情形,无法连接
ssh: connect to host 192.168.3.13 port 888: No route to host

执行修改:

[root@linuxprobe ~]# firewall-cmd --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.3.13 --permanent  ## 执行端口转移
success
[root@linuxprobe ~]# firewall-cmd --reload ## 重新加载,立即生效
success

测试修改效果:

[root@linuxprobe ~]# ifconfig | head -n 5 ## 登录另一台主机,并查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.14  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::d7fe:9dfc:42ec:c255  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ef:86:f2  txqueuelen 1000  (Ethernet)
        RX packets 650  bytes 76761 (74.9 KiB)
[root@linuxprobe ~]# ssh -p 888 192.168.3.13  ## 测试端口流量转移后远程控制,通过
root@192.168.3.13's password:
Last login: Fri Oct 30 22:01:12 2020

 

11、firewalld中的富规则表示更细致、更详细的防火墙策略配置,优先级在所有的防火墙策略中是最高的,比如我们可以在firewalld服务中配置一条富规则,使其拒绝192.168.3.0/24网段的所有用户访问本机的ssh服务(22端口):

测试修改前的ssh服务:

[root@linuxprobe ~]# ifconfig | head -n 5  ## 查看当前主机IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.13  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::20c:29ff:feab:7b00  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ab:7b:00  txqueuelen 1000  (Ethernet)
        RX packets 360  bytes 37008 (36.1 KiB)
[root@linuxprobe ~]# ifconfig | head -n 5  ## 登录另一台主机,查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.14  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::d7fe:9dfc:42ec:c255  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ef:86:f2  txqueuelen 1000  (Ethernet)
        RX packets 963  bytes 110176 (107.5 KiB)
[root@linuxprobe ~]# ssh 192.168.3.13  ## 测试ssh远程控制服务,通过
root@192.168.3.13's password:
Last login: Fri Oct 30 22:51:22 2020 from 192.168.3.13
[root@linuxprobe ~]# ifconfig | head -n 5  ## 登录另一台主机,并查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.8  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::54f8:bbf7:7760:3745  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8d:79:f8  txqueuelen 1000  (Ethernet)
        RX packets 226  bytes 211739 (206.7 KiB)
[root@linuxprobe ~]# ssh 192.168.3.13  ## 测试ssh远程控制服务,通过
root@192.168.3.13's password:
Last login: Fri Oct 30 22:52:04 2020 from 192.168.3.14

执行修改,测试192.168.10.0/24网段:

[root@linuxprobe ~]# firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.10.0/24" service name="ssh" reject"  ## 执行修改
success
[root@linuxprobe ~]# firewall-cmd --reload   ## 立即生效
success
[root@linuxprobe ~]# ifconfig | head -n 5 ## 登录另一主机,查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.14  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::d7fe:9dfc:42ec:c255  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ef:86:f2  txqueuelen 1000  (Ethernet)
        RX packets 1109  bytes 127776 (124.7 KiB)
[root@linuxprobe ~]# ssh 192.168.3.13 ## 测试远程服务,通过
root@192.168.3.13's password:
Last login: Fri Oct 30 22:53:55 2020 from 192.168.3.8
[root@linuxprobe ~]# ifconfig | head -n 5  ## 登录另一主机,查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.8  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::54f8:bbf7:7760:3745  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8d:79:f8  txqueuelen 1000  (Ethernet)
        RX packets 1875  bytes 2376094 (2.2 MiB)
[root@linuxprobe ~]# ssh 192.168.3.13  ##  测试远程服务,通过
root@192.168.3.13's password:
Last login: Fri Oct 30 23:00:27 2020 from 192.168.3.14

 执行修改,测试192.168.3.0/24网段:

[root@linuxprobe ~]# firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.3.0/24" service name="ssh" reject"  ## 执行修改
success
[root@linuxprobe ~]# firewall-cmd --reload   ## 立即生效
success
[root@linuxprobe ~]# ifconfig | head -n 5  ## 登录另一主机,查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.14  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::d7fe:9dfc:42ec:c255  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ef:86:f2  txqueuelen 1000  (Ethernet)
        RX packets 1300  bytes 152392 (148.8 KiB)
[root@linuxprobe ~]# ssh 192.168.3.13  ## 测试ssh远程服务,拒绝
ssh: connect to host 192.168.3.13 port 22: Connection refused
[root@linuxprobe ~]# ifconfig | head -n 5  ## 登录另一主机,查看IP
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.8  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::54f8:bbf7:7760:3745  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:8d:79:f8  txqueuelen 1000  (Ethernet)
        RX packets 1990  bytes 2390926 (2.2 MiB)
[root@linuxprobe ~]# ssh 192.168.3.13  ## 测试ssh远程服务,拒绝
ssh: connect to host 192.168.3.13 port 22: Connection refused

 

posted @ 2020-10-30 18:06  小鲨鱼2018  阅读(1130)  评论(0编辑  收藏  举报