Skip to content

Instantly share code, notes, and snippets.

@libChan
Last active August 31, 2025 08:57
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"
@guwinston
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"

靠谱!

@HuBuguiX
Copy link

  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 测试

excellent!是防火墙的问题!

@sucr233
Copy link

sucr233 commented Aug 15, 2025

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

方案2 win10 wsl2可用

@lemyx
Copy link

lemyx commented Aug 19, 2025

  1. 修改windows中的 ~/.wslconfig
[wsl2]
processors=12
memory=24GB
[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
sparseVhd=true
  1. 在 ClashVerge 中打开 系统代理 和 局域网连接
  2. 在 wsl 中修改 ~/.zshrc
host_ip=$(cat /etc/resolv.conf | grep "nameserver" | cut -f 2 -d " ")
export ALL_PROXY="socks5://$host_ip:7897"
  1. enjoy

参考资料
https://blog.csdn.net/u012894550/article/details/133661114

@PerryHuan9
Copy link

我访问google是可以通了,但是TLS握手失败:
try:
context = ssl.create_default_context()
with socket.create_connection((host, port), timeout=5) as sock:
with context.wrap_socket(sock, server_hostname=host) as ssock:
print(f"✅ TLS 握手成功: {ssock.version()}")
except Exception as e:
print("❌ TLS 握手失败:", e)

以下代码失败了,有朋友遇到了吗?

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