原文链接:信创服务器上firewalld命令详解 | 统信 | 麒麟 | 中科方德
Hello,大家好啊!今天给大家带来一篇关于在信创服务器上使用firewalld命令的详解文章。firewalld是Linux系统中常用的防火墙管理工具,它提供了一种动态管理防火墙的方式,可以在不重启防火墙服务的情况下立即应用新的规则。本文将详细介绍firewalld的基本用法和常用命令,帮助大家更好地管理和配置防火墙。欢迎大家分享转发,点个关注和在看吧!
1.查看配置文件
firewalld 配置文件结构
firewalld 的配置文件主要存放在 /etc/firewalld 目录下。主要子目录和文件包括:
/etc/firewalld/zones/:存放各个区域的配置文件。
/etc/firewalld/services/:存放各个服务的 XML 配置文件。
/etc/firewalld/firewalld.conf:主配置文件。

2.服务启动与关闭
[root@pdsyw ~]# systemctl stop firewalld.service
[root@pdsyw ~]#
[root@pdsyw ~]# systemctl start firewalld.service
[root@pdsyw ~]#
[root@pdsyw ~]# systemctl restart firewalld.service
[root@pdsyw ~]#
[root@pdsyw ~]# systemctl disable firewalld.service
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
[root@pdsyw ~]#
[root@pdsyw ~]# systemctl enable firewalld.service
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
[root@pdsyw ~]#

3.查看当前防火墙状态
[root@pdsyw ~]# firewall-cmd --state
running
[root@pdsyw ~]#

4.查看当前区域
[root@pdsyw ~]# firewall-cmd --get-active-zones
public
interfaces: ens160
[root@pdsyw ~]#
[root@pdsyw ~]# firewall-cmd --set-default-zone=home
success
[root@pdsyw ~]#
[root@pdsyw ~]# firewall-cmd --get-active-zones
home
interfaces: ens160
[root@pdsyw ~]#

5.更改当前区域
[root@pdsyw ~]# firewall-cmd --set-default-zone=public
success
[root@pdsyw ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

6.查看特定区域的规则
[root@pdsyw ~]# firewall-cmd --zone=public --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client mdns samba ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks: echo-request
rich rules:
rule family="ipv4" source address="192.168.80.111" accept
[root@pdsyw ~]#

7.删除服务
[root@pdsyw ~]# firewall-cmd --zone=public --remove-service=http --permanent
success
[root@pdsyw ~]#
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

8.临时开放端口
[root@pdsyw ~]# firewall-cmd --zone=public --add-port=9999/tcp
success
[root@pdsyw ~]#

9.永久开放端口
[root@pdsyw ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

10.关闭端口
[root@pdsyw ~]# firewall-cmd --zone=public --remove-port=8080/tcp --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

11.添加 IP 地址限制
[root@pdsyw ~]# firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.80.111" accept' --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

12.添加 ICMP 规则
[root@pdsyw ~]# firewall-cmd --zone=public --add-icmp-block=echo-request --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

13.添加接口到区域
[root@pdsyw ~]# firewall-cmd --zone=public --change-interface=ens160 --permanent
The interface is under control of NetworkManager, setting zone to 'public'.
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

14.添加 Direct 规则
[root@pdsyw ~]# firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 8080 -j ACCEPT
success
[root@pdsyw ~]# firewall-cmd --direct --get-all-rules
ipv4 filter INPUT 0 -p tcp --dport 8080 -j ACCEPT
[root@pdsyw ~]#

15.应用场景-公共网络
[root@pdsyw ~]# firewall-cmd --zone=public --change-interface=ens170
success
[root@pdsyw ~]#
[root@pdsyw ~]# firewall-cmd --zone=public --add-service=ssh --permanent
Warning: ALREADY_ENABLED: ssh
success
[root@pdsyw ~]#
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

16.公共网络-家庭网络
[root@pdsyw ~]# firewall-cmd --zone=home --change-interface=ens180
success
[root@pdsyw ~]# firewall-cmd --zone=home --add-service=samba --permanent
Warning: ALREADY_ENABLED: samba
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

17.公共网络-工作网络
[root@pdsyw ~]# firewall-cmd --zone=work --change-interface=ens160
success
[root@pdsyw ~]# firewall-cmd --zone=work --add-service=https --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

18.启用日志记录
[root@pdsyw ~]# firewall-cmd --set-log-denied=all
[root@pdsyw ~]# tail -f /var/log/messages
19.创建自定义区域
[root@pdsyw ~]# firewall-cmd --permanent --new-zone=pdsyw
success
[root@pdsyw ~]# firewall-cmd --zone=pdsyw --add-interface=ens150 --permanent
success
[root@pdsyw ~]# firewall-cmd --zone=pdsyw --add-port=8888/tcp --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

20.检查并移除接口和服务
[root@pdsyw ~]# firewall-cmd --zone=pdsyw --list-all
pdsyw (active)
target: default
icmp-block-inversion: no
interfaces: ens150
sources:
services:
ports: 8888/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@pdsyw ~]# firewall-cmd --zone=pdsyw --remove-interface=ens150 --permanent
success
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]#

21.删除配置文件
[root@pdsyw ~]# rm /etc/firewalld/zones/pdsyw.xml
rm:是否删除普通文件 '/etc/firewalld/zones/pdsyw.xml'?y
[root@pdsyw ~]#
[root@pdsyw ~]# firewall-cmd --reload
success
[root@pdsyw ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
[root@pdsyw ~]#

通过本文的介绍,您应该已经掌握了在信创服务器上使用firewalld命令管理防火墙的基本方法和常用命令。firewalld提供了一种灵活且强大的方式来管理Linux系统的网络安全。如果您觉得这篇文章有用,请分享和转发。同时,别忘了点个关注和在看,以便未来获取更多实用的技术信息和解决方案。感谢大家的阅读,我们下次再见!