Windows: Working with the linux subsystem (WSL)

Hi,

this post contains some examples on how to interact with the Windows Subsystem for Linux. For an installation howto see my previous post.

List all available distributions for installation

wsl -l -o
NAME            FRIENDLY NAME
Ubuntu          Ubuntu
Debian          Debian GNU/Linux
kali-linux      Kali Linux Rolling
openSUSE-42     openSUSE Leap 42
SLES-12         SUSE Linux Enterprise Server v12
Ubuntu-16.04    Ubuntu 16.04 LTS
Ubuntu-18.04    Ubuntu 18.04 LTS
Ubuntu-20.04    Ubuntu 20.04 LTS

Install a distribution

D:\> wsl --install --distribution Ubuntu-20.04
Installing Ubuntu 20.04 LTS

Common status

D:\> wsl --status

List all installed distributions

D:\> wsl -l --all

A distribution within the subsystem will be automatically startet when command is executed or a terminal is openend.
List all running distributions

D:\> wsl -l --running

Set a wsl version for a distribution

D:\> wsl --set-version Ubuntu-20.04 2

Set the default wsl version

D:\> wsl --set-default-version 2

Execute a command in the default shell

D:\> wsl -d Ubuntu-20.04 cat /etc/issue
Ubuntu 20.04 LTS \n \l

or

D:\> wsl -d Ubuntu-20.04 -- cat /etc/issue
Ubuntu 20.04 LTS \n \l

and as user root

D:\> wsl -d Ubuntu-20.04 -u root apt update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB] ....

open a terminal

D:\> wsl -d Ubuntu-20.04 
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Or with the app execution aliases simply start a terminal by

App execution aliases

D:\> debian

or Ubuntu

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

Accessing the windows filesystems from wsl. All windows disks are mounted unter /mnt/$Driveletter

michael@windev:/mnt/c/Users/Michael$ ls -l /mnt/
total 0
drwxrwxrwx 1 michael michael  512 Nov 21 12:55 c
drwxrwxrwx 1 michael michael 4096 Nov 20 17:25 d
drwxrwxrwx 1 michael michael 4096 Nov 20 17:25 m
drwxrwxrwt 2 root    root      40 Nov 22 21:10 wsl

Accessing the wsl linux filesystems from Windows with the wsl$ driver provider

D:\> dir \\wsl$\Ubuntu-20.04

Each Windows command could executed in a shell

michael@windev:/mnt/c/Users/Michael$ explorer.exe

Update the WSL 2 Kernel

D:\> wsl --update
...
Kernel-Version: 5.10.60.1

Windows can show your Distributions under the navigation pane of the explorer. This can be switched on/off by setting System.IsPinnedToNameSpaceTree to 0 or 1

D:\> reg add HKEY_CURRENT_USER\Software\Classes\CLSID\{B2B4A4D1-2754-4140-A2EB-9A76D9D7CDC6} /ve /t REG_SZ /d Linux
D:\> reg add HKEY_CURRENT_USER\Software\Classes\CLSID\{B2B4A4D1-2754-4140-A2EB-9A76D9D7CDC6} /v System.IsPinnedToNameSpaceTree /t REG_DWORD /d 1

Some settings can be configured by the app alias. The default user

D:\> ubuntu2004 config --default-user michael

Set the default distribution. If you omit the -d switch from the wsl command then this distribution is used as default.

D:\> wsl --set-default Ubuntu-20.04

Shutdown the wsl subsystem

D:\> wsl --shutdown

Terminate a wsl distribution

D:\> wsl --terminate Ubuntu-20.04

Unregister and delete the whole distribution (ext4.vhdx is deleted)

D:\> wsl --unregister  Ubuntu-20.04

Also a whole distribution can exported to a tar file

D:\> wsl --export Ubuntu-20.04 D:\temp\ubuntu.tar

and imported from a tar file. The second parameter “D:\wsl\Ubuntu-20.04-imported” is the folder where the ext4.vhdx disk image is created

D:\> wsl --import Ubuntu-20.04-imported D:\wsl\Ubuntu-20.04-imported D:\temp\ubuntu.tar

Not tested yet (not included in 20H2, coming with Windows 11 Build 22000) : Mount a disk or device

D:\> wsl --mount

Not tested yet: Import a any/custom linux distribution
A custom linux image can installed when the distributor offers an tar file with the root filesystem. For example Alpine Linux
or you can convert docker os container into wsl images. See this post on howto do this…
To be continued….

Michael

Advertisment to support michlstechblog.info

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.