8

WSL2 网络的最终解决方案

 1 year ago
source link: https://zhuanlan.zhihu.com/p/593263088
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WSL2 网络的最终解决方案

すもももももももものうち

微软终于更新了 WSL2,允许用户自行指定用于 WSL2 的网卡,因此不再需要任何的脚本等等即可固定 WSL2 地址甚至支持 IPv6 和外部访问。

注意:此方法需要至少 Windows 11 22H2。

首先需要从 Microsoft Store 里面下载最新的 Windows Subsystem for Linux。

用不了商店的可以前往这里自己下载 msixbundle 部署安装:https://github.com/microsoft/WSL/releases

装好之后可以运行 wsl --version 确认一下是不是 1.0 以上。

然后在用户目录 %USERPROFILE% 下面创建一个配置文件 .wslconfig,按照如下配置方法设置网络,例如:

[wsl2]
networkingMode=bridged # 桥接模式
vmSwitch=my-switch # 你想使用的网卡
ipv6=true # 启用 IPv6

wsl --shutdown && wsl 重启 WSL2,搞定。

我自己电脑上的话,使用 Hyper-V 创建了一个叫做“WSLBridge”的外部虚拟交换机来给 WSL2 用,于是配置文件改成:

[wsl2]
networkingMode=bridged
vmSwitch=WSLBridge
ipv6=true

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK