- Shutdown or Reboot a WSL session from inside the WSL session
- 2 Answers 2
- To shutdown a session from within a WSL guest, you can run:
- Creating an Alias
- How to Shutdown and Start WSL 2 Using Linux Shell: A Beginner’s Guide
- How to Shutdown a Linux Distro on WSL
- How to Restart WSL after Shutdown
- How to Shutdown WSL from Inside the Linux Distro
- Other Useful Commands for WSL
- Advanced Settings and Troubleshooting Tips for WSL
- Other helpful code examples for shutting down and starting WSL 2 using Linux shell
- Conclusion
- How to shutdown Linux distros on WSL
- Terminate a Linux distro on WSL
- Terminate all Linux distros on WSL
- Related Stories
Shutdown or Reboot a WSL session from inside the WSL session
I would like to be able to reboot WSL sessions. To do so is a little awkward as WSL does not use systemd so we cannot use reboot . Within a WSL session, we can run any Windows executable:
boss@Asus: ~ $ wsl.exe -l -v NAME STATE VERSION * Ubuntu-20.04 Running 2 fedoraremix Stopped 1 Alpine Stopped 1 Ubuntu Stopped 1
- Run an asynchronous command that will initiate a new session 5-10 seconds in the future to allow the previous session to fully shutdown (and that will not terminate when this session is terminated).
- Terminate the currently running session with wsl.exe -t .
- A few seconds later, the new session will start up.
It sounds like you are looking for $WSL_DISTRO_NAME ? See this answer for an example of its usage with wsl.exe from inside the WSL session. Your «async reboot» logic looks like it is the right track to me. The challenge will be in starting a process that survives the WSL instance termination. Likely it will be PowerShell-based, but it may even need to be a Windows Scheduled Task to survive — Not sure.
I think this’ll do the job. From within WSL, run cmd.exe /c «wsl.exe —terminate $WSL_DISTRO_NAME» to terminate current WSL distro.
Perfect, thanks @NotTheDr01ds, I did not know about that variable. Handles the shutdown perfectly thanks.
@Umair, you are incorrect in your answer for the cmd.exe /c part. Try it and see. WSL allows running commands from Windows without having to have the cmd.exe part. You can essentially run any Windows binary that is on the Windows path directly. If you do echo $PATH , you will see that all of the Windows paths are imported there when the WSL session starts.
2 Answers 2
Credits to the commenters above.
To shutdown a session from within a WSL guest, you can run:
wsl.exe --terminate $WSL_DISTRO_NAME
Rebooting is also possible, however so far I do not know how to get a new terminal inside the same console window. The following will reboot the WSL guest and open a new console window of it when it has finished:
cd /mnt/c/ && cmd.exe /c start "rebooting WSL" cmd /c "timeout 5 && wsl -d $WSL_DISTRO_NAME" && wsl.exe --terminate $WSL_DISTRO_NAME
Explanation:
- From the perspective of Windows, WSL systems are mounted as network resources. cmd does not support the resulting UNC path formats such as \\wsl$\Debian\ . Therefore it may be best to cd to a directory it can resolve as a Windows path instead, such as C:\ , before it is executed. If ommited, cmd will complain and change its directory to cmd ‘s %windir% .
- && runs another command after the previous one has finished in linux and windows cmd .
- cmd.exe /c starts a cmd instance and tells it to execute a command that follows.
- start «» . is a cmd -internal command to run another program inside its own window, independent of the cmd instance. In this case the program is another cmd window. It will not be affected when WSL shuts down.
- In the original Linux-Terminal, the first cmd /c command has finished, and the third command after && shuts down the guest like above.
- The second cmd window waits for a few seconds, then starts a new WSL session of the same WSL machine.
Creating an Alias
You can make this easier to use by creating an alias. For bash users, edit your ~/.bashrc file and apply the changes afterwards:
nano ~/.bashrc && source ~/.bashrc
Add either or both of the lines below anywhere in the file.
You can of course choose any name you want. Both shutdown and reboot exist as systemd commands, but since they do not work in WSL machines, you can replace them with an alias as follows:
alias shutdown='wsl.exe --terminate $WSL_DISTRO_NAME' alias reboot='cd /mnt/c/ && cmd.exe /c start "rebooting WSL" cmd /c "timeout 5 && wsl -d $WSL_DISTRO_NAME" && wsl.exe --terminate $WSL_DISTRO_NAME'
This is really good. I adjusted it a little so that the title of the Window is renamed to represent the distro. cd /mnt/c/ && cmd.exe /c start «Rebooting WSL» cmd /c «timeout 5 && title «$WSL_DISTRO_NAME» && wsl -d $WSL_DISTRO_NAME» && wsl.exe —terminate $WSL_DISTRO_NAME
Does not work: wsl.exe: command not found . Note: For security reason /mnt/c and executing Windows binaries is administratively disabled (on the Windows side). All I have is ssh access to the Linux, with full root. If you want to try it yourself: In /etc/wsl.conf set [automount] enabled=false and [interop] enabled=false
@Tino I guess it might not work if you are not starting from a shell that has windows admin privileges. Definitely still works for me, and I always use an admin terminal on windows.
any ideas on how to bypass AUTOMOUNT DISABLED? I have the same problem because I do not want to have the widows drives (c, d, etc) exposed to WSL.
Expanding on the ansver from @BasementScience
To manage a remote WSL I’ve set up a Windows TaskScheduler job to start wsl with an /etc/init-wsl which in turn starts cron , ssh , rsyslog and autossh (so I can connect back to WSL).
So naturally I’m keen to get these processes started also on a remote WSL reboot, so I’m able to login again afterwards.
# Added to $HOME/.bashrc - Renamed aliases to separate from OS commands alias wslshutdown='wsl.exe --terminate $WSL_DISTRO_NAME' alias wslreboot='cd /mnt/c/ && cmd.exe /c start "Rebooting WSL" cmd /c "timeout 5 && wsl -d $WSL_DISTRO_NAME" -- sudo /etc/init-wsl && wsl.exe --terminate $WSL_DISTRO_NAME'
The detail is here . & wsl -d $WSL_DISTRO_NAME» — sudo /etc/init-wsl & .
This will not start a new shell, but will start my processes so I can login again.
The /etc/init-wsl script have to be created:
sudo touch /etc/init-wsl && sudo chmod 755 /etc/init-wsl # Add services as needed sudo bash -c 'cat /etc/init-wsl service ssh start service cron start EOF'
# Ensure your user (the %sudo group) can sudo the init script without password sudo bash -c 'cat /etc/sudoers.d/user-service %sudo ALL=NOPASSWD: /usr/sbin/service * %sudo ALL=NOPASSWD: /etc/init-wsl EOF'
How to Shutdown and Start WSL 2 Using Linux Shell: A Beginner’s Guide
Learn how to shutdown and start WSL 2 using Linux shell with ease. This beginner’s guide covers important tips and tricks for using WSL effectively.
- How to Shutdown a Linux Distro on WSL
- How to Restart WSL after Shutdown
- How to Shutdown WSL from Inside the Linux Distro
- Other Useful Commands for WSL
- Advanced Settings and Troubleshooting Tips for WSL
- Other helpful code examples for shutting down and starting WSL 2 using Linux shell
- Conclusion
- How do I start wsl after shutdown?
- How do I start shell WSL2?
- How do I shutdown wsl in Linux?
- How do I disable and enable WSL2?
WSL (Windows Subsystem for Linux) is a powerful tool that allows users to run Linux command-line tools and utilities directly on Windows. In this blog post, we will guide you through how to shutdown and start WSL 2 using the Linux shell. We will cover the key points, important points, and helpful points related to this topic, including tips and tricks for using wsl .
How to Shutdown a Linux Distro on WSL
To shutdown a Linux distro on WSL, you need to follow these simple steps:
- Open Command Prompt or PowerShell as administrator.
- Type “wsl –shutdown”.
- This command will shutdown all running Linux distros on WSL.
This command will shut down all running Linux distributions on WSL. It is a quick and easy way to shut down any Linux distro running on WSL.
How to Restart WSL after Shutdown
To restart WSL after shutdown, you can follow these simple steps:
- Right-click the Lxssmanager service and select “Restart”, or type “wsl -t” followed by the distribution name.
- This command will restart WSL and all previously running Linux distros.
This command will restart WSL and all previously running Linux distros. It is a quick and easy way to restart WSL and get back to your work.
How to Shutdown WSL from Inside the Linux Distro
To shut down WSL from inside the Linux distro, you can follow these simple steps:
This command will exit the shell and shut down the WSL. It is a quick and easy way to shut down WSL from inside the Linux distro.
Other Useful Commands for WSL
Apart from shutting down and restarting WSL, there are other useful commands that you can use to manage your WSL environment. Here are some of them:
These commands can be used to terminate a running distro or to restart WSL. They are useful when you want to manage your WSL environment.
Advanced Settings and Troubleshooting Tips for WSL
WSL can be set as the default environment, and Linux GUI apps can be run with WSL. WSL 2 offers improved speed and performance, and systemd support is now available. Troubleshooting tips include restarting the machine after enabling Virtual Machine Platform, and ensuring no background processes are running before exiting the shell.
To set WSL as the default environment, you can follow these steps:
To run linux gui apps with wsl , you need to install an X Server application like VcXsrv or Xming. Once you have installed the X Server application, you can run linux gui apps with the “DISPLAY” environment variable set to “localhost:0”.
To troubleshoot WSL, you can follow these tips:
- Restart the machine after enabling Virtual Machine Platform.
- Ensure no background processes are running before exiting the shell.
Other helpful code examples for shutting down and starting WSL 2 using Linux shell
In shell, shutdown wsl code example
In shell, wsl shutdown reboot code example
Conclusion
WSL is a powerful tool for developers and users who want to run Linux on Windows. Knowing how to shutdown and start WSL 2 using the Linux shell is an essential skill for using this tool effectively. By following the key points, important points, and helpful points outlined in this blog post, you will be able to shutdown and start WSL 2 with ease and troubleshoot any issues that may arise.
How to shutdown Linux distros on WSL
If you use WSL, you can use a couple of commands to shut down one or all Linux distros manually, and here’s how.
Although the Windows Subsystem for Linux (WSL) is a convenient way to run Linux distros alongside Windows 11 or Windows 10, the lightweight virtual machine will continue to run in the background even when you exit the command shell, wasting system resources.
If you prefer to terminate the Linux distro (Ubuntu, Kali, Debian, Mint, etc.) as soon as you are done using it or have to restart it, use a wsl.exe command-line tool to shut down one or all distributions manually.
This guide will teach you the steps to shut down Linux distros running on the WSL2 platform on Windows 10 and 11.
Terminate a Linux distro on WSL
To terminate a Linux distro on WSL running on Windows 11 or Windows 10, use these steps:
In the command, replace DISTRO-NAME with the name of the distro you want to shut down, as shown in step 3. For example, wsl -t Ubuntu-20.04 .
(Optional) Type the following command to confirm the distro is “stopped” and press Enter:
Once you complete the steps, the distro running on the Windows Subsystem for Linux will gracefully shut down. You can always repeat the steps to terminate other distros or use the steps below to shut them all down.
Terminate all Linux distros on WSL
To shut down all the WSL distros running on Windows 10 (or 11), use these steps:
- Open Start.
- Search for Command Prompt (or PowerShell), right-click the top result, and select the Run as administrator option.
- Type the following command to view all running WSL distros and press Enter:
(Optional) Type the following command to confirm the distros are “stopped,” and press Enter:
After you complete the steps, all the WSL distros will terminate immediately on Windows 11 or 10.
If you need to restart the Linux distro, spin it again from the Start menu, or use Command Prompt or PowerShell and run the wsl —distribution DISTRO-NAME command (replace the DISTRO-NAME with the actual name of the distro before running the command).
We may earn commission for purchases using our links to help keep offering the free content. Privacy policy info.
All content on this site is provided with no warranties, express or implied. Use any information at your own risk. Always backup of your device and files before making any changes. Privacy policy info.
Since you are here.
I’ve got a small favor to ask. This is an independent site, and producing content takes a lot of hard work. Although more people are reading Pureinfotech, many use adblocker. Thus advertising revenue is falling fast. And unlike many other sites, there is no paywall blocking readers here. So you can see why your help is needed. If everyone who finds this website useful and helps to support it, the future would be much more secure. Thank you.
If you use adblocker, please disable it for this site.
$125 raised so far by 11 people.