WSL2 常规使用

7/24/2020 WSL2

WSL2 是再Windows上运行linux,相对于虚拟机长占后台,WSL2占用资源更少,同样能完成包括大多数命令行工具、程序和应用。

# WSL

# WSL概要

WSL 是 Windows Subsystem for Linux 的缩写,意思是 linux 版的 window 子系统。

Linux 的 Windows 子系统让开发人员无需虚拟机就可以直接在 Windows 上运行 Linux 环境,包括大多数命令行工具、程序和应用。

使用 WSL 的好处是:

  • 与在虚拟机下使用 Linux 相比,WSL 占用资源更少,更加流畅
  • WSL 可以对 Windows 文件系统下的文件直接进行读写,文件传输更方便
  • 剪贴板互通,可以直接在 Windows 下其它地方复制文本内容,粘贴到 WSL

WSL2简介 WSL 2 是 WSL 中体系结构的新版本,它更改了 Linux 分发版与 Windows 进行交互的方式。 WSL 2 的主要目标是提高文件系统性能并增加完全的系统调用兼容性。 每个 Linux 分发版都可以作为 WSL 1 或 WSL 2 运行,并可随时在它们之间进行切换。 WSL 2 是对基础体系结构的一次重大改造,它使用虚拟化技术和 Linux 内核来实现其新功能

总结下有以下几个优点

  • WSL2附带真实的Linux4.19内核,能带来完整的系统调用兼容性
  • 支持本机直接运行Docker容器
  • WSL2运行速度更快

# WSL安装

# 启用Windows 子系统

安装适用于 Linux 的 Windows 子系统,必须先启用“适用于 Linux 的 Windows 子系统”可选功能,然后才能在 Windows 上安装 Linux 分发版。以管理员身份打开 PowerShell 并运行:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
1

若要仅安装 WSL 1,现在应重启计算机并继续安装所选的 Linux 分发版 (opens new window),否则请等待重启并继续更新到 WSL 2

阅读有关比较 WSL 2 和 WSL 1 (opens new window) 的详细信息。

# 更新到 WSL 2

若要更新到 WSL 2,必须满足以下条件:

# 启用“虚拟机平台”可选组件

安装 WSL 2 之前,必须启用“虚拟机平台”可选功能。

以管理员身份打开 PowerShell 并运行:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
1

重新启动计算机,以完成 WSL 安装并更新到 WSL 2。

# 将 WSL 2 设置为默认版本

安装新的 Linux 分发版时,请在 PowerShell 中运行以下命令,以将 WSL 2 设置为默认版本:

wsl --set-default-version 2
1

