Skip to content

Technical Dive : Linux stuff on Windows with WSL2 – Part 1

Table of contents

  1. What is Windows Subsystem for Linux
  2. Why using WSL2 rather than an Ubuntu’s virtual machine, Cygwin or Docker Desktop
  3. My usages
  4. Install WSL2 with Ubuntu
  5. Run “WSL Linux command” and go to the Linux operating system
  6. Add systemd to WSL2
  7. Create a Windows Scheduled task to start all the stuff
  8. Next steps
  9. Links

What is Windows Subsystem for Linux

Windows subsystem for linux (wsl) lets you run a linux environment directly from your Windows operating system. In this post I will only focus on the “2” version named wsl2. it is based on an hyper-v “hidden” and optimised virtual machine to run a real linux kernel. On top of it, you will be able to install linux distro like ubuntu, debian and others. This will give you the ability to use linux software on your windows with better integration than if you were using a regular linux virtual machine.

You must be running windows 10 version 2004 build 19041 and higher, windows 11 and windows server 2019 version 1709 and later. Windows’ new version and build will add some new features to wsl2. For example you needto have windows 11 Build 22000 or later to mount a vhdx disk inside the distro.

Why using WSL2 rather than an Ubuntu’s virtual machine, Cygwin or Docker Desktop

Why not Cygwin?

Well I’ve never been a big fan of cygwin. Not because the software is not efficient (I have seen implementations of cygwin in production) but because I don’t have expertise with the software which makes it complex to install, configure and maintain (to me).

What not a linux virtual machine on top if virtualbox or hyper-V?

Because I prefer to get a native software with better integration and because it is fun to test new tools. ?

Why not run docker desktop directly on windows with wsl2?

I don’t know if I do something bad to docker desktop but I always have some network implementation issues between my host, my container and the external world. I also had some regressions when upgrading docker desktop and several times after changing the configuration I broke the install, forcing me to clean the “AppData Local” and “Roaming” docker desktop folders… Also, before working on windows I was using a chromebook ( a beefy one) with crostiny that allowed me to run a debian container on lxc in which I had installed docker to run my stuff. (Basically, the same approach but on windows). ?

My usages

I need to have some native linux commands for my devops stuff like openssl, grep, awk etc… I also need a simple way to mount a local dev environment on which I can create, test and quickly clean all parts for my own apps and, of course, test new tools.

This is where docker comes into action:

I’m adding a visual studio code integration to mount my local workspace directly inside a dedicated container with the prerequisite software I need (nodejs, dotnet, database, mq etc…). I also use systemd to run two containers like a classic linux daemon directly on the wsl2/linux : One is a coredns container to serve a local domain, the other is traefik (edge router, proxy, load balancer) container to expose endpoints to my host over that domain. I also have some domotics containers (zigbee2mqtt, vernemq, homeassistant, etc…) that need to access an usb zigbee dongle from my host when I develop and configure domotics components for my home. (We will see some part of this in the other part) ?

A small scheme is always nice :

In this first part, we will see how to enable wsl2, install ubuntu, run some commands and add a systemd alternative to it.

Install WSL2 with Ubuntu

The recent version of windows 10 and 11 come with the wsl cli. We only need to enable wsl2 and install the distro. It is possible to run wsl2 on a windows 11 Home version but take care : It seems that we have to enable “windows subsystem for linux” and “virtual machine platform” from windows feature before installing ubuntu distro.

(Here you will probably have a quick workflow to configure the ubuntu os and define user and password)

We can check the official distro available using :

We can do the same but locally with more details :

Now we have a working linux inside our windows. Of course it is possible to install multiple linux distros and run commands over a specific one or change the default one.

Run “WSL Linux command” and go to the Linux operating system

For this, we will just update WSL2/Ubuntu :

We can do the same specifying a user :

And from WSL2/Ubunto

Note 1

I have uninstalled and reinstalled the openssh-server. I do that because from scratch my WSL2/Ubuntu’s openssh server is installed but it doen’t start and I found that reinstalling it solves the issue. I never deeply investigate this to find why it is not working in the first place. (unzip and jq are useful tools)

Note 2

WSL2 come with a share that I can access from windows :

And from WSL2/Ubuntu :

Add systemd to WSL2

WSL2 doen’t come with systemd. Some software needs it to run correctly. Fortunately arkane-systems give us “genie” (A quick way into a systemd “bottle” for WSL). It allows us to run systemd process as pid1.

First we need the “WSL-transdebian” repository :

Now we are able to install systemd-genie :

Then we have to shutdown WSL2 and launch it with genie :

Note 1

On my side I have issues with some daemons. I solved them with the command below (dirty way).

Hiding multipathd :

Changing sdb label :

Increasing timeout to genie to let some time to the services to start :

Create a Windows Scheduled task to start all the stuff

Now that we have a working WSL2/Ubuntu with a systemd integration, let’s configure it to start when our user logged in.

To do that I chose to use a small scheduled task :

Open the windows task scheduler.

Import the following xml file inside the task scheduler :

Action > Import Task …

Note 1

The interesting part in the scheduled task is the “exec” field that starts the WSL2 machine with the “genie” option.

Note that I chose to launch WSL2 at “logon” because I got some issues trying to start it before a user logged in.

Next steps

In the “Part2” we will see how to install docker inside WSL2/Ubuntu, how to create a systemd service based on docker compose and configure our new dns service to be accessed from our windows host.