Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WSL 2 - Enabling systemd

Enable systemd in WSL 2

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/46085955818d83b483fd62c65383a7f8dda84f5f/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
GENIE_VERSION="1.23" # update if you need
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 fixDebDependencies() {
# repackage systemd-genie to require dotnet 3.1 instead of 3.0
rm -rf "${GENIE_DIR_PATH}"
mkdir -p "${GENIE_DIR_PATH}"
dpkg-deb -x "${GENIE_FILE_PATH}" "${GENIE_DIR_PATH}"
pushd "${GENIE_DIR_PATH}"
dpkg-deb -e "${GENIE_FILE_PATH}"
popd
sed -i 's/dotnet-runtime-3[.]0/dotnet-runtime-3.1/g' "${GENIE_DIR_PATH}/DEBIAN/control"
rm -f "${GENIE_FILE_PATH}"
pushd /tmp
dpkg-deb -b "${GENIE_DIR_PATH}"
popd
rm -rf "${GENIE_DIR_PATH}"
}
function downloadDebPackage() {
rm -f "${GENIE_FILE_PATH}"
pushd /tmp
wget --content-disposition \
"https://packagecloud.io/arkane-systems/wsl-translinux/packages/debian/bookworm/${GENIE_FILE}.deb/download.deb"
popd
}
function installDependencies() {
# install systemd-genie dependencies
sudo apt-get update
wget --content-disposition \
"https://packages.microsoft.com/config/ubuntu/20.04/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-3.1
sudo rm -f /usr/sbin/daemonize
sudo ln -s /usr/bin/daemonize /usr/sbin/daemonize
}
function main() {
installDependencies
downloadDebPackage
fixDebDependencies
installDebPackage
}
main
@akbarhabiby

This comment has been minimized.

Copy link

@akbarhabiby akbarhabiby commented Dec 7, 2020

Hello, i had a problem, after shutdown the wsl using wsl --shutdown and try to run using wsl genie -s. I had a message genie: must execute as root - has the setuid bit gone astray?

Can you help me? Thanks!

@PavelSosin-320

This comment has been minimized.

Copy link

@PavelSosin-320 PavelSosin-320 commented Dec 18, 2020

I just upgraded Ubuntu distro to Ubuntu 20.10 groovy and it doesn't start systemd anymore.
In the Groovy WSL distro wsl-translinux package is missed and I can't add it because of any attempt to create a file in /etc/apt/sources.list.d results in error "read-only filesystem" Does a way to install genie manually exist?
P.S. Regardless of Ubuntu upgrade method!

@lss233

This comment has been minimized.

Copy link

@lss233 lss233 commented Jan 13, 2021

Hello, i had a problem, after shutdown the wsl using wsl --shutdown and try to run using wsl genie -s. I had a message genie: must execute as root - has the setuid bit gone astray?

Can you help me? Thanks!

@akbarhabiby Try to change the version of system-genie.
It worked for me to install the latest version manually.
You can check it out here: https://github.com/arkane-systems/genie/releases

@PavelSosin-320

This comment has been minimized.

Copy link

@PavelSosin-320 PavelSosin-320 commented Jan 13, 2021

OK, Systemd-genie simulates Boot and not Login. It requires -u root of course to run systemd, execute all installed units, etc. Then you can create your user using addusr, change password, .... Next time you can "Login" using wsl -d ... -u user or WT or switch user when you are already inside Linux. su -newuser. Everything as usual. But if you want to start services via systemd you must be root.
Actually, all installed services that are not disabled will start without your intervention according to their configuration.
P.S. I upgraded my Ubuntu 20.04 to 20.10. So I had to upgrade systemd-genie to the corresponding version, 1.29 and DotNet runtime 5.0 to fulfill its requirement. Everything works very stably. Please, take into account that 20.04 is close to end-of-support.

@QiuChen-Developer

This comment has been minimized.

Copy link

@QiuChen-Developer QiuChen-Developer commented Jan 15, 2021

It works,Thanks

@danthe1st

This comment has been minimized.

Copy link

@danthe1st danthe1st commented Jan 15, 2021

I had the same problem as @akbarhabiby and sudo apt --fix-broken install fixed it but genie -s was stuck at Waiting for systemd, afterwards.

@HaneulKim214

This comment has been minimized.

Copy link

@HaneulKim214 HaneulKim214 commented Jan 28, 2021

/tmp/install-sg.sh && rm /tmp/install-sg.sh outputs ERROR 429: Too Many Requests... Also we are supposed to run these commands on ubuntu right?

@nazaruuyee

This comment has been minimized.

Copy link

@nazaruuyee nazaruuyee commented Jan 28, 2021

Yeaa... outputs ERROR 429: Too Many Requests also here

@jimmyleith

This comment has been minimized.

Copy link

@jimmyleith jimmyleith commented Jan 29, 2021

Same issue here, 429 Too many requests

@techorix

This comment has been minimized.

Copy link

@techorix techorix commented Feb 2, 2021

dito

@sososinelu

This comment has been minimized.

Copy link

