SSH Agent for WSL and Ubuntu

Related: Disable Gnome Keyring SSH Agent


SSH Agent remembers SSH Public Key authentication for a period of time. While native Windows has SSH built in, there is no straightforward way to have an SSH agent in Windows itself. Instead, one can use WSL for SSH agent as follows.

SSH agent setup

This works for Linux in general, including Windows Subsystem for Linux.

Add to ~/.bashrc:

if [ -z "$(pgrep ssh-agent)" ]; then
   rm -rf /tmp/ssh-*
   eval $(ssh-agent -s) > /dev/null
else
   export SSH_AGENT_PID=$(pgrep ssh-agent)
   export SSH_AUTH_SOCK=$(find /tmp/ssh-* -name agent.*)
fi

Open a new Terminal and type:

ssh-add -t 30m ~/.ssh/mygithubkey
-t 30m
remember authentication for a period of time (here, 30 minutes)

When done SSHing, you can optionally remove all SSH agent keys from RAM by

ssh-add -D

Tips

Add multiple SSH keys in one command by commands like:

ssh-add ~/.ssh/{mygithub,mybitbucket}

Notes

reference