满纸荒唐言,一把心酸泪,都云作者痴,谁解其中味。 技术博客 心情随笔
WSL:在Windows中使用Linux
2025/1/17 502

导航

1前言

2在Windows中启用WSL功能

3在Windows中装载Linux发行版

4解决在WSL中systemctl命令无法运行的问题

5在WSL中让64位Ubuntu支持32位程序

1 前言

WSL全称Windows Subsystem for Linux,是由微软开发的支持在Windows中运行Linux的一个子系统,2022年发布WSL 1.0,2023年发布了WSL 2.0,在2.0中使用了真正的Linux内核,并提升了性能与Linux程序的兼容性。来源:https://www.wubayue.com

2 在Windows中启用WSL功能

控制面板 > 程序 > 启用或关闭Windows功能:

在Windows中启用WSL功能

以管理员身份运行 Windows PowerShell,并在Windows PowerShell中执行如下命令:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

对于 x64 系统,Windows 10版本需要 1903 或以上,内部版本 18362.1049 及以上。如果版本过低,请参考微软的解决方案:https://learn.microsoft.com/zh-cn/windows/wsl/install-manual

设置 WSL 2 为默认版本(WSL 2的官网下载地址:https://github.com/microsoft/WSL/releases),在 Windows PowerShell中执行如下命令:

wsl --set-default-version 2

执行命令升级WSL内核(也可手动下载安装:https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi):来源:https://www.wubayue.com

wsl --update 

3 在Windows中装载Linux发行版

从以下链接中下载一个Linux发行版:https://learn.microsoft.com/zh-cn/windows/wsl/install-manual

解压并在Windows PowerShell中安装Linux发行版本(安装过程中设置账号/密码):

.\ubuntu.exe 

安装完成后退出:

exit

在Windows命令行执行wsl命令启动Linux,然后在Linux命令行设置root密码:

sudo passwd root

在Linux命令行切换至root账号:

su -

验证WSL2以及Linux发行版本是否已装载成功,执行Windows命令:

wsl -l -v

如需在WSL中卸载一个Linux发行版本,可先通过wsl命令查看已安装的Linux版本列表:

wsl --list

然后通过命令完成卸载:来源:https://www.wubayue.com

wsl --unregister <发行版本名称>

4 解决在WSL中systemctl命令无法运行的问题

编辑/etc/wsl.conf:来源:https://www.wubayue.com

[boot]
systemd=true 

5 在WSL中让64位Ubuntu支持32位程序

安装qemu-user-static,用来模拟运行32位可执行程序:

sudo apt install qemu-user-static 

在系统的binfmt support的服务中增加i386可执行程序文件格式的支持,使得系统可以直接加载i386可执行程序文件格式并运行(实际上是调用qemu-i386-static来运行):

sudo update-binfmts --install i386 /usr/bin/qemu-i386-static --magic '\x7fELF\x01\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03\x00\x01\x00\x00\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\xff\xff\xff\xff'
sudo service binfmt-support start

在系统中增加i386 cpu架构的支持,然后安装i386程序运行需要依赖的软件包:来源:https://www.wubayue.com

sudo dpkg --add-architecture i386
sudo apt update
sudo apt install -y libc6:i386 libncurses5:i386 libstdc++6:i386 zlib1g:i386 zlib1g-dev:i386

<全文完>