运行该命令后,你可能会看到此消息:WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel。 跟随链接(https://aka.ms/wsl2kernel),在文档中安装来自该页面的 MSI,以便在计算机上安装 Linux 内核供 WSL 2 使用。 安装内核后,请再次运行该命令,该命令应会成功完成而不显示消息。

备注

从 WSL 1 更新到 WSL 2 可能需要几分钟才能完成,具体取决于目标分发版的大小。 如果从 Windows 10 周年更新或创意者更新运行 WSL 1 的旧(历史)安装,可能会遇到更新错误。 按照这些说明卸载并删除任何旧分发 (opens new window)

如果 wsl --set-default-version 结果为无效命令,请输入 wsl --help。 如果 --set-default-version 未列出,则表示你的 OS 不支持它,你需要更新到版本 2004、内部版本 19041 或更高版本。

# 安装 Linux 分发版

打开windows应用商店,搜索ubuntu,并选择你偏好的 Linux 分发版。我安装的是Ubuntu 20.04版本。

# 注意:

  1. ssh连接设置:

每次切换网络,或者重新连接网络,主机会动态分配给WSL一个ip,ssh连接能智能查找本地WSL(例如MobaXterm)可以直接连接,但是连接ftp,就需要每次修改连接的ip。其次还有一些环境变量的冲突问题,因为主机和WSL的PATH环境变量和hosts配置是共享的。

// 关闭WSL和主机的交互,在WSL下
echo "[interop]\nenabled=false\nappendWindowsPath=false" | sudo tee /etc/wsl.conf

// 在powershell(以管理员身份运行)中输入: (以重启wsl)
net stop LxssManager
net start LxssManager

// 后续ssh,ftp连接,ip可以直接设置为127.0.0.1即可
1
2
3
4
5
6
7
8
  1. DNS配置

WSL会自动在启动时根据虚拟交换机WSL生成/etc/resolv.conf文件,如下为默认的文件内容:

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.27.96.1
1
2
3
4

暂时解决方案:自动开机启动项写入配置

'echo -e "\nnameserver 114.114.114.114\nnameserver 8.8.8.8\nnameserver 8.8.4.4" > /etc/resolv.conf'

echo -e "options timeout:1 attempts:1 rotate\nnameserver 114.114.114.114\nnameserver 8.8.8.8" >/etc/resolv.conf
1
2
3
  1. 设置root用户登陆
# 设置ubuntu登录默认用户为root,我的是 ubuntu2004,根据自己对应版本来
ubuntu2004 config --default-user root

# 设置允许root用户登陆,好像是允许ftp root登陆的,自行测试
vi  /etc/ssh/sshd_config
LoginGraceTime 2m
PermitRootLogin yes
StrictModes yes

# 设置允许密码登陆
PasswordAuthentication yes

# 之前修改的下面这种方式,修改Root账户密码,参考
sudo passwd root

# 停止Windows WSL服务,在Power Shell(以管理员身份)运行
net stop lxssmanager 

# 更改WSL默认用户, 在Power Shell(以管理员身份)运行,重新打开WSL即可看到登录用户为Root
C:\Users\用户名\AppData\Local\Microsoft\WindowsApps\ubuntu2004.exe config --default-user root
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  1. 设置开机自启动ssh服务,让三方软件可以通过ssh连接

每次开机,终端管理软件无法通过ssh连接Linux, 需要先通过自带命令行,或者启动ubuntu客户端,输入service ssh restart,之后才能连接

# win+R, 输入shell:startup,进入开机启动文件夹目录,新建一个.bat的批处理文件,内容如下:
ubuntu2004 -c "service ssh start"
1
2

# ubuntu添加启动脚本

ubuntu20.04添加启动脚本,开机启动nameserver,防止每次启动WSL2 nameserver被初始化覆盖

防止WSL2 nameserver 重启被覆盖
Incredibly unhelpful.

It is the issue for many people, it is the same in issue trackers/forums/etc across the internet.

The WSL instance cannot resolve domain names. Editing resolv.conf to point to a functioning nameserver "works" for the duration of the session, but as soon as the distro is rebooted resolv.conf is regenerated using WSL's original template. Because etc/resolv.conf is actually a symlink to run/resolvconf/resolv.conf

Steps that have worked for me:

Boot your distro.
cd ~/../../etc
Create wsl.conf, however you see fit. , and edit it later, whatever.sudo vim wsl.confsudo touch wsl.conf
Add these lines to wsl.conf:
[network]
generateResolvConf=false
exit or in Windows cmd wsl --terminate [YourDistroName]
Boot your distro.
At this point, thanks to wsl.conf, run/resolvconf should no longer exist and will never be created again.

cd ~/../../etc
sudo rm resolv.conf - this deletes the existing symlink file.
Create a new resolv.conf, however you see fit. , and edit it later, whatever.sudo vim resolv.confsudo touch resolv.conf
Add this line to resolv.conf:

replace 8.8.8.8 with your preferred functional nameserver.nameserver 8.8.8.8
exit or in Windows cmd wsl --terminate [YourDistroName]
wsl --shutdown just to be sure that you've definitely killed everything.
Boot your distro.
Confirm that your resolv.conf changes are still in effect, or just ping a domain name and cry tears of joy after struggling to get this working for far too fucking long
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
27
28

# 移除WSL中PATH共享Windows(可选)

sudo vim /etc/wsl.conf

# 不加载Windows中的PATH内容
[interop]
appendWindowsPath = false

# 不自动挂载Windows系统所有磁盘分区
[automount]
enabled = false

启动powershell
wsl --list
# 适用于 Linux 的 Windows 子系统:
# Ubuntu-20.04 (默认)
wsl --terminate Ubuntu-20.04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# ubuntu安装源

备份源

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

修改源

sudo gedit /etc/apt/sources.list
1

ubuntu20.04源

#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#添加清华源
deb https://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 https://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 https://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 https://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 multiverse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

刷新并立即生效

# 更新源
sudo apt-get update
# 更新软件
sudo apt-get upgrade
1
2
3
4

# WLS和VMware冲突解决

使用powershell工具,输入下面指令:

  • 启用VMware:bcdedit /set hypervisorlaunchtype off
  • 启用WSL:bcdedit /set hypervisorlaunchtype auto

# 重启WSL2

有些配置需要重启生效,可以选择宿主机也就是电脑重启,也可以以管理员运行命令控制台,重启WSL服务

停止:net stop LxssManager

启动:net start LxssManager