CentOS系统中防火墙管理主要依赖firewalld(动态防火墙工具,推荐用于CentOS 7及以上版本)和iptables(传统静态防火墙工具,逐渐被firewalld替代)。以下是两者的具体更新/管理方法:
firewalld的“更新”主要包括软件包升级和配置/规则更新两部分,其中软件包升级通过系统包管理器完成,配置更新则通过命令动态调整。
sudo cp -rf /etc/firewalld /etc/firewalld.bak
yum(CentOS 7)或dnf(CentOS 8/9)更新系统,firewalld会随系统包自动升级:# CentOS 7
sudo yum update -y
# CentOS 8/9
sudo dnf update -y
firewall-cmd --version
sudo systemctl restart firewalld
sudo firewall-cmd --reload
firewalld的规则无需手动“更新版本”,而是通过命令动态调整,修改后会自动保存(默认路径:/etc/firewalld/zones/)。常见操作包括:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
iptables2firewalld工具(需提前安装),将现有iptables规则转换为firewalld配置:sudo yum install iptables2firewalld -y
sudo iptables2firewalld
iptables是Linux内核的防火墙模块,其“更新”主要涉及内核模块升级和规则管理,但CentOS 7及以上版本推荐使用firewalld替代。
iptables版本与内核绑定,升级iptables需先升级系统内核:
sudo iptables-save > /etc/sysconfig/iptables.bak
yum或dnf更新系统,iptables会随内核升级:# CentOS 7
sudo yum update kernel iptables -y
# CentOS 8/9
sudo dnf update kernel iptables -y
sudo reboot
iptables --version
iptables规则需手动管理,修改后需保存才能持久化(避免重启丢失):
sudo iptables -L -n -v
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -D INPUT 1
sudo service iptables save # 或使用 iptables-save > /etc/sysconfig/iptables
sudo systemctl restart iptables
以上方法覆盖了CentOS系统中firewalld和iptables的主要更新场景,可根据系统版本和需求选择合适的方式。