Skip to content

Instantly share code, notes, and snippets.

@libChan
Last active May 26, 2025 12:55
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"
@WorlesEnric
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, thanks! It bothers me about 1 month.

@ROROROA
Copy link

ROROROA commented May 23, 2025

我也碰到同样问题,最后发现在Docker-Desktop settings里配置proxy 127.0.0.1:7890就可以了

@xudong7
Copy link

xudong7 commented May 26, 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"

靠谱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment