Do you want to do remote development on your WSL2 container in Visual Studio Code? Read this.
- On the host set up OpenSSH for Windows
- Run
wsl --update
to make sure you are running the latest WSL - Open WSL and install another SSH server inside WSL with
sudo apt-get install openssh-server
- Now run
sudo systemctl enable --now ssh
to automatically start ssh when WSL starts. - On the remote machine run
ssh -J windows_user@windows_ip wsl_user@localhost
to see if the proxy jump will work
If you see the bash prompt it works. You can add it to the ~/.ssh/config file on the remote machine.
Host remote-wsl
HostName localhost
User wsl_user
ProxyJump windows_user@windows_ip
StrictHostKeyChecking no
Now connect with ssh remote-wsl
.
SSH to your Windows host (SSH Server must be installed in Windows Features)
ssh user@windowshost
Start Powershell
powershell
Run this command to switch SSH from CMD to WSL
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\WINDOWS\System32\bash.exe" -PropertyType String -Force
ssh user@windowshost
You should now see WSL2 instead of CMD
At this point you can connect to your Win10 host with Remote SSH in VSCode with your Windows username and pw. But you'll actually start and connect to WSL2.
- Install the Remote - SSH extension
- Add a new SSH target and connect with your Windows host, username and password (you will automatically log in as the Linux user)
The workaround was inspired by: https://www.hanselman.com/blog/the-easy-way-how-to-ssh-into-bash-and-wsl2-on-windows-10-from-an-external-machine
The ProxyJump method shared here is great, but it is missing the ability to start WSL if it is not already running. If we use
ProxyCommand
instead ofProxyJump
, we can ensure that WSL always starts when we connect. Building on ajihyf's solution:In your ssh config (e.g.,
%USERPROFILE%\.ssh\config
on Windows), add:ProxyCommand
does the following:windows_user@windows_ip
)wsl.exe --distribution wsl_distribution
)--exec bash -c
)netcat %h %p
)%h
becomes the value ofHostName
and%p
becomes the value ofPort
.This is also works if you use a tool like 1Password to manage your SSH and GPG keys. Expand for an example.
In this is example, I use ForwardAgent to forward SSH requests to the 1Password SSH Agent. I have create an SSH key for my GIS Workstation computer and added the public key to my authorized keys on Windows and Ubuntu 24.04 (in WSL). I have also saved the public key to
~/.ssh/gis_workstation_jack.pub
so that SSH does not try to use every SSH key I have in 1password, which may result in a "too many authentication failures" error.I find this helpful because it allows me to connect to my WSL instance on my desktop PC from my laptop without needing to remotely log in to my desktop PC first and manually start WSL.