Skip to content

Instantly share code, notes, and snippets.

@libChan
Last active April 16, 2025 03:04
Show Gist options
  • Save libChan/3a804a46b532cc326a2ee55b27e8ac19 to your computer and use it in GitHub Desktop.
Save libChan/3a804a46b532cc326a2ee55b27e8ac19 to your computer and use it in GitHub Desktop.
WSL2使用clash for windows代理
# 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"
@Starballoon
Copy link

我的问题比较奇怪,不知道有没有人有遇到过,直接用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 using sudo visudo and adding Defaults env_keep += "http_proxy https_proxy ftp_proxy no_proxy", which allowed sudo to use the proxy correctly.

works for me also, but it's weird for I changed nothing recently

@simin75simin
Copy link

lz第一个帖子里面那三个指令就让我可以用vpn了,win11+wsl2+ubuntu22.04

@jackcjp
Copy link

jackcjp commented Aug 20, 2024

然而,在我的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无法成功。

Good

@JayTam
Copy link

JayTam commented Aug 25, 2024

image

官方文档(看场景二):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"

@zhanba
Copy link

zhanba commented Sep 8, 2024

image

官方文档(看场景二):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"

靠谱

@boyiz
Copy link

boyiz commented Oct 31, 2024

image

官方文档(看场景二):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"

靠谱

@Rongger
Copy link

Rongger commented Nov 4, 2024

很奇怪,以前我用nat模式也是通过这种方式设置代理,但不知道为什么现在设置后还是访问不了外网

设置成mirrored模式后虽然可以自动代理,不再需要手动设置,但当我使用openVpn访问公司内网的时候却无法正常访问,据说是mirrored模式和openVpn某些版本有兼容性问题,OpenVPN 2.4.7及之前的版本是正常运行的(https://github.com/microsoft/WSL/issues/10879#issuecomment-1854559320)

@Yanagi-kon
Copy link

image

官方文档(看场景二):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"

靠谱

@zmh02
Copy link

zmh02 commented Jan 6, 2025

image

官方文档(看场景二):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! !

@ctbfl
Copy link

ctbfl commented Jan 15, 2025

感谢!终于解决了,粘贴代码后重启windows系统, clash 开启 Allow Lan ,ok了!

前面的动态提取ip命令+allow lan,有用,感谢!

@cuixing158
Copy link

cuixing158 commented Feb 6, 2025

image
官方文档(看场景二):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打开模式冲突

correct

实测有效通过WSL2可以上外网了!

@zzfoo
Copy link

zzfoo commented Feb 24, 2025

  1. window 防火墙 7890 端口放开。 cmd 命令行执行:
    netsh advfirewall firewall add rule name="Open Port 7890" dir=in action=allow protocol=TCP localport=7890

  2. clash 设置 allow lan

  3. 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"

  1. source ~/.bash_profile
  2. 使用 wget google.com 测试

@fine-c
Copy link

fine-c commented Mar 5, 2025

参考此链接,我最后成功的用Class连上了外网。

@jiangqing03
Copy link

参考此链接,我最后成功的用Class连上了外网。

这个确实不错,配置.wslconfig文件就可以全局使用

@wuwen03
Copy link

wuwen03 commented Mar 31, 2025

我参考的是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