Last active
April 14, 2024 04:46
-
-
Save libChan/3a804a46b532cc326a2ee55b27e8ac19 to your computer and use it in GitHub Desktop.
WSL2使用clash for windows代理
This file contains 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" |
想知道export后面的变量是否区分大小写,这么配置变量的出处在哪里?
感谢
然而,在我的wsl上配置这三行代码并不生效。
我是直接将配置改成电脑的本地ip,而不是nameserver(运行
cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*'
的值)比如:
export https_proxy="http://192.168.1.6:7890" export http_proxy="http://192.168.1.6:7890"
另外,我开启了7890的windows防火墙端口。
可以正常访问外网,但是ping无法成功。
根据我的经验,假设运行clash的ip为192.168.1.1
,那么只需要两步操作:
- 打开clash的
Allow LAN
- 运行代码(可以设置开机自动运行)
export http_proxy=http://192.168.1.1:7890
export https_proxy=http://192.168.1.1:7890
不需要打开防火墙,因为wsl和clash运行在同一台电脑。
如果其他设备(比如手机)需要通过电脑的clash进行代理,那么可以放开防火墙的7890
端口
根据我的经验,假设运行clash的ip为192.168.1.1,那么只需要两步操作:
设置代理确实可以成功,我在Android studio 中设置http代理,gradle 就可以正常下载
如果有人按照这个操作失败了,可以试试这里面的
如果有人按照这个操作失败了,可以试试这里面的
这对我有效,我使用如下配置(%USERPROFILE%\.wslconfig
):
[wsl2]
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
[experimental]
# requires dnsTunneling but are also OPTIONAL
bestEffortDnsParsing=true
useWindowsDnsCache=true
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
然而,在我的wsl上配置这三行代码并不生效。
我是直接将配置改成电脑的本地ip,而不是nameserver(运行
cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*'
的值)比如:
export https_proxy="http://192.168.1.6:7890" export http_proxy="http://192.168.1.6:7890"
另外,我开启了7890的windows防火墙端口。
可以正常访问外网,但是ping无法成功。