Linux防火墙如何更新版本升级
小樊
40
2026-01-01 02:51:53
Linux防火墙升级与更新操作指南
一、升级前的准备与版本确认
- 明确当前使用的防火墙栈:常见为 firewalld、iptables(RHEL/CentOS 6 常用)、或 ufw(Debian/Ubuntu 常用)。
- 查看版本与运行状态(示例):
- firewalld:执行 firewall-cmd --version 与 firewall-cmd --state;服务状态用 systemctl status firewalld。
- iptables:执行 iptables --version;服务状态用 systemctl status iptables(若安装了 iptables-services)。
- ufw:执行 ufw version;状态用 ufw status。
- 备份当前规则与配置:
- firewalld:规则通常无需单独备份,变更用 –permanent 写入,变更后用 firewall-cmd --reload 生效。
- iptables:建议备份 /etc/sysconfig/iptables(RHEL/CentOS 6/7 常见路径)后再升级。
- ufw:规则可用 ufw status numbered 导出清单,变更后 ufw reload 生效。
二、按发行版与防火墙组件的升级方法
- RHEL/CentOS 7(firewalld)
- 推荐通过系统仓库升级:执行 yum update firewalld(或 dnf update firewalld),随后 systemctl restart firewalld。
- 离线环境:在一台可联网机器下载同版本的 firewalld 及其依赖的 rpm 包,拷贝到目标机后执行 rpm -ivh 包名.rpm 安装;安装完成后 systemctl restart firewalld。
- RHEL/CentOS 7(切换为 iptables 并升级)
- 安装/升级组件:yum install -y iptables iptables-services;如需升级执行 yum update iptables。
- 切换与启用:停用 firewalld(见下文“切换栈注意事项”),然后 systemctl start iptables && systemctl enable iptables。
- RHEL/CentOS 8+/Fedora(iptables 与 firewalld)
- 使用 dnf 统一管理:dnf update firewalld 或 dnf update iptables-services;按需重启服务。
- Debian/Ubuntu(ufw 或 firewalld)
- ufw:apt update && apt install --only-upgrade ufw,变更后 ufw reload。
- firewalld(如已安装):apt update && apt install --only-upgrade firewalld,随后 systemctl restart firewalld。
三、升级后的验证与回滚
- 验证
- 版本:firewalld 用 firewall-cmd --version;iptables 用 iptables --version;ufw 用 ufw version。
- 运行状态:firewalld 用 firewall-cmd --state 与 systemctl status firewalld;iptables 用 systemctl status iptables;ufw 用 ufw status。
- 规则有效性:firewalld 用 firewall-cmd --list-all;iptables 用 iptables -L -n;ufw 用 ufw status verbose。
- 回滚
- 优先使用包管理器回滚:如 yum downgrade firewalld 或 dnf downgrade firewalld;Debian/Ubuntu 用 apt install firewalld=<版本>。
- 规则回滚:iptables 使用备份文件 /etc/sysconfig/iptables 执行恢复(如 iptables-restore < /etc/sysconfig/iptables);firewalld 因使用 –permanent 规则,回滚对应配置后 firewall-cmd --reload 即可。
四、切换防火墙栈的注意事项
- 从 firewalld 切换到 iptables(RHEL/CentOS 7)
- 先停止并禁用 firewalld:systemctl stop firewalld && systemctl disable firewalld。
- 安装并启用 iptables 服务:yum install -y iptables iptables-services && systemctl start iptables && systemctl enable iptables。
- 注意:切换会替换运行时栈,务必保留当前规则备份,并在维护窗口操作。
- 从 iptables 切换到 firewalld
- 停止并禁用 iptables:systemctl stop iptables && systemctl disable iptables。
- 启用 firewalld:systemctl start firewalld && systemctl enable firewalld,按需用 firewall-cmd 重建规则(优先使用 –permanent 并 –reload)。