在 CentOS 中,“替换防火墙”通常指从默认的 firewalld 切换到 iptables,或者反过来。下面按常见场景给你完整的操作说明,先确认版本再操作。
firewalldiptables(已停止维护,不建议使用)✅ 下面以 CentOS 7+ 为例。
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl start iptables
systemctl enable iptables
iptables -L -n
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
service iptables save
# 或
iptables-save > /etc/sysconfig/iptables
systemctl stop iptables
systemctl disable iptables
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
CentOS 8 默认:
firewalld ✅iptables ❌(已废弃,使用 nftables)CentOS 8 如果要使用 iptables:
yum install -y iptables-services
但不推荐。
CentOS 8+ 实际底层是:
firewalld → nftables
查看当前防火墙后端:
firewall-cmd --get-backend
| 场景 | 推荐 |
|---|---|
| 新手 / 云服务器 | firewalld |
| 老系统 / 兼容脚本 | iptables |
| 新系统 | firewalld(nftables) |
❌ 同时启用 firewalld + iptables
❌ 修改规则后忘记保存
❌ 误关 22 端口导致 SSH 断开
如果你愿意,可以告诉我:
我可以给你精确的一键配置命令。