在 CentOS 系统中禁用 iptables 的方法取决于你使用的 CentOS 版本以及是否使用了 firewalld(CentOS 7 及以后默认使用 firewalld 作为防火墙管理工具,而 iptables 服务在 CentOS 7 中仍然存在但可能未启用)。以下是针对不同情况的详细步骤:
iptables 服务)CentOS 6 及之前的版本直接使用 iptables 服务管理防火墙规则,禁用步骤如下:
iptables 服务service iptables stop
iptables 开机自启chkconfig iptables off
service iptables status
# 或查看开机自启状态
chkconfig --list iptables
firewalld)CentOS 7 开始,默认防火墙管理工具是 firewalld,但 iptables 可能被 firewalld 间接调用(通过 iptables 后端)。若需彻底禁用 iptables 相关功能,需同时处理 firewalld 和 iptables 服务:
firewalld(因为 firewalld 会管理 iptables 规则)systemctl stop firewalld
systemctl disable firewalld
iptables 服务并禁用(部分系统可能安装了 iptables-services)如果系统安装了 iptables-services(传统 iptables 服务包),需同时停止并禁用它:
# 停止 iptables 服务(若存在)
systemctl stop iptables
# 禁止开机自启(若存在)
systemctl disable iptables
# 检查 firewalld 状态
systemctl status firewalld
# 检查 iptables 服务状态(若存在)
systemctl status iptables
iptables 规则(不停止服务)若仅需临时清除当前 iptables 规则(而非停止服务),可执行以下命令(重启后会恢复,除非保存规则):
# 清除 filter 表规则(默认表)
iptables -F
# 清除 nat 表规则(若有)
iptables -t nat -F
# 清除 mangle 表规则(若有)
iptables -t mangle -F
# 清除 raw 表规则(若有)
iptables -t raw -F
# 清除所有自定义链
iptables -X
# 设置默认策略为 ACCEPT(允许所有流量)
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
注意:若需永久清除规则,需结合停止服务或删除规则文件(如
/etc/sysconfig/iptables,CentOS 6 中规则保存位置)。
iptables 规则默认保存在 /etc/sysconfig/iptables,若需永久删除规则,需删除该文件或执行 service iptables save(但停止服务后保存无意义)。firewalld 与 iptables 冲突:CentOS 7 中不要同时启用 firewalld 和 iptables 服务,会导致规则冲突。iptables 服务。firewalld,若存在 iptables 服务也一并禁用。iptables -F 等命令,但重启后可能恢复。根据你的 CentOS 版本选择对应方法即可。