Windows Subsystem for Linux (WSL2)

Windows Subsystem for Linux (WSL2) is a compatibility layer that enables running Linux binary executables natively on Windows, providing an integrated experience between the two operating systems.

Cheet sheet

# Status of WSL
wsl --status

# Install WSL
wsl --install

# Update WSL
wsl --update

# List available distributions
wsl --list --online

# To list installed distributions
wsl --list

# To list installed distributions along with its running status and wsl config being 1 or 2
wsl --list --verbose

# To run a specific distro
wsl --distribution distro_name

# To terminate/shutdown a specific distro
wsl --terminate distro_name_to_shutdown

# To shutdown all disstros
wsl --shutdown

# Set specific distro as default
wsl --set-default my_default_distro

# To EXPORT a running distro as image
wsl --export distro_name_to_export windows_path\tar_file_name.tar

# To IMPORT an image as distro
wsl --import new_distro_name install_location_windows_path tar_file_name.tar --version wsl-version-1-or-2
wsl --import Ubuntu-20 D:\VMs\WSL\Ubuntu-20\ Ubuntu-20.04.tar --version 2 # Setting my secondary HDD as storate loc for new distro

# To UNREGISTER (also removes the its file storage) a distro
wsl --unregister distro_name_that_delete

# To run a WSL distro as the specified user.
wsl -u username -d distroname
wsl -u root -d Ubuntu-20.04

# To change the default user for a distribution
distributionName config --default-user Username
ubuntu config --default-user my_default_username
ubuntu2004.exe config --default-user johndoe # When you have Ubuntu 20.04 version installed from the Microsoft Store

The following steps are numbered and it’s recommended to follow each step in order so your WSL environment is setup correctly.

(1) Install WSL

Open PowerShell elevated as Administrator and run the following commands.

wsl --install

This command just sets up the WSL subsystem, you still need to install a Linux distro.

(2) Install a distribution

List the available distros.

PS ~> wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_8_5                        Oracle Linux 8.5
OracleLinux_7_9                        Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4                     openSUSE Leap 15.4
openSUSE-Tumbleweed                    openSUSE Tumbleweed

Install a distribution of your choice.

PS ~> wsl --install Ubuntu-22.04
Installing: Ubuntu 22.04 LTS
Ubuntu 22.04 LTS has been installed.
Launching Ubuntu 22.04 LTS...
Installing, this may take a few minutes...
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: {username}
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.90.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/{username}/.hushlogin file.

When the new distribution starts for the first time you’ll be prompted to create a user and password. These are local to the WLS instance not have not correlation to the Windows credentials.

(3) Update and Upgrade the distribution

Perform an update and upgrade of the operating system.

sudo apt update && sudo apt upgrade -y

(4) Validate you can run Windows and Linux commands from Linux and Windows

Your bashrc and profile configuration files should be configured with paths to Windows executables. It’s important these work otherwsie your whole setup will give errors. To test this make sure you can execute a Windows binary from the WSL instance.

This command open a Windows Explorer session

Make sure you include the trailing space and period

explorer.exe .

From Windows you can also run Linux commands from PowerShell.

PS ~> wsl ls -la
total 11576
drwxrwxrwx 1 neil neil    4096 Apr 27 10:14  .
dr-xr-xr-x 1 neil neil    4096 Dec  4  2021  ..
drwxrwxrwx 1 neil neil    4096 Apr 27 07:56  .VirtualBox
drwxrwxrwx 1 neil neil    4096 Apr 17 15:30  .aws
drwxrwxrwx 1 neil neil    4096 Nov 15 10:50  .azcopy

It’s important that your default WSL instance is set to the one you want to use as this is where the commands will execute against. The error below was because the default distribution was Docker which was shutdown. When I change the default to the running Ubuntu instance then the error resolved.

PS ~> wsl ls -la
Processing fstab with mount -a failed.

<3>WSL (8) ERROR: CreateProcessEntryCommon:370: getpwuid(0) failed 2
<3>WSL (8) ERROR: CreateProcessEntryCommon:374: getpwuid(0) failed 2
<3>WSL (8) ERROR: CreateProcessEntryCommon:577: execvpe /bin/sh failed 2
<3>WSL (8) ERROR: CreateProcessEntryCommon:586: Create process not expected to return

(5) File access between Windows and Linux

Access Linux Files from Windows

You can access the WSL instance file system from Windows Explorer. The path starts at \\wsl$\ and then you choose the WSL instance itself.

For an Unbutnu instance the path to the home directory would be

\\wsl.localhost\Ubuntu\home\webadmin

You can map the WSL instance disk as a drive letter but only up to the instance, not deeper.

y:\\wsl.localhost\Ubuntu\

In the Terminal settings, set the path to your homedirectory as seen from Windows as the Starting directory for the WSL instance.

\wsl.localhost\Ubuntu\home\webadmin

Access Windows Files from Linux

Windows files are accessed through the mnt directory.

webadmin@ubuntu:~$ ls -la /mnt/c/users
total 0
dr-xr-xr-x 1 webadmin webadmin 4096 Dec  4  2021  .
drwxrwxrwx 1 webadmin webadmin 4096 Apr 27 10:24  ..
lrwxrwxrwx 1 webadmin webadmin 18   Jun  5  2021 'All Users' -> /mnt/c/ProgramData
dr-xr-xr-x 1 webadmin webadmin 4096 Jan 20  2022  Default
lrwxrwxrwx 1 webadmin webadmin 20   Jun  5  2021 'Default User' -> /mnt/c/Users/Default
drwxrwxrwx 1 webadmin webadmin 4096 Oct 23  2021  Public
-r-xr-xr-x 1 webadmin webadmin 174  Jun  5  2021  desktop.ini
drwxrwxrwx 1 webadmin webadmin 4096 Apr 27 10:14  winuser

You can create a synbolic link to a folder on the Windows file system to make it easier to access from the Linux shell. Say you have a project folder called project00 in your Windows home directory. You can run the following command to create a symbolic link to that directory.

ln -s /mnt/c/users/winuser/project00

To access it from the shell

cd ~/project00

(6) Use SSH Keys from Windows 11 on WSL OS

If you have SSH key pairs in your Windows environment in the .ssh folder these cannot be used by your WSL system by default. Even though the WSL system is integrated with Windows it’s still basically a different environment and it cannot by default call the private key in the .ssh folder to authenticate against an SSH service hosting a copy of the public key. But you can modify things so that you can.

Logon to the WSL environment you want to use the key from. By running this command, you’re copying your SSH keys and configuration files from your Windows file system to your Linux file system. This will allow you to use the same SSH keys and configuration settings in both environments, making it easier to connect to remote servers.

This assumes there is no directory ~/.ssh on the WSL instance already as the cp commmand creates it

cp -r /mnt/c/Users/<username>/.ssh ~/.ssh

You need to adjust the permissions on the keys to allow you to use them. The command chmod 600 ~/.ssh/id_rsa sets the permissions on your private SSH key file to read and write only for the owner of the file (you), and no permissions for anyone else. This is the recommended permission setting for private key files, as it ensures that only you can access and use the key.

chmod 600 ~/.ssh/id_rsa

Now you can SSH from the WSL instance to a target system that has your pubic key and use the private key copied from Windows to authenticate.

References

Install WSL

Run Linux commands on Windows

WSL Guide

Last modified July 21, 2024: update (e2ae86c)