Score:7

System has not been booted with systemd as init system (PID 1). Can't operate

ca flag

I use WSL2 on Windows 11. I want to run the systemctl command in Ubuntu 20.04, but it gives me the following error:

System has not been booted with systemd as init system (PID 1). 
Can't operate. Failed to connect to bus: Host is down

How can I fix it?

dylanh724 avatar
br flag
"Why"? He was likely following an install guide. Eg, https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-redis-on-ubuntu-20-04
user535733 avatar
cn flag
The `systemctl` command won't work on WSL without some serious hacking. Not recommended.
WinEunuuchs2Unix avatar
in flag
Why would you want to use `systemctl` on Windows 11? You should be using Windows commands. What are you trying to achieve?
dsha256 avatar
ca flag
I want to restart ssh service in Ubuntu 20.04 which runs using WSL2
ChanganAuto avatar
us flag
@WinEunuuchs2Unix The OP is running Ubuntu in WSL2. The question is not about running systemctl in Windows but in Ubuntu. That said user535733 is absolutely right.
dsha256 avatar
ca flag
Okay, thank you!
user535733 avatar
cn flag
Try using the `service` command instead.
NotTheDr01ds avatar
vn flag
Specifically, `sudo service ssh restart`. But be aware that `ssh` doesn't quite work as you might expect under WSL2 either.
dsha256 avatar
ca flag
Thank you, all!
Score:11
vn flag

Surprisingly, after 5 years or so of WSL, there doesn't seem to be a good, general-purpose "Systemd" question here on Ask Ubuntu. So this looks like a good one to use for that purpose.

The problem

In general, when you see either of the following two messages:

  • System has not been booted with systemd as init system (PID 1). Can't operate.
  • Failed to connect to bus: Host is down

Then it's typically the same root cause. In the case of systemctl and attempting to start ssh, you are seeing both.

The core problem, as mention in the comments, is that WSL doesn't use Systemd, even in distributions where it is the default. Instead, WSL currently uses its own /init process as PID 1, which performs a few WSL-specific tasks that I mention in this answer (so I won't repeat them here).

And while WSL is (IMHO) a great way to have a full-featured Linux CLI in Windows (and recently, even GUI), the lack of Systemd does tend to make things more difficult in distributions that expect it to be there. Fortunately, Ubuntu is pretty good overall about being able to cope without Systemd.

How to handle the lack of Systemd

And regardless, Systemd at its core is just a (probably gross-oversimplification) "way of accomplishing system tasks". There is (usually? always?) a way of doing the same task without Systemd, and often more than one way.

  • Option 1: "The old way"

    In Ubuntu on WSL, many of the common system services still have the "old" init.d scripts available to be used in place of systemctl with Systemd units. You can see these by using ls /etc/init.d/.

    So, for example, you can start ssh with sudo service ssh start, and it will run the /etc/init.d/ssh script with the start argument.

    Even some non-default packages such as MySql/MariaDB will install both the Systemd unit files and the old init.d scripts, so you can still use the service command for them as well.

  • Option 2: "The 'manual' way"

    But some services don't have a init-script equivalent, especially on other distributions. For simplicity, let's assume that the ssh init.d script wasn't available.

    In this case, the "answer" is to figure out what the Systemd unit files are doing and attempt to replicate that manually. This can vary widely in complexity. But I'd start with looking at the Systemd unit file that you are trying to run:

    less /lib/systemd/system/ssh.service
    
    # Trimmed
    [Service]
    EnvironmentFile=-/etc/default/ssh
    ExecStartPre=/usr/sbin/sshd -t
    ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
    RuntimeDirectory=sshd
    RuntimeDirectoryMode=0755
    

    I've trimmed out some of the less relevant lines for understanding its behavior, but you can man systemd.exec, man systemd.service, and others to see what most of the options do.

    In this case, when you sudo systemctl start ssh, it:

    • Reads environment variables (the $SSHD_OPTS) from /etc/default/ssh
    • Tests the config, exits if there is a failure
    • Makes sure the RuntimeDirectory exists with the specified permissions. This translates to /run/sshd (from man systemd.exec). This also removes the runtime directory when you stop the service.
    • Runs /usr/sbin/sshd with options

    So, if you don't have any environment-based config, you could just set up a script to:

    • Make sure the runtime directory exists. Note that, since it is in /run, which is a tmpfs mount, it will be deleted after every restart of the WSL instance.
    • Set the permissions to 0755
    • Start /usr/sbin/sshd as root

    ... And you would have done the same thing manually without Systemd.

    Again, this is probably the simplest example. You might have much more to work through for more complex tasks.

  • Option 3: Run Systemd as PID 1 in a PID namespace/container

    Finally, it's possible to get Systemd running under WSL. This is a fairly advanced topic, although there are multiple scripts and projects that attempt to simplify it. Even so, my personal recommendation is to make sure that you understand what is going on behind the scenes should you use one of these techniques.

    Let's start with some of the more popular projects to enable Systemd in WSL:

    I haven't personally run any of them, but all are open-source, and I've scanned the source to compare the techniques. At the core, each creates a new namespace or container where Systemd can run as PID 1.

    You can see this in action by following the steps:

    • Run:

      sudo -b unshare --pid --fork --mount-proc /lib/systemd/systemd --system-unit=basic.target
      

      This starts Systemd in a new namespace with its own PID mapping. Inside that namespace, Systemd will be PID1 (as it must, to function) and own all other processes. However, the "real" PID mapping still exists outside that namespace.

      Note that this is a "bare minimum" command-line for starting Systemd. It will not have support for, at least:

      • Windows Interop (the ability to run Windows .exe)
      • The Windows PATH (which isn't necessary without Windows Interop anyway)

      The scripts and projects listed above do extra work to get these things working as well.

      Wait a few seconds for Systemd to start up, then:

      sudo -E nsenter --all -t $(pgrep -xo systemd) runuser -P -l $USER -c "exec $SHELL"
      

      This enters the namespace, and you can now use ps -efH to see that systemd is running as PID 1 in that namespace.

      At this point, you should be able to run systemctl.

      And after proving to yourself that it's possible, I recommend exiting the WSL instance completely, then doing wsl --terminate <distro> on it. Otherwise, you will have some things be "broken" until you do. They can likely be "fixed", but that's beyond the scope of any one Ask Ubuntu question ;-). My recommendation is to refer to the projects above to see how they handle it.

