Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hemenkapadia/bd30e511624f3a22dde1 to your computer and use it in GitHub Desktop.
Save hemenkapadia/bd30e511624f3a22dde1 to your computer and use it in GitHub Desktop.
[SSH Agent Configuration] Configuring SSH Agent to automatically add ssh keys for password less login #ssh #tips #git
# add the below scriptlet to your ~/.bashrc
# File to store the SSH Agent environment
SSH_ENV=$HOME/.ssh/environment
# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo "Agent started...."
# Source environment, which sets SSH_AGENT_PID in the current environment
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
# Adding keys to ssh agent
find ~/.ssh -type f -name *id_rsa -exec /usr/bin/ssh-add {} \;
}
# Do not start SSH Agent if SSH agent is still running as the same PID
# as the one stored in the environment
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment