In this tutorial, we will configure a Windows Linux subsystem that will automatically start when the system boots without requiring a user to log in. WSL is started like a daemon, without opening a terminal. It will be easily accessible via SSH.
- WSL started as a service
- No user login needed
- No WSL terminal visible after login
- SSH access
- Static WSL IP address
Open an elevated PowerShell or Windows Terminal (run as administrator) and type
wsl --install Ubuntu
shutdown -r -f /t 0Enter WSL by typing wsl in the terminal and use following commands to install and enable SSH Server.
sudo apt update && sudo apt upgrade -y
sudo apt install -y openssh-server
sudo systemctl enable --now ssh
echo -e "[boot]\nsystemd=true" | sudo tee /etc/wsl.conf > /dev/nullDo not close WSL terminal.
On Windows host run
$host_port = <host_port>
$wsl_ip = (wsl hostname -I).Trim()
netsh interface portproxy add v4tov4 listenport=$host_port connectport=22 connectaddress=$wsl_ip
netsh advfirewall firewall add rule name="WSL SSH" dir=in action=allow protocol=TCP localport=$host_portNow check if SSH connection to already running WSL instance is working. If yes, go to the next step. Setting port forwarding causes WSL to reuse the previous IP address, so it becomes static.
Run the following command to install Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))Restart terminal and verify installation:
choco -vInstall NSSM
choco install nssm -ynssm install wslEnter an absolute path to WSL from Program Files directory into the Path field.
C:\Program Files\WSL\wsl.exe
As Startup Directory use parent directory of the wsl.exe
C:\Program Files\WSL
By default WSL is turned off after a timeout about 10s after WSL terminal is closed or inactivity.
If you want WSL always run in the background you need to add the following arguments into Arguments field.
sleep infinity
Go to Log On tab, select this account and enter your username and password. Your account can be administrator type. It must have direct access to the WSL.
Next start wsl service
nssm start wslFinally reboot your machine
shutdown -r -f /t 0
and try to log in via SSH to WSL. Use
ping 8.8.8.8and wait at about 60s to be sure that WSL is running properly without interruption.