1.4. 使用 CLI 控制端口
端口是可让操作系统接收和区分网络流量并将其转发到系统服务的逻辑设备。它们通常由侦听端口的守护进程来表示,它会等待到达这个端口的任何流量。
通常,系统服务侦听为它们保留的标准端口。例如, httpd 守护进程侦听端口 80。但默认情况下,系统管理员会将守护进程配置为在不同端口上侦听以便增强安全性或出于其他原因。
1.4.1. 打开端口
通过打开端口,系统可从外部访问,这代表了安全风险。通常,让端口保持关闭,且只在某些服务需要时才打开。
要获得当前区的打开端口列表:
列出所有允许的端口:
[root@kittod ~]# firewall-cmd --list-ports
在允许的端口中添加一个端口,以便为入站流量打开这个端口:
[root@kittod ~]# firewall-cmd --add-port=9092/tcp --permanent
success
[root@kittod ~]# firewall-cmd --reload
success
[root@kittod ~]# firewall-cmd --list-ports
9092/tcp
端口类型可以是 tcp 、 udp 、 sctp 或 dccp 。这个类型必须与网络通信的类型匹配。
1.4.2. 关闭端口
当不再需要开放端口时,在 firewalld 中闭该端口。强烈建议您尽快关闭所有不必要的端口,因为端口处于打开状态会存在安全隐患。
流程
要关闭某个端口,请将其从允许的端口列表中删除:
[root@kittod ~]# firewall-cmd --remove-port=9092/tcp --permanent
success
[root@kittod ~]# firewall-cmd --reload
success
[root@kittod ~]# firewall-cmd --list-ports
1.5 使用firewalld区
zones 代表一种更透明管理传入流量的概念。这些区域连接到联网接口或者分配一系列源地址。您可以
独立为每个区管理防火墙规则,这样就可以定义复杂的防火墙设置并将其应用到流量。
1.5.1. 列出区域
这个步骤描述了如何使用命令行列出区。
流程
查看系统中有哪些可用区:
[root@kittod ~]# firewall-cmd --get-zones
block dmz drop external home internal nm-shared public trusted work
查看所有区的详细信息:
[root@kittod ~]# firewall-cmd --list-all-zones
block
target: %%REJECT%%
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
dmz
target: default
icmp-block-inversion: no
······
查看特定区的详细信息:
[root@kittod ~]# firewall-cmd --zone=work --list-all
work (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client mysql ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
1.5.2. 更改特定区的 firewalld 设置
要在不同的区中工作,使用 --zone=zone-name 选项。例如,允许在区 work 中使用 httptest 服务:
[root@kittod ~]# firewall-cmd --add-service=httptest --zone=work
success
[root@kittod ~]# firewall-cmd --zone=work --list-all
work (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client httptest mysql ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
1.5.3. 更改默认区
系统管理员在其配置文件中为网络接口分配区域。如果接口没有被分配给指定区,它将被分配给默认区。每次重启 firewalld 服务后, firewalld 加载默认区的设置并使其活跃。
设置默认区:
显示当前的默认区:
[root@kittod ~]# firewall-cmd --get-default-zone
public
设置新的默认区:
[root@kittod ~]# firewall-cmd --set-default-zone=work
success
[root@kittod ~]# firewall-cmd --get-default-zone
work
注意
在此流程后,设置是一个永久设置,即使没有 --permanent 选项。
1.5.4. 将网络接口分配给区
可以为不同区定义不同的规则集,然后通过更改所使用的接口的区来快速改变设置。使用多个接口,可以为每个具体区设置一个区来区分通过它们的网络流量。
要将区分配给特定的接口:
列出活跃区以及分配给它们的接口:
[root@kittod ~]# firewall-cmd --get-active-zones
internal
interfaces: ens224 ens192
work
interfaces: ens160
为不同的区分配接口:
[root@kittod ~]# firewall-cmd --zone=internal --change-interface=ens192 --permanent success
[root@kittod ~]# firewall-cmd --get-active-zones
internal
interfaces: ens224 ens192
work
interfaces: ens160
1.5.5. 使用 nmcli 为连接分配区域
这个步骤描述了如何使用 nmcli 工具在 NetworkManager 连接中添加 firewalld 区。
为 NetworkManager 连接配置集分配区域:
[root@kittod ~]# nmcli connection modify ens160 connection.zone public
[root@kittod ~]# firewall-cmd --get-active-zones
internal
interfaces: ens224 ens192
public
interfaces: ens160
1.5.7. 创建一个新区
要使用自定义区,创建一个新的区并使用它像预定义区一样。新区需要 --permanent 选项,否则命令无法正常工作。
创建一个新区testzone:
[root@kittod ~]# firewall-cmd --new-zone=testzone --permanent
success
[root@kittod ~]# firewall-cmd --get-zones
block dmz drop external home internal libvirt nm-shared public testzone02 trusted work yjh
[root@kittod ~]# firewall-cmd --reload
success
[root@kittod ~]# firewall-cmd --get-zones
block dmz drop external home internal libvirt nm-shared public testzone testzone02 trusted work yjh
1.5.8. 区配置文件
区也可以通过区配置文件创建。如果您需要创建新区,但想从不同区重复使用设置,这种方法就很有用了。
firewalld 区配置文件包含区的信息。这些区描述、服务、端口、协议、icmp-blocks、
masquerade、forward-ports 和丰富的语言规则采用 XML 文件格式。文件名必须是 zone- name.xml ,其中 zone-name 的长度限制为 17 个字符。区配置文件位于
/usr/lib/firewalld/zones/ 和 /etc/firewalld/zones/ 目录中。
以下示例显示了允许一个服务( SSH )和一个端口范围的配置,适用于 TCP 和 UDP 协议:
[root@kittod ~]# cd /etc/firewalld/zones/
[root@kittod zones]# cp public.xml testzone
[root@kittod zones]# cp public.xml testzone02.xml
[root@kittod zones]# firewall-cmd --get-zones
block dmz drop external home internal libvirt nm-shared public testzone testzone02 trusted work yjh
[root@kittod zones]# firewall-cmd --reload
success
[root@kittod zones]# firewall-cmd --get-zones
block dmz drop external home internal libvirt nm-shared public testzone testzone02 trusted work yjh