在 CentOS 系统中,使用 iptables
命令配置防火墙规则后,可以通过以下方法保存配置:
service
命令保存当前配置:
sudo service iptables save
重启 iptables
服务以应用保存的配置:
sudo service iptables restart
将当前的 iptables
规则导出到一个文件:
sudo iptables-save > /etc/sysconfig/iptables
确保 /etc/sysconfig/iptables
文件存在并且可写:
sudo touch /etc/sysconfig/iptables
sudo chmod 600 /etc/sysconfig/iptables
重启 iptables
服务以应用保存的配置:
sudo service iptables restart
firewall-cmd
(适用于 CentOS 7 及以上版本)如果你使用的是 CentOS 7 或更高版本,并且安装了 firewalld
,可以使用 firewall-cmd
来管理防火墙规则,并且它会自动保存配置。
添加规则:
sudo firewall-cmd --permanent --add-rule ipv4 filter INPUT 0 -p tcp --dport 80 -j ACCEPT
重新加载防火墙配置:
sudo firewall-cmd --reload
无论使用哪种方法保存配置,都可以通过以下命令查看当前的 iptables
规则,以确保配置已正确应用:
sudo iptables -L -n -v
或者对于 firewalld
:
sudo firewall-cmd --list-all
通过这些步骤,你可以确保在 CentOS 系统中配置的 iptables
规则在系统重启后仍然有效。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>