..

Windows 11 WSL2 使用Clash代理

前提

因为一些不可描述的原因,我们需要在WSL2中使用代理,这里使用Clash。

WSL + Clash

新版本的WSL 2网络模式默认是NAT,这一点可以从WSL2 Settings中查看。 在常规启用Clash的情况下,我们打开WSL 2 Ubuntu终端,会提示如下问题:

wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。

我们在Ubuntu中尝试curl -I https://www.google.com 果然无法成功连接。 接着尝试开启Clash Tun模式,接管所有流量,依然无法成功连接。

Mirrored模式

我们将WSL2网络模式切换为Mirrored模式,参见 使用 WSL 访问网络应用程序 ,然后执行 wsl –shutdown 关闭WSL2后重启,成功连接。

curl -I https://www.google.com
HTTP/1.1 200 Connection established

WSL2 Ubuntu开发容器中测试

启动VS Code DevContainer, 在容器中执行curl -I https://www.google.com 测试也没有问题。

VS Code DevContainer

参考资料