Best way to manage your SSH environment in WSL
The best way I’ve found of managing my ssh environment in WSL is to skip SSH inside WSL altogether. Instead we defer everything to Windows’ own ssh system. The benefit of which is that we have an ssh-agent which persists across WSL instances, and is automatically started on boot.
This depends on two things that aren’t well known:
- Windows has its own SSH client & agent.
- WSL can execute Windows binaries.
Here’s a neat set of scripts which take care of the setup for you: https://github.com/arquivolta/wsl-use-windows-openssh
The end results is that all my ssh commands inside WSL are now links to the Windows versions.
~$ which ssh
/usr/lib/wsl-ssh/ssh
~$ ls -lah /usr/lib/wsl-ssh/
total 0
drwxr-xr-x 1 root root 4.0K Mar 2 11:14 .
drwxr-xr-x 1 root root 4.0K Apr 17 13:24 ..
lrwxrwxrwx 1 root root 39 Mar 2 11:14 scp -> /mnt/c/Windows/System32/OpenSSH/scp.exe
lrwxrwxrwx 1 root root 40 Mar 2 11:14 sftp -> /mnt/c/Windows/System32/OpenSSH/sftp.exe
lrwxrwxrwx 1 root root 39 Mar 2 11:14 ssh -> /mnt/c/Windows/System32/OpenSSH/ssh.exe
lrwxrwxrwx 1 root root 43 Mar 2 11:14 ssh-add -> /mnt/c/Windows/System32/OpenSSH/ssh-add.exe
lrwxrwxrwx 1 root root 45 Mar 2 11:14 ssh-agent -> /mnt/c/Windows/System32/OpenSSH/ssh-agent.exe
lrwxrwxrwx 1 root root 46 Mar 2 11:14 ssh-keygen -> /mnt/c/Windows/System32/OpenSSH/ssh-keygen.exe
lrwxrwxrwx 1 root root 47 Mar 2 11:14 ssh-keyscan -> /mnt/c/Windows/System32/OpenSSH/ssh-keyscan.exe
Note that due to some incompatibility with version 8.9p1 (the Ubuntu 22.04 LTS default), I had to manually upgrade Windows OpenSSH binaries. I took the binaries from here and placed them in C:\Windows\System32\OpenSSH
.