-
-
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" |
官方文档(看场景二):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"Good solution! !
注意:clash的系统代理”System Proxy“要关闭掉才行,避免与TUN打开模式冲突
实测有效通过WSL2可以上外网了!
-
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
测试
参考此链接,我最后成功的用Class连上了外网。
参考此链接,我最后成功的用Class连上了外网。
这个确实不错,配置.wslconfig文件就可以全局使用
我参考的是https://www.caomei.wiki/knowledge-base/clash-lan/,我踩的坑是开启了clash的service模式,关掉就能够成果了,关闭的方法在连接里面。我的wsl2使用的是nat模式,应该关闭dns隧道
我的问题比较奇怪,不知道有没有人有遇到过,直接用wsl2和clash的话,是可以访问外网的,但是使用apt-get update的时候,会一直卡在0%的进度,但是能ping通源的域名,单独关闭设置里的代理也不行,必须要把clash也关掉才能update成功。
If you are using the http_proxy variable, you need to check whether the http_proxy variable continues to be used after running sudo. Generally, sudo will clear the environment variables of the common user by default. Previously, I encountered this issue where
sudo curl ip.gs
could not use the proxy due to this reason. I eventually solved the issue by modifying the /etc/sudoers file usingsudo visudo
and addingDefaults env_keep += "http_proxy https_proxy ftp_proxy no_proxy"
, which allowed sudo to use the proxy correctly.
Works for me, thanks! It bothers me about 1 month.
我也碰到同样问题,最后发现在Docker-Desktop settings里配置proxy 127.0.0.1:7890就可以了
官方文档(看场景二):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"
靠谱
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
的地址 微软官方详细
有用。感谢。
ipconfig 信息大致如下
以太网适配器 vEthernet (WSL (Hyper-V firewall)):
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : *************************************
IPv4 地址 . . . . . . . . . . . . : 172.21.0.1
子网掩码 . . . . . . . . . . . . : 255.255.240.0
默认网关. . . . . . . . . . . . . :
试了下上面的:export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
获取的是里面的子网掩码,用这个来连总是Connection refuse
让 hostip 直接等于 ipconfig 里查看到的 IPv4 地址就可以连了(export hostip=$(ip route show | grep -i default | awk '{ print $3}')
)
官方文档(看场景二):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"靠谱
获取ip地址最为关键 稳
官方文档(看场景二):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"
靠谱!
前面的动态提取ip命令+allow lan,有用,感谢!