安装 WSL

是一个为在 Windows 10 和 Windows Server 2019 以上能够原生运行 Linux 二进制可执行文件(ELF 格式)的兼容层。可以把它当作一个只能用命令行交互的 Linux 虚拟机。

安装

参考:安装 WSL | Microsoft Learn

在管理员模式下打开终端,输入:

wsl --install

这将开始安装 Ubuntu。

安装好之后会提示你创建一个默认 UNIX 用户名和密码。用户名最好使用小写的单个单词:

Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username:
New password:
Retype new password:

输入完成之后,我们的 Ubuntu 就初始化成功了:

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.133.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


This message is shown once a day. To disable it please create the
/home/user/.hushlogin file.
user@host:~$

如果要退出 WSL,输入 exit

exit

如果下次要进入 WSL,输入 wsl,将进入设置的默认 Linux 发行版:

wsl

卸载

卸载 Linux 发行版:

wsl --unregister ubuntu  # 卸载 Ubuntu

卸载完成后我们可以通过 wsl -l 命令检查卸载是否成功。

Docker

参见:在 WSL 中使用 Docker | 博客园

Troubleshooting

安装了 Docker 的同学可能会发现使用 wsl 命令启动虚拟机时报错:

$ wsl
<3>WSL (34) ERROR: CreateProcessParseCommon:708: Failed to translate C:\Users\user
<3>WSL (34) ERROR: CreateProcessParseCommon:754: getpwuid(0) failed 2
<3>WSL (34) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files\PowerShell\7
<3>WSL (34) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\system32
<3>WSL (34) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows
...

这是因为 WSL 的默认 Linux 发行版被设置成了 docker-desktopp-data。我们可以使用 wsl -l 命令检查默认 Linux 发行版:、

$ wsl -l
适用于 Linux 的 Windows 子系统分发:
docker-desktop-data (默认)
Ubuntu
docker-desktop

可以看到默认 Linux 发行版是 docker-desktop-data。我们可以使用 wsl -s 命令重新设置默认 Linux 发行版:

wsl -s ubuntu  # 设置 Ubuntu 为默认发行版

相关阅读:在 WSL 中使用 Docker

posted @ 2024-04-15 16:25  Undefined443  阅读(194)  评论(0编辑  收藏  举报