Wsl is a windows feature that allows you to run Linux and command-line tools on Windows 10 and Windows Server 2019.

Earlier, we learned how to install WSL 2 on Windows 10 and installed Linux. Here, we look at wsl commands and how to manage installed Linux distributions.

Ways to Launch WSL

Windows provides different ways to launch wsl once it is enabled.

  • From Windows Start Menu

Open the Windows Start menu and begin typing the name of your installed Linux distribution. For instance, if you have installed Ubuntu Linux, type ubuntu and then click the corresponding app tile in the search results. Ubuntu Linux will launch in its own console window.

Launch Ubuntu From Start Menu

Another method is to launch the wsl command from windows start menu.

wsl command - windows start menu
  • From Windows PowerShell or Command Prompt

In Windows PowerShell or Command Prompt, you can either enter the name of a specific installed Linux distribution to open or enter wsl to use your default distribution, within the current command line interface.

By distribution name:

Type the name of the Linux distribution and press enter. For example, ubuntu. If you have installed Ubuntu 20.04 LTS, then you should enter ubuntu2004 in the command-line interface instead. You will then be taken to the home directory of the distribution as shown using the pwd command below. To logout, type exit.

PS C:\Users\Shola > ubuntu2004 
shola@ltwin10:~$ pwd 
/home/shola 
shola@ltwin10:~$ exit 
logout 
PS C:\Users\Shola >

By wsl.exe command:

Type wsl and press enter. This will open your default Linux distribution while maintaining the current working directory. In other words, unlike the distribution name method, entering wsl does not directly take you to the home directory of the Linux distribution.

PS C:\Users\Shola> wsl 
shola@ltwin10:/mnt/c/Users/Shola$ pwd 
/mnt/c/Users/Shola 
shola@ltwin10:~$ exit 
logout 
PS C:\Users\Shola>

Combine WSL with Linux commands

There is one other way to use the wsl command and that is by typing wsl followed by the Linux command you would like to run, in one line. The command runs as the WSL default user.

For example, The following wsl date returns the current date:

PS C:\Users\Shola> wsl date 
Mon Nov 09 17:32:37 UTC 2020

You could also combine PowerShell and Linux commands in one line.

For example, Get-ChildItem | wsl grep -i file

Output
PS C:\Users\Shola> Get-ChildItem | wsl grep -i file 
-a---- 08/11/2020 17:07 3332 file.txt

WSL command to List Distributions

To list installed Linux distributions, use wsl with --list option.

Syntax:

To list installed and usable Linux distributions.

wsl -l or wsl --list

To list all distributions, including ones that are not usable; i.e. ones that may be currently installing, uninstalling or corrupted.

wsl --list --all

To list all currently running distributions.

wsl --list --running

Note: Adding the -v option tells wsl.exe to display detailed information (v for verbose), including the WSL version number.

Output
PS C:\Users\Shola> wsl -l -v 
NAME STATE VERSION 
Ubuntu-20.04 Running 2

WSL Command to Set Default Distribution

Recall that we mentioned earlier that entering wsl in Windows PowerShell opens your default Linux distribution. Well, if you have installed more than one distribution, you can run any of the following commands using PowerShell to set your default distribution.

Syntax:

To set the default distribution to <DistributionName>

wsl -s DistributionName
or
wsl --setdefault DistributionName

For example, wsl -s ubuntu-20.04 will set Ubuntu 20.04 LTS as the default distribution.

PS C:\Users\Shola> wsl -s ubuntu-20.04

Unregistering and reinstalling distribution

When you need to reinstall a Linux distribution, you must first unregister it. When you unregister a Linux distribution, you will lose all data, settings, and apps associated with that distribution with no way to recover them thereafter.

Syntax:

To unregister a distribution:

wsl --unregister DistributionName

For example, wsl --unregister ubuntu-20.04 will unregister Ubuntu 20.04 LTS.

PS C:\Users\Shola> wsl --unregister ubuntu-20.04 
Unregistering…

To reinstall an unregistered distribution, search for it in the Microsoft store and click Launch.

Run WSL as a specific user

To run WSL as the specified user, use -u option. Note that the user must already exist in the WSL distribution.

Syntax:

wsl -u User
wsl --user User
Output
PS C:\Users\Shola> wsl -u shola2 
shola2@ltwin10:/mnt/c/Users/Shola$

Changing default user for distribution

To change that default user of a distribution use config command.

Syntax:

DistributionName config --default-user User

For example, ubuntu2004 config --default-user shola2 will change the default user in Ubuntu 20.04 LTS to shola2.

Note that the user must already exist in the WSL distribution.

PS C:\Users\Shola> ubuntu2004 config --default-user shola2 
PS C:\Users\Shola>

Use specific distribution

To run commands in another installed distribution without having to change your default distribution as follows.

Syntax:

wsl -d <DistributionName>

wsl --distribution <DistributionName>

For example, wsl -d kali-linux will allow me to run commands in Kali Linux.

PS C:\Users\Shola> wsl -d kali-linux

Note that the wsl.exe tool requires Windows 10 Version 1903 and later. In earlier Windows 10 versions, you should use the wslconfig.exe tool to manage your Linux distributions in WSL.

To see all available wslconfig options

wslconfig /? 

To see commands specific to a distribution. E.g. Ubuntu /?

DistributionName /? 

Conclusion

Windows interoperability with Linux is very laudable. We hope that this article helps you to easily navigate the WSL platform and harness the powerful synergy between Windows and Linux.