WSL2
WSL是windows推出的可让开发人员不需要安装虚拟机或者设置双系统启动就可以原生支持运行GNU/Linux的系统环境,简称WSL子系统。WSL2使用全新体系架构使其能真正的运行一个Linux内核。
1. 安装
必须运行 Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11。可以通过winver命令查看版本。
- 启用虚拟机平台和Linux子系统功能。控制面板>程序>程序和功能>启用或关闭Windows功能>适用于Linux的Windows子系统、虚拟机平台。打上勾、重启后生效。
- 在MicroStore安装一Linux发行版(或者Ubantu),下载完成后便可启动。命令行输入wsl -l —all -v查看已安装的Linux发行版。其形式如下:
可以看到还是VERSION1,即wsl版本,接下来将其升级到wsl2版本(如果version=2则不用升级)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26# 1. 确保已在 BIOS 中启用 CPU 虚拟化。在Bios的CPU设置中启用,AMD是SVM,Intel是VT-x。
# 2. 升级
wsl --update
# 3. 关闭wsl以重启
wsl --shutdown
# 3. 开启hyper-v,也可以在在windows功能中开启。(家庭版没有hyper-v选项,先用命令行添加hyper-v功能)
bcdedit /set hypervisorlaunchtype auto
# 4. 将wsl设为默认。以后下载安装的Linux默认就是wsl2
wsl --set-default-version 2
# 5. 查看已安装的Linux
wsl -l -v
# 6. 将目标Linux转为wsl2
wsl --set-version <name> 2
# 附:添加hyper-v。新建新建hyperv.cmd文件,写入以下内容,运行
pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL - 启用systemd。以下在linux系统内操作
1
2
3
4
5
6
7
8
9# 1. etc目录下新建wsl.conf文件
vi /etc/wsl.conf
# 2. 写上以下
[boot]
systemd=true
# 3. 重启wsl2
wsl --shutdown
2. 常用命令
- 查看已安装的Linux发行版:wsl -l —all -v
- 导出Linuxtar文件到D盘:wsl —export Ubuntu-20.04 d:/wsl-ubuntu-20.04.tar
- 注销当前linux发行版:wsl —unregister Ubuntu-20.04
- 重新导入并安装wsl到D盘:wsl —import Ubuntu-20.04 d:/wsl-ubuntu-20.04 d:/wsl-ubuntu-20.04.tar —version 2
- 设置默认登陆用户名为安装时的用户名:ubuntu2004 cofig —default-user USERNAME