Windows10 安装 WSL2 的一些记录

1.查看本机 windows 版本

键盘上按下 win+R,输入 winver,查看系统版本。必须运行 Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11。满足版本要求后,继续下一步。

2.BIOS 及控制面板开启虚拟化

(1)BIOS 开启虚拟化
我用的是联想小新笔记本,开机时狂按 F2(Fn+F2)即可进入 BIOS 界面,找到在上方的【Cpu Configuration】选项,将最下方的【Intel Virtual Technology】,在 VirtuallizationTechnology 上按下回车键把 Disabled 改为 Enabled。AMD 则是将【SVM Support】设置为设置为【Enable】的状态;最后点击【F10】保存退出即可。
开机后打开【任务管理器——性能】检查 VT 功能是否成功开启

(2)在 Windows 系统中开启虚拟化

3.Powershell 中启用 Windows 子系统和虚拟机平台功能

在 powerShell 中以管理员身份运行下面命令以确保开启适用于 Linux 的 Windows 子系统虚拟机平台配置项。

1
2
3
4
5
wsl --install
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

wsl --set-default-version 2

开启后重启
检查版本

1
wsl -l -v

4.下载安装包并安装

1
2
3
4
5
6
Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu18.appx -UseBasicParsing
Rename-Item .\Ubuntu_1804.2019.522.0_x64.appx Ubuntu18.appx
Rename-Item .\Ubuntu18.appx Ubuntu18.zip
Expand-Archive .\Ubuntu18.zip -Verbose
cd Ubuntu18/
.\ubuntu1804.exe
1
2
3
4
5
Rename-Item .\Ubuntu_2004.2020.424.0_x64.appx Ubuntu20.appx
Rename-Item .\Ubuntu20.appx Ubuntu20.zip
Expand-Archive .\Ubuntu20.zip -Verbose
cd Ubuntu20/
.\ubuntu2004.exe

5.WSL apt 换源

换源地址集合(开源软件国内镜像源对比
WSL2 换源系列问题(https://blog.csdn.net/weixin_47359411/article/details/123861792
(1).备份源列表

1
2
3
cd /etc/apt/

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

(2).命令行打开 sources.list 文件

1
sudo vim /etc/apt/sources.list

(3).将源文件内容全部注释,并添加以下任意源(优先清华源) #阿里云 #20221005 经测试 ubuntu18.04 速度只有 100 多 kb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

#清华源 #20221005 经测试 ubuntu18.04 报错但下方找到解决方案
解决:将/etc/resolv.conf 下的源本机为 WSL 设置的虚拟 ip 地址注释掉

1
2
3
4
5
6
7
8
9
10
11
12
13
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

20.04(http)

1
2
3
4
5
6
7
8
9
10
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

#中科大源(配置链接)#20221005 经测试 ubuntu18.04 OK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse

## Not recommended
# deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

(4).更新源

1
2
3
cat sources.list

sudo apt-get update

(5).更新软件,然后你就可以感受到更换国内源之后的飞速提升了

1
2
sudo apt-get -y dist-upgrade
sudo apt-get -y upgrade

(6).更换默认 vim
卸载 vim-tiny

1
sudo apt-get remove vim-common

安装 vim-full

1
sudo apt-get install vim

6.开启宿主 Win10 文件系统相互访问

(1).Win 系统访问 WSL2 文件
在资源管理器输入

1
\\wsl$

(2).Linux 子系统访问 Win10 文件
在 WSL 内 进入 mnt 目录

1
cd /mnt/

7.解决 systemctl 命令无法使用问题

1.安装daemonizefontconfig

1
2
apt install -y fontconfig daemonize

2.编辑/etc/profile 脚本,加入如下内容:

1
2
3
4
5
6
7
8
9
10
11
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')

if [ -z "$SYSTEMD_PID" ]; then
sudo /usr/bin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
fi

if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
exec sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi

3.修改/etc/sudoers 文件,加入如下内容:

1
2
3
%sudo ALL=(ALL) NOPASSWD: /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
%sudo ALL=(ALL) NOPASSWD: /usr/bin/nsenter -t [0-9]* -a su - [a-zA-Z0-9]*

4.执行 source /etc/profile 或者是重新打开 terminal,执行 systemctl 验证是否能够正常操作。

1
2
3
4
5
6
sudo vi /etc/wsl.conf

[network]
generateHosts = false


8.关闭 WSL2 进程

1
2
#关闭虚拟机
wsl --shutdown

9.对 WSL 内存进行限制

输入 %UserProfile% 并打开 CMD
执行code .wslconfig并填入下列内容

1
2
3
4
5
[wsl2]
processors=8
memory=8GB
swap=8GB
localhostForwarding=true

安装配置参考:https://blog.csdn.net/RenLJ1895/article/details/122741040
安装到其他盘参考:https://www.cnblogs.com/lidabo/p/16614014.html
WSL2 内存限制参考:https://zhuanlan.zhihu.com/p/345645621