why systemd is disabled in WSL?

87,772

Solution 1

Systemd is not supported in WSL at this time. More information can be found with this GitHub issue.

Furthermore, Docker does not (at the time of writing this) work on top of WSL, which means Kubelet won't be of much use. In order to run Kubernetes locally, you can use Docker for Windows which includes Kubernetes support, or you can use Minikube to run a VM with Hyper-V or Virtualbox.

Solution 2

When using WSL2 you can use:

sudo service docker start

This command basically execute the script /etc/init.d/docker.

Some customization, like specifying HTTP proxy, is possible via the script /etc/default/docker.

Solution 3

Windows Subsystem for Linux (WSL) 2 introduces a significant architectural change as it is a full Linux kernel built by Microsoft, allowing Linux containers to run natively without emulation.

Before you install the Docker Desktop WSL 2 backend, you must complete the following steps:

Install Windows 10, version 1903 or higher. Enable WSL 2 feature on Windows.

Source - Docker Desktop WSL 2 backend

To find out which version of Windows your device is running, press the Windows logo key + R, type winver in the Open box, and then select OK.

Systemd is NOT supported in WSL but there is a workaround for this - Script to enable systemd support on current Ubuntu WSL2 images from the Windows store.

Solution 4

Hack Systemd in WSL2

Systemd is not native in WSL2, but Shayne found a way to hack it: https://github.com/shayne/wsl2-hacks

The experience is quite destabilizing on firt approch but it works for most of my usage: docker, minikube (--driver=none), systemd services.

PS: Mind to always connect to your user (bash $USER) before using it. Otherwise you won't have much access to your profile configurations (~/.profile or ~/.bash_profile).


Using Systemd Genie

Arkane published a way to orchestrate namespace (or bottle) in systemd for WSL2: https://github.com/arkane-systems/genie

After installing connect to your profile through genie:

genie -c bash

At this time, only Ubuntu 18.04 and 19.10 works. There is no package for Ubuntu 20.04 for the moment (I am exciting this moment).

Docker and Minikube also work in this configuration as native (--driver=none).

Solution 5

You can boot systemd fairly easily in WSL2 using bubblewrap:

# pacman -S bubblewrap  # or apt, etc.
# bwrap --dev-bind / / --unshare-pid --as-pid-1 /usr/lib/systemd/systemd

It will not print anything to the current TTY if it starts successfully, but if you run htop etc. in another TTY, you will see that it booted and started configured services.

You can then manually enable OpenSSHd by symlinking /usr/lib/systemd/system/sshd.service to /etc/systemd/system/multi-user.target.wants/. Configure your keys in /root/.ssh, start systemd, and you should be able to SSH in.

Share:
87,772

Related videos on Youtube

Santosh Hegde
Author by

Santosh Hegde

Updated on December 12, 2021

Comments

  • Santosh Hegde
    Santosh Hegde over 2 years

    None of the systemd commands are working inside WSL( Ubuntu Bash 18.04). When I ran sudo systemctl is-active kubelet, error is output: System has not been booted with systemd as init system (PID 1). Can't operate. : running command: sudo systemctl is-active kubelet

    How to enable systemd feature in WSL? Whats the way to get rid of System has not been booted with systemd

  • Leo
    Leo about 4 years
    Docker now has experimental support for WSL: docs.docker.com/docker-for-windows/wsl-tech-preview
  • Markus Zeller
    Markus Zeller almost 4 years
    This is working for all the other installed services like apache, memcached, etc. You can do this WSL (1), too.
  • Dee
    Dee over 3 years
    was microsoft 'init' replaced by a true linux 'init' in WSL2? or still microsoft 'init'?
  • Samuel Nwaokoro
    Samuel Nwaokoro almost 3 years
    On windows, use this command - sudo service, instead of systemctl For example: sudo service docker start sudo service ssh start
  • NotTheDr01ds
    NotTheDr01ds over 2 years
    @datdinhquoc A bit late, but (in case you are still wondering) it's still Microsoft init in WSL2, and as of now there isn't any indication from Microsoft that it's going to change anytime soon. They are still investigating how to support Systemd, but haven't announced any plans at this time.
  • Vladimir Panteleev
    Vladimir Panteleev over 2 years
    Done, though I think this will work in any compliant Linux environment. :)
  • NotTheDr01ds
    NotTheDr01ds over 2 years
    Cool - I'll have to give it a try soon! I've learned to make do without Systemd, but there are certainly times it would make life easier under WSL.
  • NotTheDr01ds
    NotTheDr01ds over 2 years
    Finally got around to trying this, at least with nsenter to join the namespace instead of ssh'ing in. One question (for now) -- Doesn't your example bwrap commandline need --proc /proc? Otherwise it seems like the existing /proc gets used, which means that PID1 in that /proc is still /init.
  • NotTheDr01ds
    NotTheDr01ds over 2 years
    Also, in my testing this bwrap command creates a rootfs that is nosuid, meaning that you can't run as a regular user inside it and use sudo. Is that your experience? From your instructions, it sounds like you may just avoid this by running as root inside the namespace?