Linux系统中网络异常可能由防火墙阻断端口引起,需先检查firewalld、ufw或iptables状态,再分别停止并禁用对应服务,最后验证端口监听与连接是否恢复正常。

如果您在Linux系统中遇到网络连接异常或服务无法访问的问题,可能是防火墙正在运行并阻止了相关端口的通信。以下是查看及停止防火墙服务的具体操作步骤:
一、检查防火墙服务状态
在执行关闭操作前,需先确认当前系统使用的防火墙服务类型及其运行状态,不同发行版默认防火墙服务不同:CentOS 7/8、RHEL 7+ 默认使用 firewalld;Ubuntu/Debian 系统多使用 ufw;部分精简系统可能直接运行 iptables 服务。
1、执行命令查看 firewalld 是否启用:sudo systemctl is-active firewalld
2、执行命令查看 ufw 状态:sudo ufw status verbose
3、执行命令检查 iptables 服务是否运行:sudo systemctl is-active iptables(若提示 unit not found,则该服务未安装或未启用)
二、停止并禁用 firewalld 服务
firewalld 是基于 D-Bus 的动态防火墙管理器,停止服务需同时禁止开机自启,防止重启后自动恢复运行。
1、立即停止 firewalld 服务:sudo systemctl stop firewalld
2、禁止 firewalld 开机自启动:sudo systemctl disable firewalld
3、验证服务已停用:sudo systemctl is-active firewalld(返回 inactive 表示成功)
三、停止并禁用 ufw 防火墙
ufw(Uncomplicated Firewall)是 Ubuntu/Debian 系统默认的前端工具,其底层仍调用 iptables,关闭 ufw 即可解除所有规则限制。
1、关闭 ufw 防火墙:sudo ufw disable
2、确认状态输出为 Inactive:sudo ufw status
3、如需彻底卸载 ufw(非必需):sudo apt remove --purge ufw
四、清空并停用 iptables 规则
iptables 是内核级包过滤框架,若系统未使用 firewalld 或 ufw,而是直接配置 iptables,则需手动清空规则并停止相关服务。
1、清空所有链的规则:sudo iptables -F
2、将默认策略设为 ACCEPT:sudo iptables -P INPUT ACCEPT && sudo iptables -P FORWARD ACCEPT && sudo iptables -P OUTPUT ACCEPT
3、保存当前空规则(如已安装 iptables-services):sudo service iptables save 或 sudo iptables-save > /etc/sysconfig/iptables
4、停止 iptables 服务:sudo systemctl stop iptables
五、验证防火墙已关闭
无论采用哪种方式关闭防火墙,最终均需通过端口监听与连接测试确认拦截行为已终止,避免因服务残留导致误判。
1、检查 22(SSH)、80(HTTP)等常用端口是否处于 LISTEN 状态:sudo ss -tuln | grep ':22\|:80'
2、从另一台机器尝试 telnet 或 curl 访问目标端口:telnet 服务器IP 22
3、确认无 DROP 或 REJECT 日志输出:sudo dmesg | grep -i 'drop\|reject'










