This small BASH shell script will help you share the same SSH Agent instance between multiple logins, assuming that you will keep the primary login active.
SSH-Agent is a program to hold authentication private keys. SSH-Agent is started in the beginning of a new session, and all other logins or programs are started as children of the ssh-agent program. Programs started under the agent inherit a connection to the agent, and the agent is automatically used for RSA authentication when logging on to other machines using SSH.
1. Prerequisites
If you did not have a public/private key pair yet, please create it by doing:
ssh-keygen -t rsa
2. Installation
Add the following code segment into your ~/.profile or ~/.bashrc file.
agent ()
{
eval `ssh-agent -s`
ssh-add
echo ${SSH_AGENT_PID} ${SSH_AUTH_SOCK} > ~/.ssh-agent-info
trap "ssh-agent -k ; rm -f ~/.ssh-agent-info" 0
}
if [ -e ~/.ssh-agent-info ]; then
read SSH_AGENT_PID SSH_AUTH_SOCK < ~/.ssh-agent-info
if [ ! -d /proc/${SSH_AGENT_PID} ]; then
rm -f ~/.ssh-agent-pid ~/.ssh-agent-sock
else
declare -x SSH_AGENT_PID SSH_AUTH_SOCK
fi
fi
3. Usage
Re-login or do a source to reload your .profile or .bashrc. Type agent on shell prompt to start your ssh-agent. It will ask you for your private key password if you defined one when you created your key.
As long as you keep this login active, all of your future logins will share the same ssh-agent instance.