48

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

Santosh Hegde
  • 3,030
  • 9
  • 32
  • 46

5 Answers5

35

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.

Gian Marco
  • 20,365
  • 8
  • 51
  • 44
32

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.

DWSR
  • 611
  • 6
  • 7
  • 9
    Docker now has experimental support for WSL: https://docs.docker.com/docker-for-windows/wsl-tech-preview/ – Leo Feb 15 '20 at 13:35
  • 1
    was microsoft 'init' replaced by a true linux 'init' in WSL2? or still microsoft 'init'? – Dee Jan 12 '21 at 10:53
  • 1
    @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. – NotTheDr01ds Aug 21 '21 at 18:19
7

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.

Nikita Fedyashev
  • 16,963
  • 11
  • 45
  • 76
Aditya Mohan
  • 81
  • 1
  • 4
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).

1

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.

Vladimir Panteleev
  • 24,384
  • 6
  • 67
  • 112
  • Done, though I think this will work in any compliant Linux environment. :) – Vladimir Panteleev Aug 21 '21 at 20:02
  • 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 Aug 21 '21 at 20:05
  • 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 Sep 14 '21 at 18:18
  • 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? – NotTheDr01ds Sep 14 '21 at 18:23