56

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,420
  • 10
  • 35
  • 51
  • Does this answer your question? [Enable Systemd in WSL 2](https://stackoverflow.com/questions/65400999/enable-systemd-in-wsl-2) – NotTheDr01ds Oct 07 '22 at 21:49

6 Answers6

46

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
  • 22,140
  • 8
  • 55
  • 44
33

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
  • 631
  • 6
  • 7
  • 10
    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.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
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,651
  • 6
  • 70
  • 114
  • 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
0

There are two questions you seem to have asked:

  • From the question title: Why Systemd is disabled in WSL
  • From the question body: How to enable Systemd in WSL

The answer to "Why" is because, historically, the WSL architecture required that its init process be PID 1. Systemd also requires that it run as PID 1 for pretty much all of its functionality (other than some small subsets like tmpfiles).

As to how, there are some answers here already, obviously, as well as some new answers (see below). To avoid repeating these in detail across some 35+ Systemd-related questions here on Stack Overflow, a Community Wiki answer has been posted on the question Enable systemd in WSL 2.

To summarize:

  • Option 1: Upgrade WSL to the latest version (if supported by your system) and opt-in to the Systemd feature
  • Option 2: Run a Systemd-helper script designed for WSL2
  • Option 3: Manually run Systemd in its own namespace
  • Alternative 1: SysVInit scripts (e.g. sudo service <service_name> start) where available
  • Alternative 2: Manually configuring and running the service
  • Alternative 3: Docker

Please see the Community Wiki for more details.

While new, individual additional answers are welcome, it is hoped that they don't repeat any of the options above. It is also encouraged that if new information is available, it is edited into the Community Wiki.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70