@sososinelu sososinelu commented Feb 2, 2021

Same here. Still too many requests :|

@baras

This comment has been minimized.

Copy link

@baras baras commented Feb 4, 2021

Yup, I also get ERROR 429: Too Many Requests. from packagecloud.io
If you browse to that URL it says Bandwidth or Storage Limit Exceeded.
I will try downloading the .deb from github and installing manually.

@FastStonewkx

This comment has been minimized.

Copy link

@FastStonewkx FastStonewkx commented Feb 4, 2021

thanks,it works.
image

I update the wsl2 kernel
image
,I change the linux default dns namespace,
8,8,8,8 and 8,8,4,4 while having too many requests, or maybe chang source.list?
or check it.
https://packagecloud.io/arkane-systems/wsl-translinux/packages/debian/buster/systemd-genie_1.31_amd64.deb

@Jenishkubavat

This comment has been minimized.

Copy link

@Jenishkubavat Jenishkubavat commented Feb 12, 2021

Capture54
I'm facing this problem sorry i just enter Linux world please forgive also give solution for 404 in/tmp/install-sg.sh && rm /tmp/install-sg.sh

@empetit

This comment has been minimized.

Copy link

@empetit empetit commented Feb 12, 2021

Solution regarding the 404 as of today

Inside wsl

