11

How do reboot the current WSL instance from the WSL console?

The usual 'suspects' are not working:

reboot
shutdown -r

They fail with the messages:

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

Is there any other way to force WSL to reboot?

1
  • 1
    Try the force, Luke: reboot -f. See also my answer below. (But: I did not try this in WSL1) And AFAICS: This does only shutdown without reboot at my side.
    – Tino
    Jan 10, 2023 at 20:05

3 Answers 3

11

You would need to do so via Windows cmd or powershell with;

> wsl --shutdown

If you have numerous instances and need to shutdown a specific instance or distribution, you can use;

> wsl -l -v

to see distribution names, the state (whether running or stopped) and the wsl version and shut it down with

> wsl -t NAME
1
  • 2
    I know and use all that options... and thare is even a 'hard' way that is kill the windows service that hosts WSL... what I'm seeking (and I think is explicit) is a way to reboot the Kernel from inside WSL,,, to demonstrate a use case... imagine you have a script operating under WSL and you want to reboot from that script...
    – ZEE
    Nov 15, 2022 at 21:15
2

Following works for me with WSL2 under Windows 11 from within Linux:

sudo poweroff -f

This powers off the Linux kernel directly without trying to do it via some missing init like SystemD.

Note that it may take some time (at least 8s, but I also observed 1min) until the instance can be started again. In the meanwhile, when you try to access the instance, you will observe something like following (this is from German Windows install with Windows Terminal set to German, too):

Schwerwiegender Fehler
Error code: Wsl/Service/E_UNEXPECTED

[Verarbeitung des Prozesses mit Code 4294967295 (0xffffffff) beendet]

Don't worry. Just sit and wait a bit longer and try again.

If you are curious:

strace poweroff -f

shows how that works. The last lines you see will be something like:

sync()                                  = 0
reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_CAD_ON) = -1 EINVAL (Invalid argument)
writev(2, [{iov_base="Powering off.", iov_len=13}, {iov_base="\n", iov_len=1}], 2Powering off.
) = 14
reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF) = ?
+++ exited with 0 +++

[Verarbeitung des Prozesses mit Code 1 (0x00000001) beendet]
4
  • Keep in mind that this question was specifically around reboot (not shutdown). I made the same mistake in my answer ;-) Jan 11, 2023 at 17:34
  • GREAT!!! this really works... I had tryed "poweroff" but gave up with the error messages about no "systemd" present... never thought of trying "-f" anyway,,, THANKS!
    – ZEE
    Feb 14, 2023 at 12:44
  • I assumed since it 'grunts' that it can't talk with the deamon it will never work,,, <<<< System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down Failed to talk to init daemon: Host is down >>>>
    – ZEE
    Feb 14, 2023 at 12:45
  • @NotTheDr01ds Correct! You are right that one cannot issue a reboot from within WSL directly. However, as Changes to /etc/wsl.conf require WSL to restart and I like to bring up WSL after the host reboots, too, I have an entry in Task Scheduler which starts WSL if it is down (i.E. just run bash.exe /etc/init.d/cron start every 5 minutes and let cron start everything else with @reboot). Hence shutdown -f actually does a reboot at my side - but now I lack a good solution to really shutdown WSL permanently (; who wants this anyway? ;)
    – Tino
    Feb 20, 2023 at 21:25
1

First, it's important to understand that, when you are running a Linux distribution in WSL, it's not like running on a physical, or even virtual, machine. What you are actually in is a container, using similar technologies (namespaces, cgroups, etc.) as Docker or most other container systems.

Attempting to "shut down" a WSL distribution would be like attempting to "shut down" a Docker/Podman/other container. If Ubuntu is running in Docker, you don't shut it down from Ubuntu, but by either:

  • Executing a docker stop command from outside the container
  • Or simply exiting all running processes inside the container

It's the same with WSL. You can either:

  • Stop all running processes in the distribution, exit all interactive shells, and WSL should self-terminate the distribution after 15 seconds. Note that Docker Desktop will keep any distribution running, however, since its background processes continue to run.

  • Forcibly terminate the distribution. While this is typically done from PowerShell or CMD via wsl --terminate <distroname>, it can also be done from inside the distribution using:

    wsl.exe --terminate $WSL_DISTRO_NAME
    
  • Shut down the WSL system entirely with wsl --shutdown. Again, within WSL itself, you must use the full executable name with extension, so wsl.exe --shutdown.

Note that any --terminate or --shutdown will forcibly stop the distribution. This is not a graceful termination. All running processes will end immediately, without being given the opportunity to respond to any normal signals.

For graceful shutdown (copying from my AU answer here), under recent WSL releases in Windows 11, you can now run Systemd. Services started by Systemd under WSL2 are automatically terminated when no other processes (other than those started by Systemd, essentially) are running in Ubuntu.

This shutdown is graceful. WSL2 communicates with Systemd to have it gracefully stop (via normal Systemd signal behavior) any service running under it.

This still isn't necessarily optimal, since you need one additional process running in the background (started "interactively") to keep WSL running in the meantime. See this answer for a solution on how to do this. If you use something like keychain as I mention there, then you just need to stop that process (keychain -k all), exit the shell in Ubuntu, and within 15 or so WSL2 should send the "graceful shutdown" signal to Systemd.

4
  • I have no problem managing WSL from the windows side... I understand WSL2 is inherently a VM... and I reboot Linux in my VMs!!! essentially I understand that the absence of PID#1 control is the essence of the matter,,, and that maybe this will have to pass through a WSL internal addition... the problem is that some Linux scripts I have need to reboot the kernel after making some changes,,, and that's the only annoyance I have to put them to work in WSL,,, meanwhile if somebody knows a hack to do this REBOOT I beg him/her to share it with us...
    – ZEE
    Nov 15, 2022 at 21:25
  • 1
    @ZEE As far as a reboot hack goes, here's one (still off-topic on Stack Overflow, but never got closed). Hope that works for your use-case! Nov 15, 2022 at 21:39
  • that is good info... I'll see what I can do with... thanks!
    – ZEE
    Nov 15, 2022 at 21:56
  • 1
    @ZEE And apologies -- I didn't re-read your question here closely enough. I was still answering the "shutdown" question you asked on SO rather than this one, which was clearly about "reboot" :-/ Nov 15, 2022 at 22:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .