WSL2 今天史诗级更新

292 天前
 hez2010

今天 WSL2 更新了 2.0.0 版本,详细更新内容见: https://github.com/microsoft/WSL/releases/tag/2.0.0

总结一下主要是:

  1. 支持自动回收内存
  2. 支持自动释放 WSL2 虚拟硬盘空间
  3. 支持和 Windows 使用相同的网络(镜像网络)
  4. 支持 DNS Tunneling
  5. 支持 Windows 设置的代理
  6. 支持 Windows 防火墙
  7. 支持 Multicast

需要至少 Windows 11 23H2 ,目前还没有发布正式版本,不过可以考虑加入 Insider 的 Release Preview 或者 Beta 通道,更新系统之后运行 wsl --update --pre-release 升级 WSL ,然后就可以在 %userprofile%\.wslconfig 里面设置以下内容:

[experimental]
autoMemoryReclaim=gradual # 可以在 gradual 、dropcache 、disabled 之间选择
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

然后你会发现,WSL2 和 Windows 主机的网络互通而且 IP 地址相同了,还支持 IPv6 了,并且从外部(比如局域网)可以同时访问 WSL2 和 Windows 的网络。这波升级彻底带回以前 WSL1 那时候的无缝网络体验了,并且 Windows 防火墙也能过滤 WSL 里的包了,再也不需要什么桥接网卡、端口转发之类的操作了。

33292 次点击
所在节点    程序员
194 条回复
TsubasaHanekaw
292 天前
这波好了之后,windows 11 就成了最好的 linux 发行版
ShadowPower
292 天前
@DigitalG
https://anthony-f-tannous.medium.com/wsl2-how-to-prepare-and-attach-virtual-drives-vhd-ac17b1fc7a61
这篇文章里有自动挂载的例子,但是启动 wsl 要用两条命令来完成。
BurgerTown
292 天前
VSCode 好像没能适应 mirrored 极其的慢 把自动端口转发关了也是一样(关了好像有些进程还是会自动转发
sunmoon1983
292 天前
我想要 win10 也能支持啊
leeyuzhe
292 天前
发现一个问题,开 clash 的 tun mode 之后,wsl 里面上不了网。ping 所有域名都被转发到一个内网 ip 上面
hez2010
292 天前
@default996 看错误代码是更新过程被中止。
可以考虑去 GitHub Release 页面下载 msix 包自己手动升级。
LANB0
292 天前
23h2 要 beta 通道吧,似乎 Release Preview 搜不到更新
leeyuzhe
292 天前
@leeyuzhe 完蛋 jetbrains 的 ide 也连不上网了
FYFX
292 天前
@leonshaw
这个应该挺早就支持了吧, 我现在 wsl 的 /home 目录挂的就是单 ext4 分区的一个盘, 不过每次 wsl --shutdown 之后都要手动再挂一遍,开机是写了个脚本自动挂载的
https://learn.microsoft.com/en-us/windows/wsl/wsl2-mount-disk
Akiya
292 天前
终于不用再整端口转发的麻烦事了吗
hez2010
292 天前
@leeyuzhe 可以考虑关掉 clash 然后重启一下系统。转发到的那个内网 IP 有可能是 clash 给你设置的代理,既然都用 tun mode 了就应该把代理的地址从设置里清掉。
leeyuzhe
292 天前
@hez2010 #111 没有手动或者自动设置全局代理,只有 tun mode ,关掉后 wsl 可以正常上网。但是 idea 还是不行
yeohreet
292 天前
可以可以,基本痛点都解决了。
hez2010
292 天前
@leeyuzhe 有可能 idea 帮你做了端口转发然后发生端口冲突了,对于原来的网络来说有必要,对于新的来说属于是多此一举。
a632079
292 天前
@cylx3124 #69

https://devblogs.microsoft.com/commandline/windows-subsystem-for-linux-september-2023-update/#automatic-memory-reclaim

However, this feature does require disabling cgroups v1 in WSL, which can cause some issues. In early testing we noticed that this will break the docker daemon when running it as a service in WSL, and so if you’re using this feature we recommend you use Docker Desktop for your docker needs. We are working with the Docker team to address this in the future.

If you’d like to customize your idle detection thresholds and more, we’d recommend doing so by not enabling this feature and creating a bash script, see this GitHub gist for instructions.

You can also set this to drop which will instead drop caches entirely after detecting idle, and will not require any cgroup changes.


总结下来,如果你是直接 apt install docker 的话,那你需要打开 systemed 转 cgroup2 ,然后禁用 cgroup 1 。亦或者直接切 Docker Desktop ,亦或者直接采用 drop 策略。
dryadent
292 天前
我有个问题,wsl 微软官方只支持 Ubuntu 么,可以装其他的发行版么
a632079
292 天前
@dryadent 可以啊,Fedora ,CentOS (现在是 Rocky ),Debian ,Arch 啥的都在商城里能直接搜到。如果自己打包的话会麻烦一些。不过微软他自己肯定是优先基于 ubuntu 测试的。
hez2010
292 天前
@dryadent Ubuntu Debian CentOS Kali Arch Fedora openSUSE Manjaro 甚至 Alpine 等等,基本上所有发行版都有,微软商店里面找不到的话 GitHub 上也有很多社区开发者自己做了 WSL 的发行版。
Nasei
292 天前
这下边提 mac 的就是搞笑的,一旦你要编译的代码涉及到很多 native 的库,你就知道 mac 有多坑了,只能当成一个远程上网本到服务器上编译。但是 wsl 可以
gav1nvv
292 天前
之前就是因为网络问题,导致基于微服务的 docker 开发特别困难,有空试试

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/975098

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX