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
Note that any
--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.