Introduction
WSL on Windows is great. After using it for some months, I have learned way more about how Windows and Linux systems interact.
One of the things that bothered me was that for some reason my SSH keys on the WSL system never works. On my previous machine running Ubuntu, the SSH Agent would automatically select the correct key to use in my ~/.ssh folder.
Discovery
I stumbled across this project https://github.com/rupor-github/wsl-ssh-agent, and it seemed promising, so I tried it out.
After downloading the binary, I extracted the contents to C:\tools\wsl-ssh-agent
. Then from Powershell I ran wsl-ssh-agent-gui.exe -socket c:\tools\wsl-ssh-agent\ssh-agent.sock
. From WSL terminal EXPORT SSH_AUTH_SOCK=/c/tools/wsl-ssh-agent/ssh-agent.sock
.
Now that the SSH_AGENT is setup, we need to register our ssh keys with the Windows ssh-agent. From Powershell, run ssh-add ${path to ssh private key}
.
Now when I run ssh from WSL, it will automatically run through the available ssh keys in my system to log into the remote machine. Win!
Persisting
Now we need a way to persist this setup across reboots.
First is to add wsl-ssh-agent-gui.exe
to start up on boot, next is to add the environment variable in WSL to use the correct ssh-agent.
To add to startup, navigate to C:\Users\[User Name]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
. From there, right click and create a new shortcut. Set Target to C:\tools\wsl-ssh-agent\wsl-ssh-agent-gui.exe -socket C:\tools\wsl-ssh-agent\ssh-agent.sock
and Start in to C:\tools\wsl-ssh-agent
.
In WSL, paste
in EXPORT SSH_AUTH_SOCK=/c/tools/wsl-ssh-agent/ssh-agent.sock
~/.profile