Managing mulltiple ssh-key pairs can be made easy by utilizing the power of ssh-agent. However, I'd like to have my ssh-sessions automatically attach to the ssh-agent or start a instance of ssh-agent if it's not already started.
Starting the ssh-agent and attaching to it can be achieved by using these commands:
[archy@server ~]$ ssh-agent -s > ${HOME}/.ssh/environment-$(hostname -s)
[archy@server ~]$ source ${HOME}/.ssh/environment-$(hostname -s)
This will create a file named 'environment-server' in the folder ~/.ssh with all information required to attach it and then source it to attach to the running ssh-agent.
Reconnecting to the running ssh-agent can be done using this command again:
[archy@server ~]$ source ${HOME}/.ssh/environment-$(hostname -s)
Another thing to consider is not starting multiple ssh-agents, so we'll have to check there is a instance of ssh-agent running for the current user and then determine if we should attach to the currently running instance or stop it and restart it by using the method mentioned above, here's a code-snippet that can help with that:
AGENTPID=$(pgrep -u ${USER} -x ssh-agent)
PGREP_RC=$?
if [ ${PGREP_RC} -eq 0 ]; then
if [ -f ${HOME}/.ssh/environment-$(hostname -s) ]; then
echo -e "attaching to ssh-agent for user ${USER}"
source ${HOME}/.ssh/environment-$(hostname -s)
else
echo -e "killing stale agent for user ${USER}"
kill -s TERM ${AGENTPID}
ssh-agent -s > ${HOME}/.ssh/environment-$(hostname -s)
source ${HOME}/.ssh/environment-$(hostname -s)
fi
fi
Now, let's throw it all together:
AGENTPID=$(pgrep -u ${USER} -x ssh-agent)
PGREP_RC=$?
if [ ${PGREP_RC} -eq 0 ]; then
if [ -f ${HOME}/.ssh/environment-$(hostname -s) ]; then
echo -e "attaching to ssh-agent for user ${USER}"
source ${HOME}/.ssh/environment-$(hostname -s)
else
echo -e "killing stale agent for user ${USER}"
kill -s TERM ${AGENTPID}
ssh-agent -s > ${HOME}/.ssh/environment-$(hostname -s)
source ${HOME}/.ssh/environment-$(hostname -s)
fi
else
echo -e "starting ssh-agent for user ${USER}"
ssh-agent -s > ${HOME}/.ssh/environment-$(hostname -s)
source ${HOME}/.ssh/environment-$(hostname -s)
export SSH_AGENT
fi
And now to make it automatic, add it to your shellrc so that it will be executed on every login. In my case, I want this to apply serverwide, so I'll add it to /etc/zshrc. I'll also add a check to ensure that ~/.ssh exists:
[archy@server ~]$ sudo vim /etc/zshrc
# start - attach or start ssh-agent
if [ ! -d ${HOME}/.ssh ]; then
mkdir -p -m 750 ${HOME}/.ssh
fi
AGENTPID=$(pgrep -u ${USER} -x ssh-agent)
PGREP_RC=$?
if [ ${PGREP_RC} -eq 0 ]; then
if [ -f ${HOME}/.ssh/environment-$(hostname -s) ]; then
echo -e "attaching to ssh-agent for user ${USER}"
source ${HOME}/.ssh/environment-$(hostname -s)
else
echo -e "killing stale agent for user ${USER}"
kill -s TERM ${AGENTPID}
ssh-agent -s > ${HOME}/.ssh/environment-$(hostname -s)
source ${HOME}/.ssh/environment-$(hostname -s)
fi
else
echo -e "starting ssh-agent for user ${USER}"
ssh-agent -s > ${HOME}/.ssh/environment-$(hostname -s)
source ${HOME}/.ssh/environment-$(hostname -s)
export SSH_AGENT
fi
# stop - attach or start ssh-agent
On login, you'll then see a message like this:
attaching to ssh-agent for user archy
Agent pid 123456
I am well aware that there is a package manager for zsh (oh-my-zsh) which offers packages that can do this for you but I'm purposefully trying to stay away from oh-my-zsh because it is overkill for my use-case.
Feel free to comment and / or suggest a topic.
Comments
Post a Comment