Thoughts and Ramblings

General things I find of interest.

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.

  1. Launch the WSL instance and inside it execute:
    sudo apt install openssh-server
    
  2. Run ip addr show to get the IP address of this VM to be used later
  3. Launch your favorite shell on the windows side and run:
    netsh 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
    
    and substitute the IP address you obtained earlier for the connectaddress in the second line.
  4. Execute netsh interface portproxy show v4tov4 to confirm it is set correctly (and you can run netsh 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:

  1. Run Task Scheduler and create a new task
  2. Set it to Run whether the user is logged in or not and Do not store password
  3. Under Triggers add to run At startup
  4. Under Actions, add Start a Program with the program C:\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.