发布于2026-08-18 阅读(0)
扫一扫,手机访问
要在firewalld中添加永久端口规则,必须先使用--permanent参数将其写入配置文件,然后再执行--reload命令加载到运行时环境中。否则,规则仅会被保存到配置文件中,但不会生效,一旦系统重启,这些规则仍会丢失。

加完规则后,如果不执行reload操作,规则是不会生效的;而加规则时若不加上 --permanent 参数,那么一旦重启,规则就会丢失。这可是大家在使用firewalld时最容易踩到的坑啦!要知道,firewalld的临时规则和永久规则是分别独立存储的,所以必须要明确地声明进行持久化操作才行哦。
firewall-cmd --state 返回 running 才能操作firewall-cmd --permanent --add-port=80/tcpfirewall-cmd --permanent --add-port=53/udpfirewall-cmd --reload,否则规则只存配置文件里,没进内存firewall-cmd --list-ports 或 firewall-cmd --list-alliptables 命令默认只改内存里的规则,系统一重启全清空。要持久化,得把当前规则导出到文件,并确保开机时自动恢复。
iptables-sa ve > /etc/sysconfig/iptablesiptables-sa ve > /etc/iptables/rules.v4cat /etc/sysconfig/iptables | head -n 5,确认有 *filter 和 COMMITiptables 服务(systemctl enable iptables),但注意:该服务在 firewalld 启用时会被禁用,二者不能共存/etc/sysconfig/iptables 文件手动改——容易格式错、缺 COMMIT、导致 restore 失败firewall-cmd --add-service=http 没反应没报错但端口不通,大概率是没指定 zone,或者 zone 绑定错了网卡。firewalld 规则永远绑定到具体 zone,而不是全局生效。
firewall-cmd --get-active-zones,看你的网卡(比如 ens33)绑在哪个 zone 下public 区域,就得加:firewall-cmd --zone=public --add-service=http --permanenthttp 是预定义 service,对应 TCP 80;自定义端口不能用 --add-service,只能用 --add-port--reload,且 --list-all 输出里要能看到 services: http 才算成功firewalld 没内置 undo,iptables 更没有。所谓“回滚”,其实是靠你提前留下的快照或备份配置。
firewall-cmd --list-all > /root/firewall-before-20260708.bak/etc/firewalld/ 下,重点备份:/etc/firewalld/zones/public.xml(或你用的 zone 文件)iptables-sa ve 导出的文件,替换后执行 iptables-restore < /path/to/backupsystemctl restart firewalld 能恢复——它只重载 --permanent 配置,不是撤销上一步操作--list-all 输出里的 “permanent: yes/no” 和 “active: yes/no”,能省掉大半排查时间。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
4
5
6
7
8
9