Skip to content

Instantly share code, notes, and snippets.

@djfdyuruiry
Last active February 7, 2023 10:57
Embed
What would you like to do?
WSL 2 - Enabling systemd

Enable systemd in WSL 2

NOTE: If you have Windows 11 there is now an official way to do this in WSL 2, use it if possible - see MS post here (WINDOWS 11 ONLY)

This guide will enable systemd to run as normal under WSL 2. This will enable services like microk8s, docker and many more to just work during a WSL session. Note: this was tested on Windows 10 Build 2004, running Ubuntu 20.04 LTS in WSL 2.

  • To enable systemd under WSL we require a tool called systemd-genie

  • Copy the contents of install-sg.sh to a new file /tmp/install-sg.sh:

    cd /tmp
    wget --content-disposition \
      "https://gist.githubusercontent.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950/raw/952347f805045ba0e6ef7868b18f4a9a8dd2e47a/install-sg.sh"
  • Make it executable:

    chmod +x /tmp/install-sg.sh
  • Run the new script:

    /tmp/install-sg.sh && rm /tmp/install-sg.sh
  • Exit the WSL terminal and shutdown the WSL env:

    wsl --shutdown
  • To open a new WSL terminal with systemd enabled, run:

    wsl genie -s
  • Prove that it works:

    sudo systemctl status time-sync.target
#! /usr/bin/env bash
set -e
# change these if you want
UBUNTU_VERSION="20.04"
GENIE_VERSION="1.44"
GENIE_FILE="systemd-genie_${GENIE_VERSION}_amd64"
GENIE_FILE_PATH="/tmp/${GENIE_FILE}.deb"
GENIE_DIR_PATH="/tmp/${GENIE_FILE}"
function installDebPackage() {
# install repackaged systemd-genie
sudo dpkg -i "${GENIE_FILE_PATH}"
rm -rf "${GENIE_FILE_PATH}"
}
function downloadDebPackage() {
rm -f "${GENIE_FILE_PATH}"
pushd /tmp
wget --content-disposition \
"https://github.com/arkane-systems/genie/releases/download/v${GENIE_VERSION}/systemd-genie_${GENIE_VERSION}_amd64.deb"
popd
}
function installDependencies() {
sudo apt-get update
wget --content-disposition \
"https://packages.microsoft.com/config/ubuntu/${UBUNTU_VERSION}/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install -y \
daemonize \
dotnet-runtime-5.0 \
systemd-container
sudo rm -f /usr/sbin/daemonize
sudo ln -s /usr/bin/daemonize /usr/sbin/daemonize
}
function main() {
installDependencies
downloadDebPackage
installDebPackage
}
main
@voltrare
Copy link

@kylhuk not yet for windows 10 users and probably will take longer as wsl preview in store version only available on windows 11 sadly

@kylhuk
Copy link

kylhuk commented Sep 29, 2022

@kylhuk not yet for windows 10 users and probably will take longer as wsl preview in store version only available on windows 11 sadly

@voltrare Oh, I see - didn't know that was a thing. I kinda forgot why people do not want to upgrade to Windows 11 :)

@voltrare
Copy link

cuz lot of ppl actually still using old pcs that havent received win11 support tho im aware there is workaround but possibly itll be slower than win 10

@djfdyuruiry
Copy link
Author

djfdyuruiry commented Oct 8, 2022

Updated the gist guys as MS being silly about Windows 10 support

@mabusdogma
Copy link

C:\Users\me>wsl genie -s
genie: already inside the bottle; cannot start shell!

...alright, I've got the genie, how can I make it work?

@HeckTwrcha
Copy link

If you are on Win10, add 'genie -s' to your distro's ".bash_profile" then close and reopen your distro.
If you are on Win11 systemd is nativly supported and you do not need geine. Check the previous announcement made earlier in this thread.

@ringtailedfox
Copy link

ringtailedfox commented Oct 19, 2022

when i type wsl genie -s, i get this error:

genie: WARNING: systemd is in degraded state, issues may occur!
genie: already inside the bottle; cannot proceed

that can't be good...

