解决 Linux curl 报错 curl 7 Failed to connect Connection refused 端口连接拒绝修复
排查执行 curl 抛出 curl: (7) Failed to connect to ... port 8080: Connection refused,排查进程端口监听与 localhost 绑定。
问题现象
在终端尝试连接目标服务时命令瞬间返回错误:
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
触发根因
- 目标端口上没有任何应用程序处于监听(LISTEN)状态:服务进程根本未启动或在启动阶段崩溃退出;
- 目标服务仅监听在 IPv6 回环地址(
::1)而 curl 默认尝试连接 IPv4(127.0.0.1); - 服务监听地址绑定为
127.0.0.1而客户端从外部物理网卡 IP 发起访问。
解决步骤
1. 使用 ss 或 lsof 检查端口真实监听状态
在目标主机执行检查:
sudo ss -tulpn | grep 8080
查看进程是否存在,以及绑定的具体 IP 是 127.0.0.1:8080、:::8080 还是通配的 0.0.0.0:8080。
2. 将服务监听绑定地址调整为通配 0.0.0.0
确保服务可以接收来自所有网卡接口的连接:
在配置文件中(如 Spring Boot application.yml 或 Node.js):
server:
address: 0.0.0.0
port: 8080
3. 针对 IPv6 监听环境使用 -4 或 -6 显式指定
curl -4 http://localhost:8080/
验证确认
启动服务后重新触发探活:
curl -I http://localhost:8080/
控制台成功打印目标应用的 HTTP 返回头,连接顺畅建立。


