Inspired by Collin Anderson's answer, I wrote an alternative script (that needs to be put inside of your .bashrc file) which unlike his, actually works in environmentsan environment like Git Bash and WSL (where the /tmp directory doesn't get cleared, which is what his solution relies on):
source ~/.ssh/agent_out &> /dev/null
if ! ps -p $SSH_AGENT_PID &> /dev/null
then
ssh-agent > ~/.ssh/agent_out
source ~/ssh/agent_out &> /dev/null
fi
To understand what this script is actually doing, let’s consider what it would do the first time it’s run:
- It spins up an SSH agent, stores the output of the
ssh-agentcommand inside a file calledagent_outin the user’s.sshdirectory for later use. The output of thessh-agentcommand contains statements to assign the right values to environment variables likeSSH_AUTH_SOCKandSSH_AGENT_PID. - For every new shell session, it first executes the output of the last
ssh-agentcommand which was stored in the~/.ssh/agent_outfile, and then checks whether the process with the ID included inSSH_AGENT_PIDactually exists or not. If it doesn’t, it performs the first step.
This, coupled with the new SSH config option AddKeysToAgent — see the manual —, would yield a nice user experience, and eliminate the need for third-party tools like keychain and ssh-ident for the most part:
~/.ssh/config:
AddKeysToAgent yes