WSL中git clone无法下载项目,如何保证WSL能够访问windows主机的网络?一文详解
遇到问题如下:
(paddleOCREnv) tql@DESKTOP666:~$ git clone https://github.com/PaddlePaddle/PaddleOCR.git Cloning into 'PaddleOCR'... fatal: unable to access 'https://github.com/PaddlePaddle/PaddleOCR.git/': Failed to connect to 127.0.0.1 port 8890 after 0 ms: Connection refused
我的疑惑点是WSL2和Windows的.gitconfig文件内容一模一样,但是前者能够成功git clone, 后者却不能。
其实问题是出在 WSL2 的 Ubuntu 环境中执行 git clone 时,Git 尝试使用 .gitconfig 中配置的代理(http://127.0.0.1:8890),但连接被拒绝(Connection refused)。
这表明 WSL2 无法访问 Windows 主机上运行的代理服务(端口 8890)。以下是问题原因分析和解决方案:
- 代理配置不适用于 WSL2:
- 你在 Windows 的 .gitconfig 中配置了代理 http://127.0.0.1:8890,这在 Windows 的 CMD 环境中有效,因为代理服务(可能是一个本地代理软件)运行在 Windows 上,监听 127.0.0.1:8890。
- 但在 WSL2 中,127.0.0.1 指向 WSL2 的本地网络接口,而不是 Windows 主机的网络接口。WSL2 使用虚拟化网络,Windows 主机的 127.0.0.1 对于 WSL2 来说不可直接访问,因此连接到 127.0.0.1:8890 失败。
- WSL2 的网络隔离:
- WSL2 运行在 Hyper-V 虚拟机中,与 Windows 主机的网络栈隔离。Windows 主机上的代理服务无法直接通过 127.0.0.1 被 WSL2 访问。
- 需要使用 Windows 主机的实际 IP 地址(或 WSL2 特定的网络桥接方式)来访问 Windows 的代理服务。
- SSL 验证禁用:
- 你的 .gitconfig 中设置了 sslVerify = false,这在 Windows 中可能帮助绕过了某些 SSL/TLS 问题,但在 WSL2 中,问题主要出在代理连接,而不是 SSL 验证。
- 可能的代理服务问题:
- 如果代理服务(监听 8890 端口)未正确配置为允许 WSL2 的连接,或者代理服务本身未运行,也会导致 Connection refused 错误。
解决方案
获取 Windows 主机的 IP 地址
WSL2 无法直接通过 127.0.0.1 访问 Windows 主机的服务,需要使用 Windows 主机的实际 IP 地址。
1、在 WSL2 的 Ubuntu 中运行以下命令,获取 Windows 主机的 IP:
cat /etc/resolv.conf
查找类似 nameserver 的行,输出形如:
nameserver 172.29.64.1
这里的 172.29.64.1(你的 IP 可能不同)是 Windows 主机的 IP 地址。
2、修改 WSL2 的 .gitconfig 文件,将代理地址从 127.0.0.1 替换为 Windows 主机的 IP ( 172.29.64.1):
例如,将以下部分:
[http] proxy = http://127.0.0.1:8890 [https] proxy = http://127.0.0.1:8890
修改为:
[http] proxy = http://172.29.64.1:8890 [https] proxy = http://172.29.64.1:8890
保存并退出
3、再次测试克隆:
git clone https://github.com/PaddlePaddle/PaddleOCR.git
此时便可以成功完成项目的远程下载。

浙公网安备 33010602011771号