Timo avatar
bj flag
A good question, and **a good answer**, so plus one. I thought that `service` cmd belongs to `systemd`. I have on wsl the error from the OP, but `mariadb` works with `service`.[service](https://askubuntu.com/a/903405/321926) is a `wrapper` and can be used with `init.d` and `systemctl`.
gunslingor avatar
lk flag
I'm confused. WSL used to have systemctl/d, WSL used to work fine with mounted drives, WSL used to be a very solid representation of Ubuntu... now I'd call it Winbuntu as it seems its no longer ubuntu but windows version of it corrupted to fit in WSL... which is strange, because I remember when WSL 1 came out I was able to install TRUE ubuntu on it without issue through powershell instead of the windows store. What happened to WSL? Is there any way to get true ubuntu back?
NotTheDr01ds avatar
vn flag
@gunslingor I agree ... you're confused ;-). But seriously, you are almost certainly remembering this incorrectly (or perhaps nostalgically). WSL has never had Systemd support. [Here's the Github issue](https://github.com/microsoft/WSL/issues/994) on the topic, opened just 15 days after the first WSL1 beta was made available.
NotTheDr01ds avatar
vn flag
A related comment in that issue, *"running WSL without cron or syslog or sshd or a terminal emulator (what we have got is sort of a console emulator) and other servers/daemons/background tasks, is not the same as running an Ubuntu developer machine"*, shows that people didn't considered it "real Ubuntu" even back then. WSL has made great progress over the years since then, but it's still a *developer tool* on Windows, not a virtual machine designed to replicate a "full Ubuntu (or other Linux) environment."
gunslingor avatar
lk flag
Yeah, hyperV is probably what i was remembering, pure ubuntu image. I suspect WSL is using some component of that pretweaked and routed to the host system to serve itself easier... or lxssmanager or something, i forget. Docker seems to run better in wsl ubuntu than as docker destop using wsl and then you also have very slow ubuntu when using a mounted windows share, but fast ubuntu and win when using a mounted ubuntu share... why I suspect its hyperV really... I remember this issue.
NotTheDr01ds avatar
vn flag
@gunslingor Right- Hyper-V would make sense, as that's really a "pure VM" in that case. Side note - LxssManager is really just the service behind WSL.
LingYan Meng avatar
br flag
For option 3, there is a script for it, and it works for me: http://wiki.webperfect.ch/index.php?title=WSL:_System_has_not_been_booted_with_systemd_as_init_system_(PID_1)#:~:text=Failed%20to%20connect%20to%20bus%3A%20Host%20is%20down,is%20not%20enabled%20by%20default%20in%20WSL%202. also apply the fix: https://github.com/DamionGans/ubuntu-wsl2-systemd-script/issues/36
NotTheDr01ds avatar
vn flag
@LingYanMeng There are quite a few scripts, and some are more robust than others. With *all* of them, I recommend understanding *how* they work, because none of them will work in all cases and eventually require some troubleshooting.
Quolonel Questions avatar
ph flag
How do I know whether I have the inbox version or not? `wsl --version` prints: `Invalid command line option: --version`.
NotTheDr01ds avatar
vn flag
@QuolonelQuestions Great question - The results there indicate the In-box version. If you had the Store version, it would return the actual version (and some additional info).
Arthur Lacoste avatar
my flag
You (really good) answer not cover another way to launch services at startup with the `/etc/wsl.conf` file. You can add this kind of directive : [boot] command="service apache2 start; service mariadb start" [Source](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings)
NotTheDr01ds avatar
vn flag
@ArthurLacoste Good point - I focused on *starting* services in this answer, but not how to *autostart* services as boot. I've added a link to another answer that covers that per your recommendation. Thanks!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.