Skip to content

Instantly share code, notes, and snippets.

@decentraliser
Last active August 12, 2021 23:04
Show Gist options
  • Save decentraliser/c77b9750f61f1f3d5a5d7777daf79502 to your computer and use it in GitHub Desktop.
Save decentraliser/c77b9750f61f1f3d5a5d7777daf79502 to your computer and use it in GitHub Desktop.
One-time setup for git SSH credentials
# Start ssh-agent at each session if necessary
# Source: https://askubuntu.com/questions/878944/cant-store-passphrase-in-same-session-with-eval-ssh-agent-ssh-add
sudo vim ~/.bashrc
#
# setup ssh-agent
#
#start running ssh-agent if it is not already.
if [ ! 'root' = "${USER}" ]; then
if ! pgrep ssh-agent &> /dev/null && ! uname -rms | grep Darwin &> /dev/null; then
eval "$(ssh-agent -t 3600)" > /dev/null
fi
if ! uname -rms | grep Darwin &> /dev/null; then
if [ -z "${SSH_AUTH_SOCK}" -o -z "${SSH_AGENT_PID}" ]; then
#first time failed so try again.
SSH_AUTH_SOCK="$(ls -l /tmp/ssh-*/agent.* 2> /dev/null | grep "${USER}" | awk '{print $9}' | tail -n1)"
SSH_AGENT_PID="$(echo ${SSH_AUTH_SOCK} | cut -d. -f2)"
fi
if [ -z "${SSH_AUTH_SOCK}" -o -z "${SSH_AGENT_PID}" ]; then
SSH_AUTH_SOCK="$(lsof -p "$(pgrep ssh-agent | tr '\n' ',')" | grep "${USER}" | grep -e "ssh-[^/]*/agent\.[0-9]\+$" | tr ' ' '\n' | tail -n1)"
SSH_AGENT_PID="$(echo ${SSH_AUTH_SOCK} | cut -d. -f2)"
fi
fi
[ -n "${SSH_AUTH_SOCK}" ] && export SSH_AUTH_SOCK
[ -n "${SSH_AGENT_PID}" ] && export SSH_AGENT_PID
fi
# Add keys to SSH config
sudo vim ~/.ssh/config
Host ssh.dev.azure.com
User git
IdentityFile ~/.ssh/private_key
sudo chmod 600 ~/.ssh/config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment