WindowsGNU/Linux

WSL Notes

2024/03/02

WSL

Windows Subsystem for Linux (WSL) is probably my favorite addition to Microsoft Windows. I no longer dual boot my laptops or install Cygwin or MSYS. I just use Linux Distributions in WSL. This page contains notes I’ve taken while working with WSL.

Basics

You can enable WSL with a single command in PowerShell or CMD. Just run as administrator and type:

wsl --install

You can check your WSL Version and Distributions with the --status option

wsl --status

Which will look something like this

C:\Users\gtest>wsl --status
Default Distribution: Ubuntu
Default Version: 2

I’ve occasionally had to reset WSL. This can be done with the --shutdown command. It reboots immediately after shutting down.

wsl --shutdown

WSL can be update from the command line. with the --update command.

wsl --update

Note: After a recent update I got an error message when trying to run Ubuntu. update error message screenshot Checking the status gave more information. status screeshot While it says I can update the WSL kernel with wsl --update, it looks like it didn’t work. The message suggests enabling ‘Receive updates for other Microsoft products when your update Windows’. This can be set in the Settings app by navigating to Windows Update > Advanced Options Settings Windows Update Advanced Options screenshot Enabling this and running a system update fixed the problem.

Linux Distributions

WSL lets you run you choice of Linux distros. You can list the distros currently installed on your machine with the --list command:

wsl --list 

If youn want to see the current state and version number of your distros add the --verbose option

PS C:\Users\gtest> wsl --list --verbose
  NAME                  STATE           VERSION
* openSUSE-Leap-15.5    Running         2
  Ubuntu                Running         2

The asterisk * indicates that Ubuntu is my default distro. You can change the default distro with the --setdefault command: ``bash wsl --setdefault <DISTRO> ``` And the*` moves to openSUSE.

C:\Users\gtest> wsl --setdefault openSUSE-Leap-15.5
C:\Users\gtest> wsl --list --verbose
  NAME                  STATE           VERSION
* openSUSE-Leap-15.5    Running         2
  Ubuntu                Running         2

I’m perfectly happy with Ubuntu, but sometimes you might want to use a different distro. To get a list of distros available online add the --online option to the --list command

wsl --list --online

The output will look something like this:

PS C:\Users\gtest> wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl --install -d <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_7_9                        Oracle Linux 7.9
OracleLinux_8_7                        Oracle Linux 8.7
OracleLinux_9_1                        Oracle Linux 9.1
openSUSE-Leap-15.5                     openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5           SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed                    openSUSE Tumbleweed

Like it says, you can install a distro by specifying its name with the -d option.

wsl --install -d openSUSE-Leap-15.5

If everything is OK, you’ll see a new window showing the distro’s installer. Follow the instructions specific to your distribution

Error 0x80073D21

While installing openSUSE on my Windows 11 machine I got error code ‘0x80073D21’

error 0x80073D21 screenshot A quick search shows this error is not specific to WSL. I saw several hits for XBox Game Pass and other Windows programs. It basically means the application must be installed on the C: drive.

My machine was alredy set to install on C: drive, so I’m not entirely sure why it was failing for this code. Some searching led me to this github page. One of the posts suggests toggling the “new apps will save to” setting from C: to D: and back again. toggle destination screenshot Which worked for me. I was able to openSUSE after toggling it.

Distributing WSL Installations

I use WSL for my own projects and for work. Sometimes I want to setup an environment on my development machine and make backups of the entire thing and/or run it on an entirely different PC.
## Exporting WSL A backup copy can be created with the --export command. This will archive your to a .tar file. Open PowerShell as an administrator and run

wsl --export Ubuntu C:\path\to\your\backup\Ubuntu.tar

Replace Ubuntu with the name of your WSL distro, and C:\path\to\your\backup\Ubuntu.tar with the path you want to save the backup to. This creates a .tar file containing your WSL file system.

Importing WSL

To import this .tar file to a new machine. 1. Copy the tar file to your new machine 2. Run the following command on that machine:

wsl --import Ubuntu C:\path\to\new\location\Ubuntu C:\path\to

The output will look something like this:

C:\Software2>wsl --import Ubuntu-20.04 .\Ubuntu-20.04 .\ubuntu-20.04.tar
Import in progress, this may take a few minutes.
The operation completed successfully.

C:\Software2>wsl --shutdown

C:\Software2>wsl --list
Windows Subsystem for Linux Distributions:
Ubuntu-20.04 (Default)

Deleting a Distro

NOTE: This will completely remove an installation. So make a backup if you don’t want to lose important data.

First find the name of the distro you want to delete with --list

spaceba@DESKTOP-8BVZ5OT:~$ wsl.exe --list --verbose
  NAME                  STATE           VERSION
* Ubuntu                Running         2
  Debian                Running         2
  openSUSE-Leap-15.5    Running         2

Next use --unregister followed by the distro’s name to delete it.

spaceba@DESKTOP-8BVZ5OT:~$ wsl --unregister Debian
Unregistering...

spaceba@DESKTOP-8BVZ5OT:~$ wsl.exe --list --verbose
  NAME                  STATE           VERSION
* Ubuntu                Running         2
  openSUSE-Leap-15.5    Running         2

Interop

One of the main advantages over dual booting (or running Linux in a VM) is interoperability is very easy in WSL.

Accessing Windows from WSL

You can run windows executables from WSL by specifying the complete name (with extension) of the application

mspaint.exe      # brings up Paint
explorer.exe .   # Opens a File Explorer of the current drive

You can access the Windows filesystem through /mnt/. For example if you wanted to change directories into the C: drive use:

cd /mnt/c

If you attach a new windows filesystem (like a USB drive, or mountable encrypted volume) after WSL has started, you can manually mount it.

 sudo mount -t drvfs F: /mnt/f

Accessing WSL from Windows.

I’ve been using Unix and Linux for decades, and I generally prefer to use Nix command line tools over their windows counterparts.

Run Linux commands from CMD.exe by prefixing with wsl

wsl find . -name "cacert.pem"

You can pipe outputs, but will need to use wsl for each command

C:\Dev\SGDK>wsl find . -type f -name "*.c" | wsl xargs grep genesis.h  2>/dev/null  | wsl head
./11_shooter/src/boot/rom_head.c:#include "genesis.h"
./11_shooter/src/main.c:#include <genesis.h>
./ASM/src/boot/rom_head.c:#include "genesis.h"
./basic/src/boot/rom_head.c:#include "genesis.h"
./basic/src/main.c:#include <genesis.h>
./C_ASM/src/boot/rom_head.c:#include "genesis.h"
./C_ASM/src/main.c:#include <genesis.h>
./FromDesktop/03_manual_tiles/main.c:#include <genesis.h>
./FromDesktop/03_manual_tiles/src/boot/rom_head.c:#include "genesis.h"
./FromDesktop/04_image_tiles/main.c:#include <genesis.h>

You can access WSL filesystem folders in explorer with \\wsl$\DISTRO_NAME or \\wsl.localhost\DISTRO_NAME screenshot

I sometimes develop networking apps in WSL. You can easily access them from localhost. If you want to use the IP address of the WSL instance, you can. Just look for the 172.x.x.x addresses on your machine.

ip addr | grep 172
  inet 172.23.210.235/20 brd 172.23.223.255 scope global eth0
gtest@BOOMSTICK:/mnt/c/Dev$ python3 -m http.server 9000
Serving HTTP on 0.0.0.0 port 9000 (http://0.0.0.0:9000/) ...
127.0.0.1 - - [03/Mar/2024 14:01:06] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Mar/2024 14:01:06] code 404, message File not found
127.0.0.1 - - [03/Mar/2024 14:01:06] "GET /favicon.ico HTTP/1.1" 404 -
172.23.208.1 - - [03/Mar/2024 14:03:14] "GET / HTTP/1.1" 200 -
172.23.208.1 - - [03/Mar/2024 14:03:14] code 404, message File not found
172.23.208.1 - - [03/Mar/2024 14:03:14] "GET /favicon.ico HTTP/1.1" 404 -

About Me

Greg Gallardo

I'm a software developer and sys-admin in Iowa. I use C++, C#, Java, Swift, Python, JavaScript and TypeScript in various projects. I also maintain Windows and Linux systems on-premise and in the cloud ( Linode, AWS, and Azure )

Github

Mastodon

YouTube

About you

IP Address: 18.118.166.98

User Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

Language:

Latest Posts

Iowa City Weather

Today

-- ˚F / 61 ˚F

Sunday

71 ˚F / 54 ˚F

Monday

64 ˚F / 46 ˚F

Tuesday

76 ˚F / 54 ˚F

Wednesday

76 ˚F / 56 ˚F

Thursday

72 ˚F / 51 ˚F

Friday

67 ˚F / 47 ˚F