Prevent WSL from Auto Closing

By default, WSL2 automatically terminates its virtual machine when all terminal sessions are closed or after being idle. To keep WSL running continuously in the background (for daemons, background services, Docker, or SSH), you can use the following methods.

In Windows, create or edit %USERPROFILE%\.wslconfig (usually C:\Users\<YourUsername>\.wslconfig):

1
2
[wsl2]
vmIdleTimeout=-1
  • vmIdleTimeout=-1 sets the idle timeout to infinity, preventing WSL2 from shutting down automatically after closing terminal windows.
  • To apply the changes, restart WSL in PowerShell/CMD:
1
wsl --shutdown

Helpful WSL Management Commands

  • Check running status of WSL distributions:
1
wsl --list --verbose
  • Manually shut down WSL:
1
wsl --shutdown