This article is a CheatSheet dedicated to WSL2 (Windows subsystem for Linux), a kind of memo, or survival guide in the list of useful commands.
I tried to write down all the commands that are useful on a daily basis, do not hesitate to send me your commands so that I can add them to this list.
WSL2 is available from Windows 10 version 2004

List of installed distributions:

PS C:\Users> wsl --list --verbose
  NAME                   STATE           VERSION
* Ubuntu-18.04           Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2

PS C:\Users> wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-18.04           Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2

Switch a distribution from WSL1 to WSL2

PS C:\Users> wsl --set-version Ubuntu-18.04 1
The conversion is in progress. This may take a few minutes...
The conversion is complete.

PS C:\Users> wsl --set-version Ubuntu-18.04 2
The conversion is in progress. This may take a few minutes...
For more information on key differences with WSL 2, visit https://aka.ms/wsl2
The conversion is complete.
PS C:\Users>

Run the default distribution as a specific user (root / other…)

PS C:\Users> wsl --user root
root@00977-624:/mnt/c/Users# exit
logout

PS C:\Users> wsl --user tellaw
tellaw@00977-624:/mnt/c/Users$ exit
logout

Access files from Windows

Show Linux file system from Windows Explorer:

\\wsl$\<nom_distrib>\home\<user>
\\wsl$\Ubuntu-18.04\home\tellaw

Mount the Linux filesystem as a Windows drive.

PS C:\Users> subst X: "\\wsl$\Ubuntu-18.04\home\tellaw"

To unmount the network drive:

PS C:\Users> subst X: /D

Access your Windows files from Linux

Just go to the mount point : /mnt/c

Run Windows applications from Linux

In fact, I am not sure of the usefulness, except that it’s very easy to edit a file by launching notepad from Linux 🙂

cd /home/tellaw
notepad.exe monfichier.txt

Import/Export Linux Distribution via WSL

Handy for backing up and restoring a distro.

PS C:\Users\Public> wsl --export Ubuntu-18.04 bkpUbuntu.tar
PS C:\Users\Public> dir .\bkpUbuntu.tar


    Directory : C:\Users\Public


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        11/08/2020     13:52     4911390720 bkpUbuntu.tar


PS C:\Users\Public>

Restore a linux distro

PS C:\Users\Public> wsl --import UbuntuRestored c:\distri\ubuntuRestored .\bkpUbuntu.tar

PS C:\Users\Public> wsl -l
Windows Subsystem for Linux distributions:
Ubuntu-18.04 (par défaut)
UbuntuRestored   <----- My restored distro
docker-desktop
docker-desktop-data