在 CentOS 7 及以后版本,系统默认使用的是 firewalld,而不是传统的 iptables 服务。如果你想“开启 iptables”,通常有两种情况,我分别说明。
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl start iptables
systemctl enable iptables
systemctl status iptables
或
iptables -L -n
✅ 到此,iptables 已成功开启。
CentOS 6 默认使用 iptables:
service iptables start
chkconfig iptables on
查看状态:
service iptables status
⚠️ 注意:firewalld 运行时,直接用
iptables命令修改的规则 重启后会丢失
如果你只是临时用:
iptables -L -n
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
但 不推荐 长期使用这种方式。
service iptables save
# 或
/usr/libexec/iptables/iptables.init save
规则会保存到:
/etc/sysconfig/iptables
| 场景 | 建议 |
|---|---|
| 新系统 | ✅ 使用 firewalld |
| 老项目 / 运维习惯 | ✅ 使用 iptables-services |
| Docker / K8s 节点 | ⚠️ 谨慎关闭 firewalld |
firewall-cmd --state
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
如果你愿意,可以告诉我:
cat /etc/centos-release)我可以给你一套 最合适的安全规则示例。