Skip to content

Instantly share code, notes, and snippets.

@DamianPala
Last active February 8, 2026 17:05
Show Gist options
  • Select an option

  • Save DamianPala/f2a6e6a65cf746e604242696b1369646 to your computer and use it in GitHub Desktop.

Select an option

Save DamianPala/f2a6e6a65cf746e604242696b1369646 to your computer and use it in GitHub Desktop.
WSL as a service with SSH access

WSL as a service with SSH access

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.

Features

  1. WSL started as a service
  2. No user login needed
  3. No WSL terminal visible after login
  4. SSH access
  5. Static WSL IP address

WSL installation

Open an elevated PowerShell or Windows Terminal (run as administrator) and type

wsl --install Ubuntu
shutdown -r -f /t 0

SSH Server

Enter 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/null

Do not close WSL terminal.

Configure SSH port forwarding

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_port

Now 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.

Install Chocolatey and NSSM

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 -v

Install NSSM

choco install nssm -y

Create WSL service

nssm install wsl

Enter 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 wsl

Finally reboot your machine

shutdown -r -f /t 0

and try to log in via SSH to WSL. Use

ping 8.8.8.8

and wait at about 60s to be sure that WSL is running properly without interruption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment