In setting up SSH into an Linux install in WSL2, I’ve found guides on how to do this setup. While most contain the similar steps for setting up SSH, they have very different mechanisms for making it start on boot, of which almost none work.
Setting up SSH
First I’ll assume you are running WSL2 and have installed a debian based linux within it.
- Launch the WSL instance and inside it execute:
sudo apt install openssh-server
- Run
ip addr show
to get the IP address of this VM to be used later - Launch your favorite shell on the windows side and run:
and substitute the IP address you obtained earlier for thenetsh advfirewall firewall add rule name=”Open Port 2222 for WSL2” dir=in action=allow protocol=TCP localport=2222 netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=2222 connectaddress=172.23.129.80 connectport=22
connectaddress
in the second line. - Execute
netsh interface portproxy show v4tov4
to confirm it is set correctly (and you can runnetsh int portproxy reset all
to remove the entries if you need).
(A step or two may be missing above but this is the gist and I’m not trying to make a comprehensive guide for this part)
Launching WSL2 on Boot
Here is where the other suggestions fall flat. Most suggest using the wsl command-line to start SSH which is not needed as current default installs Linux in a VM with systemd launching the SSH server. Others suggest running the wsl command-line to execute a single command in the Linux VM. And finally one or two make suggestions referencing system settings that don’t exist leading me to think these are AI hallucinations.
The problem with using the wsl command-line to run a command inside the Linux VM is that it spins up the VM, runs the command, and when it is complete, it shuts down the VM. So considering this technique means it would require a command that doesn’t exit. And thus inspired my solution:
- Run
Task Scheduler
and create a new task - Set it to
Run whether the user is logged in or not
andDo not store password
- Under
Triggers
add to runAt startup
- Under
Actions
, addStart a Program
with the programC:\Windows\System32\wsl.exe
and the arguments-u root sleep infinity
This command will continue to run in WSL indefinately, will practically no CPU, and thus keep the WSL instance running.
Hope this is helpful.