-
-
Save libChan/3a804a46b532cc326a2ee55b27e8ac19 to your computer and use it in GitHub Desktop.
| # WSL通过Win访问网络,所以WSL的网关指向的是Windows,DNS服务器指向的也是Windows,设置WSL的proxy为win的代理ip+端口即可 | |
| # WSL中的DNS server在/etc/resolv.conf中查看,该文件是由/etc/wsl.conf自动生成的。 | |
| # 如果关闭了wsl.conf中自动生成resolve.conf并自行修改了resolve.conf,DNS nameserver并不是本机win ip | |
| # 需要开启wsl.conf的自动生成,再运行以下命令 | |
| # https://zhuanlan.zhihu.com/p/153124468 | |
| # 添加到环境变量设置中,例如~/.zshrc | |
| export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*') | |
| export https_proxy="http://${hostip}:7890" | |
| export http_proxy="http://${hostip}:7890" |
cat /etc/resolv.conf | grep nameserve查看Wsl的网关(通常是在 Windows-WSL 2 这一体系中,Windows 的 IP 地址,即Wsl将Windows作为Dns服务器) ,是否与ipconfig命令获得的Ethernet adapter vEthernet (WSL)中的Windows IP地址一致,如果是其他,可能是resolv.conf文件中内容不一致,导致没办法正常提取到地址以下为我的解决方案
powershell.exe -Command "(ipconfig)"# 此命令的结果和在windows中 ipconfig结果是一样的 可以看到这里是有Ethernet adapter vEthernet (WSL),然后抓取地址powershell.exe -Command "(ipconfig)" | grep -a IPv4 | grep -a 17 | awk '{print $16}'# 我这里是16,可能需要修改shell脚本中:
hostip=$(powershell.exe -Command "(ipconfig)" | grep -a IPv4 | grep -a 172 | awk '{print $16}')hostip=$(echo -e "$hostip" | tr -d '\r') #将从powershell获得的CRLF 类型的转换为LF其他解决方案: Windows中修改
%USERPROFILE%\.wslconfig配置 加入如下内容[wsl2] networkingMode=mirrored dnsTunneling=true firewall=true autoProxy=true [experimental] # requires dnsTunneling but are also OPTIONAL bestEffortDnsParsing=true useWindowsDnsCache=true在较新版本中,在mirror模式下,WSL2 已经支持和宿主机共享 IP 可以直接使用
127.0.0.1:port的地址 微软官方详细
救大命
官方文档(看场景二):https://learn.microsoft.com/zh-cn/windows/wsl/networking#identify-ip-address
按官方文档的说明,把获取 ip 地址的方式换一下
# 添加到环境变量设置中,例如~/.zshrc export hostip=$(ip route show | grep -i default | awk '{ print $3}') export https_proxy="http://${hostip}:7890" export http_proxy="http://${hostip}:7890"
靠谱!
- window 防火墙 7890 端口放开。 cmd 命令行执行:
netsh advfirewall firewall add rule name="Open Port 7890" dir=in action=allow protocol=TCP localport=7890- clash 设置 allow lan
- wsl 更改 ~/.bash_profile
export hostip="192.168.31.151" #改成你自己的局域网IP地址 export https_proxy="http://$/{hostip}:7890" export http_proxy="http://$/{hostip}:7890" export all_proxy="socks5://${hostip}:7890"
source ~/.bash_profile- 使用
wget google.com测试
excellent!是防火墙的问题!
参考此链接,我最后成功的用Class连上了外网。
方案2 win10 wsl2可用
- 修改windows中的 ~/.wslconfig
[wsl2]
processors=12
memory=24GB
[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
sparseVhd=true
- 在 ClashVerge 中打开 系统代理 和 局域网连接
- 在 wsl 中修改 ~/.zshrc
host_ip=$(cat /etc/resolv.conf | grep "nameserver" | cut -f 2 -d " ")
export ALL_PROXY="socks5://$host_ip:7897"- enjoy
参考资料
https://blog.csdn.net/u012894550/article/details/133661114
我访问google是可以通了,但是TLS握手失败:
try:
context = ssl.create_default_context()
with socket.create_connection((host, port), timeout=5) as sock:
with context.wrap_socket(sock, server_hostname=host) as ssock:
print(f"✅ TLS 握手成功: {ssock.version()}")
except Exception as e:
print("❌ TLS 握手失败:", e)
以下代码失败了,有朋友遇到了吗?
我不知道为什么就是走不了代理 换了端口,GitHub proxy
export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
export https_proxy="http://${hostip}:7890"
export http_proxy="http://${hostip}:7890"
git config --global http.proxy http://${hostip}:7890
我的问题比较奇怪,不知道有没有人有遇到过,直接用wsl2和clash的话,是可以访问外网的,但是使用apt-get update的时候,会一直卡在0%的进度,但是能ping通源的域名,单独关闭设置里的代理也不行,必须要把clash也关掉才能update成功。
解决了吗
我的问题
WSL Settings 设置 网络模式 为 mirrored, 并激活 已启用自动代理 是可以在 WSL 的 ubuntu 中通过 clash verge 代理访问外网.
但是 buildah build 中一旦试图 apt update 就会提示不能访问 127.0.0.1:7897
原因
WSL 会自动设置 http_proxy, https_proxy 为 127.0.0.1:7897.
apt update 会使用这个代理, 但是因为 mirrored 网络模式中 127.0.0.1:7897 (WSL 机) 并不是代理.
解决方法:
- 保持 WSL Settings 设置
网络模式为mirrored, 并激活已启用自动代理 - 删掉
http_proxy,https_proxy还有no_proxy变量.
unset http_proxy
unset https_proxy
unset no_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
unset NO_PROXY
之后 apt update 之类就可以用了
注
FROM ubuntu:24.04并不受 http_proxy, https_proxy 影响
我访问google是可以通了,但是TLS握手失败: try: context = ssl.create_default_context() with socket.create_connection((host, port), timeout=5) as sock: with context.wrap_socket(sock, server_hostname=host) as ssock: print(f"✅ TLS 握手成功: {ssock.version()}") except Exception as e: print("❌ TLS 握手失败:", e)
以下代码失败了,有朋友遇到了吗?
遇到相同问题,可以访问google, 比如 curl -v google.com, 但是比如 docker pull 就会 TLS handshake timeout(任何镜像仓库)
问题和你一样,
unset http_proxy
unset https_proxy
unset no_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
unset NO_PROXY
是从wsl的ubuntu里面执行码?我执行了还是不行呢
问题和你一样,
unset http_proxy unset https_proxy unset no_proxy unset HTTP_PROXY unset HTTPS_PROXY unset NO_PROXY是从wsl的ubuntu里面执行码?我执行了还是不行呢
我把这些放在 ~/.bashrc 里了
export https_proxy="http://127.0.0.1:7890"
export http_proxy="http://127.0.0.1:7890"
export all_proxy="socks5://127.0.0.1:7890"
Work for me
我访问google是可以通了,但是TLS握手失败: try: context = ssl.create_default_context() with socket.create_connection((host, port), timeout=5) as sock: with context.wrap_socket(sock, server_hostname=host) as ssock: print(f"✅ TLS 握手成功: {ssock.version()}") except Exception as e: print("❌ TLS 握手失败:", e)
以下代码失败了,有朋友遇到了吗?遇到相同问题,可以访问google, 比如 curl -v google.com, 但是比如 docker pull 就会 TLS handshake timeout(任何镜像仓库)
我的问题解决了,是Clash 的 TUN 模式的问题,改用普通的系统代理关闭 TUN 模式然后再设置 http proxy 就可以了。

靠谱!