edit:
i've found it's degraded becuase it can't find or start the two following services:
systemd-remount-fs.service.service
multipathd.socket.service

@chienbinhso14
Copy link

`PS C:\Users\Steelwix> wsl genie -s Waiting for systemd....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Timed out waiting for systemd to enter running state. This may indicate a systemd configuration error. Attempting to continue. Failed units will now be displayed (systemctl list-units --failed): UNIT LOAD ACTIVE SUB DESCRIPTION ● multipathd.socket loaded failed failed multipathd control socket

LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.

1 loaded units listed.`

How to deal with the multipathd.socket fail?

Try to using Terminal on Windows App store and run it as Administrator

@voltrare
Copy link

you can refer to genie wiki for common problems
Systemd-units-known-to-be-problematic-under-WSL

@voltrare
Copy link

voltrare commented Oct 22, 2022

Try to using Terminal on Windows App store and run it as Administrator

nah man dont need to run as admin

@ringtailedfox
Copy link

I still have this issue:
genie: already inside the bottle; cannot proceed

i'm not sure what causes it...

@Mathiasb17
Copy link

Mathiasb17 commented Oct 27, 2022

I have Ubuntu 22.04 on WSL2 (Windows 10).

I reach the point where i type wsl genie -s, but nothing happens and the program returns after a few seconds :

C:\Users\mbrousse>wsl genie -s

C:\Users\mbrousse>

When i try to run genie in bash i get the following output :

$ genie
A fatal error occurred. The required library libhostfxr.so could not be found.
If this is a self-contained application, that library should exist in [/usr/libexec/genie/].
If this is a framework-dependent application, install the runtime in the global location [/usr/lib/dotnet/dotnet6-6.0.110] or use the DOTNET_ROOT environment variable to specify the ru
ntime location or register the runtime location in [/etc/dotnet/install_location].

The .NET runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=ubuntu.22.04-x64&apphost_version=5.0.8

In the script i did update UBUNTU_VERSION to value 22.04.

Any lead on this ?

@4wk-
Copy link

4wk- commented Dec 19, 2022

Hello @djfdyuruiry I've encounter a dependency related issue.

dpkg: dependency problems prevent configuration of systemd-genie:
 systemd-genie depends on policykit-1; however:
  Package policykit-1 is not installed.
 systemd-genie depends on gawk; however:
  Package gawk is not installed.

In order to fix it:

apt --fix-broken install
dpkg-reconfigure systemd-genie

Fresh install of WSL on a fresh windows install. wsl --intall then reboot

wsl --set-default-version 2
wsl --install -d Debian
wsl --set-default Debian

Then, on Debian, I ran apt install wget (needed for your script) then as usual:

cd /tmp
wget --content-disposition \
"https://gist.githubusercontent.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950/raw/952347f805045ba0e6ef7868b18f4a9a8dd2e47a/install-sg.sh"

chmod +x /tmp/install-sg.sh
/tmp/install-sg.sh && rm /tmp/install-sg.sh

Then at the end, there is the dependencies issue:

dpkg: dependency problems prevent configuration of systemd-genie:
 systemd-genie depends on policykit-1; however:
  Package policykit-1 is not installed.
 systemd-genie depends on gawk; however:
  Package gawk is not installed.

@dominsights
Copy link

It didn't work for Ubuntu 22. I've used Ubuntu 20 and it worked fine.

@redlinejoes
Copy link

redlinejoes commented Dec 22, 2022

It didn't work for Ubuntu 22. I've used Ubuntu 20 and it worked fine.

This git and the methods described are deprecated.

You should NOT be using this method any longer. The author needs to update this Gist to inform users that this is no longer necessary. WSL has been updated for Windows 10 to match what is available in Windows 11.

You should NOT be using this Gist or the methods described in this repo to enable systemd in WSL for Windows 10 or 11.

To enable the systemd support in Windows 10 or 11, you only need to update your WSL installation.

On Windows 10, when you run the WSL --version command, you should see the following:
WSL version: 1.0.3.0
Kernel version: 5.15.79.1
WSLg version: 1.0.47
MSRDC version: 1.2.3575
Direct3D version: 1.606.4
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.19045.2364

