How does one get the SSH used by VSCode’s Remote-SSH plugin to use the same configuration and keys as that in WSL2? This was a problem I solved some years ago. But it seems nowadays there’s a better way to do it.
Alright, so it is not so much that my old solution wasn’t optimal at that time. it is just that there’ve been new developments, and also new things about Windows that I didn’t know about before.
Just to elaborate a bit on the problem that I was trying to address. I use WSL2 a lot. I run the native Ubuntu’s OpenSSH from inside WSL2 for all the terminal-based shell activities that I do. Then, when I use VSCode’s Remote-SSH plugin to get into my remote host, by default, the SSH that gets called is the OpenSSH from Windows. The two SSH programs use configuration files in different places. The WSL2 one would look in the ~/.ssh directory within the WSL2 instance, whereas the Windows one would look in $HOMEPATH\.ssh directory in the native Windows filesystem. That means, ordinarily, I would end up with two different configurations to manage.
You can read about my old solution. It solves the problem by getting VSCode to call the SSH in WSL2. This also addresses another requirement I had. I needed to use SSH’s ProxyJump feature, which was not available in the native Windows version of OpenSSH at that time. This was a showstopper for me; I did not want to muck around with ProxyCommand.
Things are much simpler now. The native OpenSSH in Windows supports ProxyJump. The issue of trying to keep the two versions of OpenSSH in sync? Well, one simple solution is to symlink the WSL’s ~/.ssh directory to the native $HOMEPATH\.ssh in Windows.
Just a little further point. It was slightly more complicated in the past also partly because I wanted my SSH stuff to be synchronised across computers. I do that by symlinking WSL’s ~.ssh to a subdirectory under a directory tree that is kept in sync by Nextcloud. (It could also work with Google Drive or OneDrive, etc, of course.)
Is there such a thing as symlinking in Windows? It turns out there is! In the CMD shell, there is a MKLINK.EXE command, which does something similar to Linux’s ln command. Oddly enough, MKLINK.EXE requires Administrator privileges to run. Using MKLINK.EXE, I can do something like:
C:\Users\lzs> MKLINK /D .ssh \Users\lzs\NextCloud\ComputerSync\ssh
The /D is required when creating a directory symlink, and the link is specified first instead of the target, unlike the Linux version of ln.
This simpler solution works beautifully to keep the SSH in both environments in sync, as well as allows me to easily keep my SSH stuff in sync between multiple computers by using Nextcloud.
There’s also another reason why I needed VSCode to go back to the Windows native OpenSSH. The problem with the Remote-SSH plugin calling WSL2’s OpenSSH is that if a password is requested, even though VSCode can present a dialog for password entry, the input doesn’t get to OpenSSH. Interactive password authentication works properly if the Windows native OpenSSH is used.
So this is the updated, perhaps also simpler, way to get SSH in VSCode in sync with the WSL2 environment.
Hi,
Thanks for your sharing how to sync the ssh.config between wsl and windows. However, I find using the same ssh.config file may cause a problem when you use some external programs in your ssh command. For example, if you use “nc” (netcat) for proxyjump, “nc” is used in wsl while “ncat.exe” is used in windows.
Appreciate your time and sharing this. Feel free to share your thought. Thanks!
Hi. SSH can directly do ProxyJump without having to specify nc/ncat.exe anymore… so at least for this particular use case there shouldn’t be any problems? But sure, if there are other things you had to do via ProxyCommand, that would be an issue still.
Hi there, i followed the original but im afraid i lost you here with the MKLINK. The first part is the link…. ?
C:\Users\lzs> MKLINK /D \Users\lzs\NextCloud\ComputerSync\ssh .ssh
so c:\Users\lzs ..would in actual scenario be c:\Users\qx1234\.ssh *** – > the windows .ssh folder, then MKLINK *** but what follows ? \Users\lzs\NextCloud…. is this where my WSL ssh should go.?
for example my WSL is mapped to Z:, so for example is Z:\home\gcan123\.ssh, which resembles my WSL structure ssh folder. and lastly what is .ssh at the end?