A bash script for managing ssh-agent
that I use for Ubuntu and OSX.
Started as a variation from this classic on Stack Overflow How to check if ssh-agent is already running and then adapted it to align with behaivor I want.
This version does the following:
- checks for ssh agent forward (remote login) and if so does not setup ssh-agent or any keys
- checks if there is an existing agent running and if so uses it
- otherwise launches a new agent
- then, if it is an interactive shell, loads keys:
- priority is to load only
$HOME/.ssh/id_rsa-${HOSTNAME}
key - if that isn't found it loads all public keys:
.ssh/*.pub
- priority is to load only
I put the script into ~/.bash.d/ssh-agent.sh
and launch the script from my .bashrc
using:
if [ -d $HOME/.bash.d ]; then
for I in $HOME/.bash.d/*.sh; do
source $I
done
fi
or you can put script as $HOME/.ssh-agent.sh do the one liner in .bashrc
curl -o ssh-agent.sh https://gist.githubusercontent.com/dayne/a97a258b487ed4d5e9777b61917f0a72/raw/ssh-agent.sh
test -f $HOME/.ssh-agent.sh && source ${_}
mkdir .bash.d
cd .bash.d
curl -o ssh-agent.sh https://gist.githubusercontent.com/dayne/a97a258b487ed4d5e9777b61917f0a72/raw/ssh-agent.sh
cd ..
nano .bashrc
Scroll to bottom of .bashrc (mouse down) and insert (paste) the above example.
Jan 17th, 2020
Updated to include debug messages to help debugging weird situations. Turns out you can get a
SSH_AUTH_SOCK
that isn't connected to a valid ssh-agent. Added a test for that scenario along with ability to toggle on debug messages to help debug why a new valid agent isn't being kicked off.