On Windows 11, when you run the WSL --version command, you should see the following:
WSL version: 1.0.3.0
Kernel version: 5.15.79.1
WSLg version: 1.0.47
MSRDC version: 1.2.3575
Direct3D version: 1.606.4
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.25267.1000

If you run the latest version of WSL, you can enable the systemd support without issue in Ubuntu 22.04. So that you know, you will want to install WSL from the MS Store. Then you can enable the systemd support using the "/etc/wsl.conf" file with the following options:
[boot]
systemd=true

Follow these instructions for help. The procedure works on Windows 11; as of November 2022, it also works on Windows 10.
https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/

@4wk-
Copy link

4wk- commented Dec 27, 2022

@redlinejoes Thanks for the update... Yeah, this gist is saying "windows 11 ONLY" :/

I have the same wsl --version as you.
Now, I will try to clean and remove/uninstall everything this gist does: https://gist.githubusercontent.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950/raw/952347f805045ba0e6ef7868b18f4a9a8dd2e47a/install-sg.sh

@rrodrigueznt
Copy link

rrodrigueznt commented Jan 2, 2023

@4wk

I have the same wsl --version as you. Now, I will try to clean and remove/uninstall everything this gist does: https://gist.githubusercontent.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950/raw/952347f805045ba0e6ef7868b18f4a9a8dd2e47a/install-sg.sh

Please, have you managed to restore the original state of your WSL installation? How?

I followed the same procedure described in the original post above before @redlinejoes comment and end up with a WSL installation that pops up the following error message every time I launch my Ubuntu-20.04 Linux server:

popup

It doesn't happen if I launch my CentOS Stream 8 or 9 servers.

My WSL and Windows releases are:

PowerShell 7.3.1
PS C:\Users\ricar> wsl --version
WSL version: 1.0.3.0
Kernel version: 5.15.79.1
WSLg version: 1.0.47
MSRDC version: 1.2.3575
Direct3D version: 1.606.4
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.963
PS C:\Users\ricar>

Thanks!

@4wk-
Copy link

4wk- commented Jan 2, 2023

Hey @rrodrigueznt
I didn't find a clean way to restore the original state of my WSL installation.
So I did a complete uninstall then reinstall of wsl: https://gist.github.com/4wk-/889b26043f519259ab60386ca13ba91b

I had the same RemoteApp error as well.

Hope it helps!

@rrodrigueznt
Copy link

@4wk- Thanks!

I'm not able to understand how the script above affects the functioning of the whole WSL and not only to Ubuntu installation or installations (not sure if it affects only to a Debian installation existing before running the scrip or any other installed after running it). Please, do you or anybody does understand it? Thanks!

@4wk-
Copy link

4wk- commented Jan 3, 2023

@rrodrigueznt I don't understand all the ins and outs of the script above, that's why I wrote my own gist to properly uninstall then reinstall wsl.

But AFAIK, genie mod does not affect only debian, it affects all wsl install.

@rrodrigueznt
Copy link

rrodrigueznt commented Jan 3, 2023

@4wk- You wrote a nice guide to completely remove WSL and any distro on Windows. Thanks!
As I have here some four nice installations, Ubuntu and CentOS running under WSL2, I will try keeping removing Genie and get systemd working natively. In the WSL Genie site, there are packages to install it on different distros, that is why I'm guessing it must be possible to uninstall it. I will report here my failure or my success!

@rrodrigueznt
Copy link

@4wk-, all,

A first result: I have two running Ubuntu distros on the same WSL2 installation.

Ubuntu 22.04 is running smoothly and has systemd activated as per below:

