信息发布→ 登录 注册 退出

LINUX怎么清空iptables的所有规则_LINUX防火墙规则清除命令

发布时间:2025-11-12

点击量:
清空iptables规则需执行-F和-X命令清除各表规则并删除自定义链,再将默认策略设为ACCEPT,最后保存空规则防止重启后恢复,确保系统防火墙处于开放状态。

清空Linux系统中iptables的所有规则,可以通过以下命令操作。这些方法适用于使用iptables作为防火墙管理工具的传统Linux发行版(如CentOS 6、Debian、Ubuntu等)。

1. 清空所有规则链

执行以下命令可清除所有表中的规则:

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t raw -F
iptables -t raw -X

说明:

  • -F:清空指定表的规则链。
  • -X:删除用户自定义的链。
  • 依次对filter、nat、mangle、raw等表进行清空,确保所有规则都被清除。

2. 设置默认策略为 ACCEPT

清空规则后,建议将默认策略改为允许,避免断开连接:

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

说明:

  • 如果不清除默认策略,即使规则为空,但默认策略是DROP或REJECT,可能导致网络不通。
  • 此步骤确保在无规则时流量能正常通过。

3. 一键清除脚本(推荐)

可将以下命令组合成一个脚本运行:

#!/bin/bash
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t raw -F
iptables -t raw -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

保存为 clear-iptables.sh,加执行权限并运行:

chmod +x clear-iptables.sh
sudo ./clear-iptables.sh

4. 持久化规则(防止重启后恢复)

某些系统会在重启后自动加载旧规则。需清除持久化配置:

  • Debian/Ubuntu:iptables-save > /etc/iptables/rules.v4(保存空规则)
  • CentOS/RHEL 6:service iptables save(保存当前状态,即空规则)
  • 使用firewalld的系统:应使用 firewall-cmd 命令管理,而非直接操作iptables。

基本上就这些。执行完上述命令后,iptables规则将被完全清空,系统进入“开放”状态,适合调试或重新配置防火墙规则。注意在远程操作时谨慎使用,避免锁死自己。

标签:# 清空  # 而非  # 可将  # 将被  # 可以通过  # 会在  # 适用于  # 设为  # 自定义  # 重启  # linux  # debian  # input  # Filter  # bash  # linux系统  # 工具  # ubuntu  # 防火墙  # centos  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!