WSL Command: Master Windows Subsystem for Linux with These Essential Commands

WSL is the command-line tool used to manage the Windows Subsystem for Linux (WSL), a feature in the Windows operating system that enables users to run Linux distributions directly on Windows.

How to Install WSL on Windows 10 and Windows 11

In this tutorial, we'll explore the usage of the wsl command for managing the Windows Subsystem for Linux (WSL). Through practical examples, you'll learn how to install and manage Linux distributions seamlessly.

Examples

To begin, you can install WSL 2 on your Windows system using the wsl --install command:

wsl --install

By default, it will install Ubuntu. You can install additional distributions. But first, run the following command to get the list of valid Linux distributions you can install:

wsl --list --online
wsl command: List Linux distributions

You can install a new Linux distribution using the --distribution or -d option. Here's an example of how to use the -d option to install Kali Linux:

wsl --install -d kali-linux

Use the following command to list all Linux distributions installed on your computer:

wsl --list --verbose
wsl -l -v

The above command will display an output similar to the following:

wsl command: list all Linux distributions
List all Linux distributions

Notice the asterisk (*) before 'Ubuntu'? It denotes that Ubuntu is the default distribution. This means that if you run the wsl command without any options, it will launch the Ubuntu shell.

wsl command

To start a specific distribution, use the --distribution or -d option. For example, you can run the following command to start Kali Linux:

wsl -d kali-linux

If you want to change the default Linux distribution use the --set-default or -s option. For example, following command makes Kali Linux the default distribution on this computer:

wsl --set-default kali-linux
change default linux distribution

The next command option we'll explore is the --exec option. This option allows you to execute a command on a specific Linux distribution without logging in to the shell.

For example, the following command executes the ls command on Kali Linux:

wsl -d kali-linux -e ls /var
wsl: execute a command without logging in to the shell

To run a Linux distribution with a specific user, use the --user or -u option:

wsl -d Ubuntu -u user2
wsl -d Ubuntu -u user2 -e whoami
Run a Linux distribution with a specific user

To stop a running distribution, use the --terminate option followed by the distribution name. For instance, to stop Ubuntu, use the following command:

wsl --terminate Ubuntu

To uninstall a distribution, use the --unregister option followed by the distribution name. For instance, to uninstall Ubuntu, use the following command:

wsl --unregister Ubuntu
wsl command: uninstall linux distribution

Utilize the --shutdown option to stop Windows Subsystem for Linux and the lightweight Hyper-V container WSL 2 running on:

wsl --shutdown

How to use the wsl command to manage different versions of WSL

The latest version of the Windows Subsystem for Linux is WSL 2. You can run the wsl --status command to see the default version of your system.

wsl status

You can change the default version by using the --set-default-version option:

wsl --set-default-version 2
wsl --set-default-version 1

When you switch to version 1, if you receive the error message "WSL1 is not supported with your current machine configuration", turn on the Windows Subsystem for Linux Windows feature.

Changing the default version affects only future installations of Linux distributions. To change the version of existing distributions, use the --set-version option.

For example, the following command changes Kali Linux to WSL version 1:

wsl --set-version kali-linux 1

Backing Up and Restoring Linux Distributions with WSL

To back up a distro, use the --export option. For example, the following command backs up the Ubuntu distro to a file named ubuntu-backup.tar.gz, saving it to the C:\Backups\ directory:

wsl --export Ubuntu C:\Backups\ubuntu-backup.tar.gz

To launch a new distro using the backup file, use the following syntax:

wsl --import <Distro-Name> <Install-Location> <File-Name>

For example, the following command launches a new distro called Ubuntu-02 from the backup file ubuntu-backup.tar.gz and installs it to the C:\WSL\Ubuntu-02:

wsl --import Ubuntu-02 C:\WSL\Ubuntu-02 C:\Backups\ubuntu-backup.tar.gz

This is also the way you clone a Linux distro in Windows Subsystem for Linux.

In Summary

And that brings us to the end of this tutorial. In this tutorial, we learned how to manage Linux distributions on Windows Subsystem for Linux using the wsl command. For more information on the wsl command and its various options, please use the --help option:

wsl --help