Automatically Starting Exactly One Ssh-Agent

Written by: Robert R. Russell on Friday, September 11, 2020.

I use SSH keys to protect all of my SSH logins. The following shell code starts only one ssh-agent and adds all ssh-keys to that agent. I recommend adding it to your ~/.bashrc file, so the proper environment variables are set up.

if [ `ps -C ssh-agent | wc -l` -ne 2 ]; then
  ssh-agent -a $HOME/.ssh/agent.sock > $HOME/.ssh/agent.env
  source $HOME/.ssh/agent.env
  for key in $HOME/.ssh/*.pub; do
    pkey=`basename -s .pub $key`
    ssh-add $HOME/.ssh/$pkey
  done
fi

©2020 Robert R. Russell — All rights reserved

Third Two Week Posting Challenge Review
Goodbye Jetpack