blog icon indicating copy to clipboard operation
blog copied to clipboard

WSL2 使用 Clash for Windows 的 TUN 模式全局访问外网

Open ninehills opened this issue 2 years ago • 1 comments

有两种方法:

第一种,配置 HTTP(S)/SOCKS 代理

  • 开启 Clash for Windows 的 LAN 访问
  • 在 Windows 防火墙中放行 Clash 代理端口
  • 在 WSL2 中使用 <HOSTNAME>.local:7890 作为代理。

但是这种代理并非全局,依赖于程序自身实现。

第二种,配置 TUN 模式代理。

  • 开启 Clash for Windows 的TUN 模式,参考 https://docs.cfw.lbyczf.com/contents/tun.html#windows
  • 开启后,配置 UWP Loopback (需要 Clash 以管理员权限启动),放行 Windows 子系统(直接放行全部可以解决部分 UWP 应用如 XBox 不走代理的问题)
  • 重启电脑后,WSL2 内便全局代理配置成功。

注:这种模式下,需要让 Clash 以管理员权限启动才能让 WSL2 使用代理,可以右键点击 Clash,配置属性来让 Clash 具备管理员权限。

ninehills avatar Jun 16 '23 15:06 ninehills

win11下尝试第二种模式,发现无法exempt Windows Subsystem for Linux,会报错。最后新建$HOME/.wslconfig 文件写入以下内容后,使用wsl --shutdown 重启wsl可以解决

[wsl2]
networkingMode=mirrored

Sduby22 avatar Sep 08 '24 18:09 Sduby22