(https://packagecloud.io/arkane-systems/wsl-translinux/packages/debian/buster/systemd-genie_1.31_amd64.deb)

curl -s https://packagecloud.io/install/repositories/arkane-systems/wsl-translinux/script.deb.sh | sudo bash
sudo apt-get install systemd-genie=1.31

In powershell

wsl --shutdown
wsl genie -s

Wait for it to load ( https://github.com/arkane-systems/genie#warning-timing-out )

inside wsl

systemctl status
@fliespl

This comment has been minimized.

Copy link

@fliespl fliespl commented Feb 14, 2021

I have used gist + fix from @empetit, but it hangs with these services:

root@flies-pc-wsl:~# systemctl list-units --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● ssh.service loaded failed failed OpenBSD Secure Shell server
● systemd-remount-fs.service loaded failed failed Remount Root and Kernel File Systems
● multipathd.socket loaded failed failed multipathd control socket

While ssh is obvious (ssh-keygen -A + restart fixes it).

I haven't found a way to fix systemd-remount + multipathd. Any ideas?

systemd-remount:
Feb 14 19:38:24 flies-pc-wsl systemd-remount-fs[61]: mount: /: can't find LABEL=cloudimg-rootfs

I can get around it using:

sudo systemctl disable ssh.service multipathd.service multipathd.socket```

but there is probably more clean solution?
@shakahl

This comment has been minimized.

Copy link

@shakahl shakahl commented Feb 23, 2021

I have used gist + fix from @empetit, but it hangs with these services:

root@flies-pc-wsl:~# systemctl list-units --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● ssh.service loaded failed failed OpenBSD Secure Shell server
● systemd-remount-fs.service loaded failed failed Remount Root and Kernel File Systems
● multipathd.socket loaded failed failed multipathd control socket

While ssh is obvious (ssh-keygen -A + restart fixes it).

I haven't found a way to fix systemd-remount + multipathd. Any ideas?

systemd-remount:
Feb 14 19:38:24 flies-pc-wsl systemd-remount-fs[61]: mount: /: can't find LABEL=cloudimg-rootfs

I can get around it using:

sudo systemctl disable ssh.service multipathd.service multipathd.socket```

but there is probably more clean solution?

Solution:

sudo systemctl mask systemd-remount-fs.service
@israelak

This comment has been minimized.

Copy link

@israelak israelak commented Mar 2, 2021

I'm having this problem when I run /tmp/install-sg.sh

image

@jhallesy

This comment has been minimized.

Copy link

@jhallesy jhallesy commented Mar 13, 2021

This script breaks my recently installed and fully updated WSL2 ubuntu 18.04 image. If you follow the directions,
you will end up with an endless loop of "sleeping for 1 second for systemd to settle". I tried it twice, thinking I didn't
follow directions correctly the first time.

You cannot get into the image to undo any changes as there is no grub prompt.

This should be easy to reproduce.

@mpp04

This comment has been minimized.

Copy link

@mpp04 mpp04 commented Mar 18, 2021

Thank you very much for your solution! However, I am getting a 404 towards the very end. Observe:

image
EDIT:

I solved my problem by installing genie thus:

echo "deb [trusted=yes] https://wsl-translinux.arkane-systems.net/apt/ /" | sudo tee /etc/apt/sources.list.d/wsl-translinux.list > /dev/null
sudo apt update
sudo apt install -y systemd-genie

source: https://oglimmer.medium.com/a-working-wsl-2-ubuntu-development-setup-332e64034e5

@JaviMuller

This comment has been minimized.

Copy link

@JaviMuller JaviMuller commented Apr 2, 2021

Please, update the links:
Connecting to packagecloud.io (packagecloud.io)|54.183.38.243|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2021-04-02 22:06:06 ERROR 404: Not Found.

@nagkumar

This comment has been minimized.

Copy link

@nagkumar nagkumar commented Apr 3, 2021

C:\temp>wsl genie -s
/bin/sh: genie: not found

who to resolve genie not found. what is genie?

@msellars-GH

This comment has been minimized.

Copy link

@msellars-GH msellars-GH commented Apr 19, 2021

This script breaks my recently installed and fully updated WSL2 ubuntu 18.04 image. If you follow the directions,
you will end up with an endless loop of "sleeping for 1 second for systemd to settle". I tried it twice, thinking I didn't
follow directions correctly the first time.

You cannot get into the image to undo any changes as there is no grub prompt.

This should be easy to reproduce.

Hi, I have the same problem, is there any way to recover ? I can't get into the WSL image to undo anything ........ and I can't even edit the files from Windows, as I don't have permissions - is there an easy way to recover this ?

@msellars-GH

This comment has been minimized.

Copy link

@msellars-GH msellars-GH commented Apr 19, 2021

This script breaks my recently installed and fully updated WSL2 ubuntu 18.04 image. If you follow the directions,
you will end up with an endless loop of "sleeping for 1 second for systemd to settle". I tried it twice, thinking I didn't
follow directions correctly the first time.
You cannot get into the image to undo any changes as there is no grub prompt.
This should be easy to reproduce.

Hi, I have the same problem, is there any way to recover ? I can't get into the WSL image to undo anything ........ and I can't even edit the files from Windows, as I don't have permissions - is there an easy way to recover this ?

I managed to get back into WSL by typing "wsl -u root" in Windows Powershell.

To stop the systemd script running: Edit bash.baschrc using "nano /etc/bash.bashrc"
Put a # on the start of the line that says "source /usr/sbin/start-systemd-namespace"

@BroVic

This comment has been minimized.

Copy link

@BroVic BroVic commented Apr 28, 2021

@mpp04

Hi. I tried your solution but the host for https://wsl-translinux.arkane-systems.net could not be resolved and thus apt update failed. Any suggestions?

apostle@NARC-main:/tmp$ sudo apt update
Hit:1 http://repo.mysql.com/apt/debian buster InRelease
Err:2 https://wsl-translinux.arkane-systems.net/apt  InRelease
  Could not resolve 'wsl-translinux.arkane-systems.net'
Hit:3 http://security.debian.org/debian-security buster/updates InRelease
Hit:4 http://ftp.debian.org/debian buster-backports InRelease
Hit:5 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease
Hit:6 http://deb.debian.org/debian buster InRelease
Hit:7 http://deb.debian.org/debian buster-updates InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
W: Failed to fetch https://wsl-translinux.arkane-systems.net/apt/InRelease  Could not resolve 'wsl-translinux.arkane-systems.net'
W: Some index files failed to download. They have been ignored, or old ones used instead.

I'm using WSL2 with Debian 10

Update:

Okay, I was able to install genie using the instructions here: https://github.com/arkane-systems/genie

@tomdecouttere

This comment has been minimized.

Copy link

@tomdecouttere tomdecouttere commented May 6, 2021

Genie v1.40 and Ubuntu21.04 won't work with genie -s

~$ sudo systemctl disable ssh.service multipathd.service multipathd.socket
[sudo] password for tom:
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable ssh
~$ sudo systemctl mask systemd-remount-fs.service
~$ 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):
Failed to list units: Transport endpoint is not connected
genie: running command failed; nsenter for systemctl list-units --failed returned 1.

@sjyi

This comment has been minimized.

Copy link

@sjyi sjyi commented May 29, 2021

... Please, take into account that 20.04 is close to end-of-support....

I don't know what you mean by "close" but End of Life for 20.04 is Apr, 2025 and Security Maintenance is until Apr, 2030

https://ubuntu.com/about/release-cycle

@raviduddela

This comment has been minimized.

Copy link

@raviduddela raviduddela commented Jun 7, 2021

systemd-genie should be installed on linux system or windows?
I installed on linux which is running on my windows machine.
then i stopped linux machine using wsl --shutdown from powershell on windows.
then from power shell i ran wsl genie -s but iget the below error
wsl genie -s
/bin/sh: genie: not found.
can anyone help?

@tomdecouttere

This comment has been minimized.

Copy link

@tomdecouttere tomdecouttere commented Jun 7, 2021

@nareshvjn

This comment has been minimized.

Copy link

@nareshvjn nareshvjn commented Jun 17, 2021

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.service loaded failed failed Device-Mapper Multipath Device Controller
● systemd-modules-load.service loaded failed failed Load Kernel Modules
● systemd-remount-fs.service loaded failed failed Remount Root and Kernel File Systems
● 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.

4 loaded units listed.

Pls give solution to this error

@MikuAuahDark

This comment has been minimized.

Copy link

@MikuAuahDark MikuAuahDark commented Jul 22, 2021

The installation is not completed because packages-microsoft-prod messes up with apt and errors with "Method https has died unexpectedly!".

Ubuntu 20.04.2

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