SSH-agent on Linux, WSL and Windows

SSH-agent remembers SSH Public Key authentication, which can be time-limited by the user. This avoids the user having to type the password for each SSH connection, especially relevant to using Git over SSH. Native Windows has SSH including SSH-agent, and separately WSL also can use SSH-agent. SSH-agent works well with Git over SSH.

Add SSH keys to SSH-agent

To use SSH-agent, add SSH keys like:

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

One can optionally remove all SSH-agent keys from RAM by

ssh-add -D

Note that if the SSH private key was manually deleted, access to the remote SSH server is lost until a new private key is placed on the remote server when an SSH key is removed from SSH-agent.

Enable SSH-agent

Each operating system has a distinct method of enabling SSH-agent, as follows.

Windows

Windows SSH-agent is off by default, but can be enabled from PowerShell:

Start-Service ssh-agent

The status of Windows SSH-agent can be checked from PowerShell:

Get-Service ssh-agent

if status is “Running” then SSH-agent should be working.

Linux / WSL

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

Notes