rjr@IGFAE-MU-05-DellXPS-Windows10:~$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
rjr@IGFAE-MU-05-DellXPS-Windows10:~$ systemctl status logrotate.timer
● logrotate.timer - Daily rotation of log files
     Loaded: loaded (/lib/systemd/system/logrotate.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Wed 2023-01-04 08:27:58 CET; 3min 34s ago
    Trigger: Thu 2023-01-05 00:00:00 CET; 15h left
   Triggers: ● logrotate.service
       Docs: man:logrotate(8)
             man:logrotate.conf(5)

Jan 04 08:27:58 IGFAE-MU-05-DellXPS-Windows10 systemd[1]: Started Daily rotation of log files.
rjr@IGFAE-MU-05-DellXPS-Windows10:~$

Ubuntu 20.04 runs smoothly if I don't activate systemd:

ricardo@IGFAE-MU-05-DellXPS-Windows10:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.5 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.5 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
ricardo@IGFAE-MU-05-DellXPS-Windows10:~$ cat /etc/wsl.conf
#[boot]
#systemd=true
ricardo@IGFAE-MU-05-DellXPS-Windows10:~$ systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
ricardo@IGFAE-MU-05-DellXPS-Windows10:~$

But if troughs the aforementioned error The connection information for this resource is invalid when I launched it with systemd=true in ```/etc/wsl.conf````. Thus, I concluded so far that the WSL Genie issue relates with each installed distro, and it is possible to install fresh ones without need to remove the whole WSL2 thing. I will investigate further about the in troubles Ubuntu installation and keep this thread posted! Any comment is always welcome. Thanks.

@redlinejoes
Copy link

I get the impression that you guys intend to keep your WSL systems for some length of time. The whole idea of WSL and containers is that they can be deleted and recreated quickly and easily without fuss. If you intend to keep a Linux system, install a VM. The WSL installation can be easily broken by Windows updates or any random software you install, like Genie. It would be wiser to install a VM and perform a snapshot of the VM before you install system-altering software like Genie. This way, you can always revert.

If you dig deep enough, you can find ways to copy/export your WSL VHD for backup and recovery purposes or to move it to a different location, but it is an Internal Development Platform (IDP). It's not intended to be kept for any length of time on the system after you develop some software.

If your system doesn't work with systemd and Genie, it would be faster and easier just to delete the installation and reinstall WSL Ubuntu from MSStore using the latest version and enable systemd and do not return to this Gist since it has been deprecated now with the latest versions of WSL.

@rrodrigueznt
Copy link

@redlinejoes Got it! I will delete and forget my in-troubles Ubuntu 20.04 VHD. I can easily agree with your POV. Thanks!

@4wk-
Copy link

4wk- commented Jan 4, 2023

@redlinejoes I respectfully disagree: in my opinion, WSL is meant to be use for interoperability with Windows, which it does perfectly. Also, in my usage, WSL is a lot better that a VM performance-wise and convenient-wise. I use WSL to run docker containers when I need testing things "on the go", and delete/recreate containers. But my WSL daily use is, indeed, in the long term.

Be that as it may, your initial comment was just at the right moment as it taught me (and others probably) that wsl now support native systemD, which is great, and after cleaning wsl from genie, everything is working perfectly for me! So everything's fine 🙂

@redlinejoes
Copy link

Technically speaking, WSL is a VM. The main difference is the kernel. The kernel in WSL distributions is different because Microsoft develops it. Per Microsoft, it is intended to be used for interoperability during development. Everyone can read the docs here: https://learn.microsoft.com/en-us/windows/wsl/about.

During development on Windows, you absolutely should be using WSL for Docker. There is nothing to disagree with. What you describe are development purposes.

If someone intends to keep a heterogeneous Linux system for production, they should use the Linus-developed kernel distributions in a VM.

There are valid reasons why this is best practice from Microsoft and the Linux community and not a matter of opinion. Using the documentation from Microsoft, I'll go ahead and elaborate so others can know the differences.

From the documentation: https://learn.microsoft.com/en-us/windows/wsl/faq#who-is-wsl-for-
Who is WSL for?
"This is primarily a tool for developers, especially web developers, those working on open source projects, or deploying to Linux server environments. WSL is for anyone who likes using Bash, common Linux tools (sed, awk, etc.) and Linux-first frameworks (Ruby, Python, etc.) but also enjoys using Windows productivity tools."

From the documentation: https://learn.microsoft.com/en-us/windows/wsl/faq#can-i-use-wsl-for-production-scenarios--
Can I use WSL for production scenarios?
"WSL has been designed and built to use with inner loop development workflows. There are design features in WSL that make it great for this purpose but may make it challenging for production-related scenarios compared to other products. Our goal is to make clear how WSL differs from a regular VM environment, so you can make the decision on whether it fits your business needs."

The main differences between WSL and a traditional production environment are:

  • WSL has a lightweight utility VM that starts, stops and manages resources automatically.
  • If you have no open file handles to Windows processes, the WSL VM will automatically be shut down. This means if you are using it as a web server, SSH into it to run your server and then exit, the VM could shut down because it is detecting that users are finished using it and will clean up its resources.
  • WSL users have full access to their Linux instances. The lifetime of the VM, the registered WSL distributions, etc., are all accessible by the user and can be modified by the user.
  • WSL automatically gives file access to Windows files.
  • Windows paths are appended to your path by default, which could cause unexpected behavior for certain Linux applications compared to a traditional Linux environment.
  • WSL can run Windows executables from Linux, which could also lead to a different environment than a traditional Linux VM.
  • The Linux kernel used by WSL is updated automatically.
  • GPU access in WSL happens through a /dev/dxg device, which routes GPU calls out to the Windows GPU. This setup is different than a traditional Linux set up.
  • There are other smaller differences compared to bare metal Linux and more differences are expected to arise in the future as the inner loop development workflow is prioritized.

In summary, Microsoft is saying that WSL is not intended to replace running Linux as a VM, regardless of how WSL performs. WSL can do certain things that make it ideal for development workflows. However, it's been designed to operate differently than users expect from a typical Linux system. This makes it not ideal for production or long-running utilization. The confusion can stem from when a development tool is released to the public, and non-developers start to use it and think it's something it is not.

@4wk-
Copy link

4wk- commented Jan 7, 2023

So I misunderstood you earlier, sorry. I wasn't talking of production use. My use of WSL is only for developing purposes, and I do agree with you! Thanks!

@dominsights
Copy link

It didn't work for Ubuntu 22. I've used Ubuntu 20 and it worked fine.

This git and the methods described are deprecated.

You should NOT be using this method any longer. The author needs to update this Gist to inform users that this is no longer necessary. WSL has been updated for Windows 10 to match what is available in Windows 11.

You should NOT be using this Gist or the methods described in this repo to enable systemd in WSL for Windows 10 or 11.

To enable the systemd support in Windows 10 or 11, you only need to update your WSL installation.

On Windows 10, when you run the WSL --version command, you should see the following: WSL version: 1.0.3.0 Kernel version: 5.15.79.1 WSLg version: 1.0.47 MSRDC version: 1.2.3575 Direct3D version: 1.606.4 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.19045.2364

On Windows 11, when you run the WSL --version command, you should see the following: WSL version: 1.0.3.0 Kernel version: 5.15.79.1 WSLg version: 1.0.47 MSRDC version: 1.2.3575 Direct3D version: 1.606.4 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.25267.1000

If you run the latest version of WSL, you can enable the systemd support without issue in Ubuntu 22.04. So that you know, you will want to install WSL from the MS Store. Then you can enable the systemd support using the "/etc/wsl.conf" file with the following options: [boot] systemd=true

Follow these instructions for help. The procedure works on Windows 11; as of November 2022, it also works on Windows 10. https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/

It doesn't work on Windows 10. I have tried this multiple times, the only thing that worked was using the Ubuntu 20 and the method the author said above. Please don't remove the gist.

@dominsights
Copy link

Nevermind, I forked it.

@redlinejoes
Copy link

redlinejoes commented Jan 13, 2023

Please read the documentation for WSL upgrades.

Check your version of WSL on Windows 10 by using the following command:
wsl --version

If that command does not return WSL version: 1.0+, you do not have the correct version of WSL.

You can fork this Gist, but you'll still be using deprecated code and methods that are no longer supported and should not be used.

If you want support for WSL, we can help you.
What is the actual issue you are experiencing?
What exactly does not work on Windows 10?
What did you specifically try that failed?

Post the response to the wsl --version command and also check that you have properly configured systemd using the Microsoft supported and recommend method using "/etc/wsl.config"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment