Last active
April 16, 2025 03:04
-
-
Save libChan/3a804a46b532cc326a2ee55b27e8ac19 to your computer and use it in GitHub Desktop.
WSL2使用clash for windows代理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
感谢!终于解决了,粘贴代码后重启windows系统, clash 开启 Allow Lan ,ok了!
前面的动态提取ip命令+allow lan,有用,感谢!
官方文档(看场景二):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隧道
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good solution! !