首页主机资讯centos防火墙规则如何清理

centos防火墙规则如何清理

时间2025-11-14 13:11:03发布访客分类主机资讯浏览209
导读:CentOS 防火墙规则清理指南 一、先确认使用的防火墙组件 查看 firewalld 状态:sudo firewall-cmd --state(返回 running/not running);或 sudo systemctl statu...

CentOS 防火墙规则清理指南

一、先确认使用的防火墙组件

  • 查看 firewalld 状态:sudo firewall-cmd --state(返回 running/not running);或 sudo systemctl status firewalld
  • 查看 iptables 规则:sudo iptables -L -n -v
  • 注意:CentOS 7/8/9 默认使用 firewalld,但系统里可能同时存在 iptables 规则;仅清理其中一个,另一个仍可能生效。

二、使用 firewalld 时的清理步骤

  • 查看状态与区域配置:sudo firewall-cmd --statesudo firewall-cmd --list-all
  • 清空当前运行时规则:sudo firewall-cmd --flush-rules(仅影响运行时,重启后若配置了持久化会恢复)。
  • 移除持久化配置并重新加载:
    • 列出并删除已添加的端口/服务(示例):
      • sudo firewall-cmd --permanent --list-ports
      • sudo firewall-cmd --permanent --remove-port=80/tcp
      • sudo firewall-cmd --permanent --list-services
      • sudo firewall-cmd --permanent --remove-service=ssh
    • 重新加载:sudo firewall-cmd --reload
  • 如需彻底停止并禁用:
    • 停止:sudo systemctl stop firewalld
    • 禁用开机自启:sudo systemctl disable firewalld
  • 验证:sudo firewall-cmd --list-all 应显示规则为空或仅保留默认配置。

三、使用 iptables 时的清理步骤

  • 查看规则与默认策略:sudo iptables -L -n -v,确认 INPUT/FORWARDpolicy(如 DROP/ACCEPT)。
  • 安全清空顺序(避免会话瞬间中断):
    1. 先将默认策略改为 ACCEPT
      • sudo iptables -P INPUT ACCEPT
      • sudo iptables -P FORWARD ACCEPT
      • sudo iptables -P OUTPUT ACCEPT
    2. 清空规则与自定义链:
      • sudo iptables -F(清空规则)
      • sudo iptables -X(删除自定义链)
      • sudo iptables -Z(清零计数器)
  • 如需重置到默认表(可选):sudo iptables -t nat -Fsudo iptables -t mangle -F
  • 重要提醒:若默认策略为 DROP,直接执行 iptables -F 可能导致现有 SSH 会话断开且无法重连,务必先放行 22/TCP 或按上述顺序操作。

四、持久化与注意事项

  • firewalld:使用 --permanent 添加/删除规则后必须执行 sudo firewall-cmd --reload 才会生效;完全清理时应同时处理运行时与持久化配置。
  • iptables:规则默认重启后丢失。如需持久化,请按系统采用的方式保存(例如 CentOS 6 可用 service iptables save;不同发行版/版本路径与工具可能不同)。
  • 共存情况:即便停止或禁用 firewalldiptables 仍可能拦截流量;反之亦然。务必同时检查并清理两者。
  • 远程操作建议:通过 控制台/带外管理 执行清理,或先放行 SSH 22/TCP,再逐步清理,避免被锁死。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: centos防火墙规则如何清理
本文地址: https://pptw.com/jishu/747952.html
centos backlog如何维护 centos backlog如何提升

游客 回复需填写必要信息