🛠️Install a Ubuntu Distribution through WSL2
wsl🤩In this note, we are going to learn how to install WSL2. If you want to learn about the differences between WSL2 and WSL1, please refer to this official docs. For more information, please refer to references.
Instructions
Installation
use
wsl --list --online
to view available Linux distributions that can be downloaded through the online storewsl --list --online use
wsl --install -d <distribution name>
to install the certain distribution and this well a few minutes for the first time. For example, in this note, Harry installed Ubuntu-20.04, for which he usedwsl --install -d Ubuntu-20.04
. If you counter some errors, please refer to AdditionsSuccessful installation
Verification
use
wsl -l -v
to view the installed distributions' name, state and version.Once installed, there are several ways to run a Linux distribution. Here are some recommend ways:
Typing the name of the installed distribution in the Windows Start menu. For example, "Ubuntu". This opens Ubuntu in its own console window.
search "Ubuntu" in the Windows Start menu Typing the name of the installed distribution in cmd or Windows Powershell. Type
wsl
orwsl.exe
will open the default distribution. To change default setting, refer to this.NOTE: If you have installed visual studio code, you can type
code .
in the cli of Ubuntu to connect Linux server with VS Code server.connect VSC with linux server For more information, refer to this.
Quit and Stop
When in a WSL, you can use
ctrl+D
orlogout
to go back to powershell or cmd.Use
wsl --shutdown [distribution name]
to stop the running WSL.

Migration
Reference: 如何对WSL2进行备份与还原
By default, WSL and Ubuntu will be installed on C:\
. In this section, you will learn how to transfer these two parts to other disks.
Deactivate: use
wsl -l --all -v
to view the states of all distributions and usewsl --shutdown
to stop the running onesBackup: use
wsl --export <name> <backup path>
Unregister: use
wsl --unregister <name>
Restore: use
wsl --import <name> <restore dir> <backup path> [--version 2]
and you can see the VHDX file of Ubuntu present in your restore dir. (see the below figure)Successfully migrated Set default user: after importing and starting the Ubuntu backup, you may find the default user is root. Although you can change the default user of a distribution by
<distribution name> config --default-user <user name>
in cmd, it doesn't work for imported distributions. But you can do that by modifying/etc/wsl.conf
in Ubuntu (see the figure below) [Reference]. Besides, you can change user temporarily by usingsu <user name>
in Ubuntu orwsl --user <user name>
in cmd
Additions
If you counter error 0x800701bc, the tutorials below are recommended for your
Error: 0x800701bc If you counter error 0x80370102, the tutorials below are recommended for your
Error: 0x80370102 In Chinese: wsl2的Error 0x80370102 解决方案
References
In Chinese: 如何使用 WSL 在 Windows 上安装 Linux | Microsoft Learn
Last updated