2 minute read

I’ve been wrestling with SSH key management in WSL. I had my SSH keys set up in Windows with the SSH agent running, but WSL insisted on using its own SSH client that doesn’t talk to the Windows agent. I’m surprised it isn’t baked into WSL at this point. I found this blog post that shares a solution, documenting my approach here.

The Solution

Step 1: Alias SSH to use Windows

Add this to your .bashrc or .zshrc:

alias ssh='ssh.exe'

Step 2: Tell Git to use Windows SSH too

Next, we tell git which SSH client to use:

git config --global core.sshCommand "ssh.exe"

That’s it!

Why This Works

You can run windows commands from within WSL, so if we run ssh.exe instead of ssh, it runs the Windows SSH client instead of the Linux SSH client, and the Windows client knows how to talk to the agent.

The Git configuration ensures that when Git needs SSH (for cloning, pushing, etc.), it uses the same Windows SSH that can talk to your Windows SSH agent.

Testing It

You can verify everything’s working:

# Should show Windows SSH version
ssh -V

# Should return "ssh.exe"
git config --get core.sshCommand

# This should work without prompting for passwords
ssh -T [email protected]

Another solution is creating a symlink:

mkdir -p ~/bin
ln -s $(which ssh.exe) ~/bin/ssh
export PATH="$HOME/bin:$PATH"  # Add this to your shell profile

The alias + Git config approach is simpler and works just as well.

If you’re using git on Windows, you might run into a related problem where git uses its own ssh client. For git to find your keys in Windows, you need to tell git to use an external binary rather than its own. This powershell command should do it, finding the location for ssh.exe and telling git to use it:

git config --global core.sshCommand "'$($( Get-Command ssh ).Path.replace("\", "/"))'"

Credit to stackoverflow.

Leave a comment

Your email address will not be published. Required fields are marked *

Loading...