1.2. 查看当前状态和设置 firewalld
1.2.1. 查看当前状态 firewalld
防火墙服务 firewalld 被默认安装在系统中。使用 firewalld CLI 界面检查该服务是否正在运行。
[root@kittod ~]# firewall-cmd --state
running
有关服务状态的更多信息,请使用 systemctl status 子命令:
[root@kittod ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor pres>
Active: active (running) since Thu 2021-12-09 18:19:14 CST; 1min 9s ago
Docs: man:firewalld(1)
Main PID: 2023 (firewalld)
Tasks: 2 (limit: 23489)
Memory: 31.2M
CGroup: /system.slice/firewalld.service
└─2023 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --no>
Dec 09 18:19:14 kittod systemd[1]: Starting firewalld - dynamic firewall daemon...
Dec 09 18:19:14 kittod systemd[1]: Started firewalld - dynamic firewall daemon.
Dec 09 18:19:14 kittod firewalld[2023]: WARNING: AllowZoneDrifting is enabled. This>
1.2.3. 使用 CLI 查看 firewalld 设置
使用 CLI 客户端可能会对当前防火墙设置有不同的视图。 --list-all 选项显示 firewalld 设置的
完整概述。
firewalld 使用 zone 管理流量。如果 --zone 选项没有指定区,该命令将在分配给活跃网络接口和连
接的默认区里有效。
要列出默认区的所有相关信息:
[root@kittod ~]# firewall-cmd --list-all
work (active)
target: default
icmp-block-inversion: no
interfaces: ens160 ens192
sources:
services: cockpit dhcpv6-client mysql ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
要指定显示设置的区域,在 firewall-cmd --list-all 命令中添加 --zone=zone-name 参数,例如:
[root@kittod ~]# firewall-cmd --list-all --zone=home
home
target: default
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client mdns samba-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
查看当前区中允许哪些服务:
[root@kittod ~]# firewall-cmd --list-services
cockpit dhcpv6-client mysql ssh
1.3. 使用 firewalld
1.3.1. 使用 CLI 禁用紧急事件的所有流量
在紧急情况下,如系统攻击,可以禁用所有网络流量并关闭攻击者。
要立即禁用网络流量,请切换 panic 模式:
[root@kittod ~]# firewall-cmd --panic-on
重要
启用 panic 模式可停止所有网络流量。因此,它应只在对机器有物理访问权限或者使用串口控制台登录时才使用。
关闭 panic 模式会使防火墙恢复到其永久设置。要关闭 panic 模式,请输入:
[root@kittod ~]# firewall-cmd --panic-off
要查看是否打开或关闭 panic 模式,请使用:
[root@kittod ~]# firewall-cmd --query-panic
no
1.3.2. 使用 CLI 控制预定义服务的流量
控制流量的最简单方法是在 firewalld 中添加预定义服务。这会打开所有必需的端口并根据 服务定义文件 修改其他设置。
检查该服务是否还未被允许:
[root@kittod ~]# firewall-cmd --list-services
cockpit dhcpv6-client mysql ssh
列出所有预定义的服务:
[root@kittod ~]# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client amqp amqps apcupsd audit bacula bacula-client bb bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc bittorrent-lsd ceph ceph-mon cfengine cockpit condor-collector ctdb dhcp dhcpv6 dhcpv6-client distcc dns dns-over-tls docker-registry docker-swarm dropbox-lansync elasticsearch etcd-client etcd-server finger freeipa-4 freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git grafana gre high-availability http https httptest imap imaps ipp ipp-client ipsec irc ircs iscsi-target isns jenkins kadmin kdeconnect kerberos kibana klogin kpasswd kprop kshell kube-apiserver ldap ldaps libvirt libvirt-tls lightning-network llmnr managesieve matrix mdns memcache minidlna mongodb mosh mountd mqtt mqtt-tls ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp nut openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole plex pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy prometheus proxy-dhcp ptp pulseaudio puppetmaster quassel radius rdp redis redis-sentinel rpc-bind rsh rsyncd rtsp salt-master samba samba-client samba-dc sane sip sips slp smtp smtp-submission smtps snmp snmptrap spideroak-lansync spotify-sync squid ssdp ssh steam-streaming svdrp svn syncthing syncthing-gui synergy syslog syslog-tls telnet tentacle tftp tftp-client tile38 tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-http wbem-https wsman wsmans xdmcp xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server
在允许的服务中添加服务:
[root@kittod ~]# firewall-cmd --add-service=http
success
使新设置永久生效
[root@kittod ~]# firewall-cmd --runtime-to-permanent
success
1.3.3. 添加新服务
可使用工具 firewall-cmd 和 firewall-offline-cmd 添加和删除服务。或者,您可以编辑/etc/firewalld/services/ 中的 XML 文件。如果用户没有添加或更改服务,则在/etc/firewalld/services/ 中没有找到对应的 XML 文件。如果要添加或更改服
务, /usr/lib/firewalld/services/ 文件可作为模板使用。
注意
服务名称必须是字母数字,以及 _ (下划线)和 - (横线)字符。
运行以下命令以添加新和空服务:
[root@kittod ~]# firewall-cmd --new-service=httptest --permanent
success
更改服务设置后,就会将服务更新副本放入 /etc/firewalld/services/ 中。
[root@kittod ~]# ll /etc/firewalld/services/
total 4
-rw-r--r--. 1 root root 60 Nov 9 14:06 httptest.xml
你也可以直接使用命令复制/usr/lib/firewalld/services目录下的配置文件到/etc/firewalld/services/目录中进行配置。
firewalld 第一次从 /usr/lib/firewalld/services 加载文件。如果文件放在/etc/firewalld/services 中,且它们有效,就会覆盖 /usr/lib/firewalld/services 中匹配的文件。一旦删除了 /etc/firewalld/services 中的匹配文件,或者要求 firewalld 加载服务的默认值,则将使用 /usr/lib/firewalld/services 中的覆盖文件。这只适用于永久性环境。要在运行时环境中获取这些回退,则需要重新载入。