Skip to content

Instantly share code, notes, and snippets.

@jdmallen
Last active July 6, 2021 22:51
Show Gist options
  • Select an option

  • Save jdmallen/a263bc8fc901082e39344f5f8e11a21a to your computer and use it in GitHub Desktop.

Select an option

Save jdmallen/a263bc8fc901082e39344f5f8e11a21a to your computer and use it in GitHub Desktop.
A .bashrc script that automatically spawns ssh-agent and adds your identities upon first load
# This .bashrc snippet is primarily geared toward those who use Git Bash for
# Windows. Save this file in the root of your user folder; e.g., C:\Users\jesse,
# or /c/Users/jesse in Git Bash parlance. This app checks to see if ssh-agent
# is running, and if it isn't, starts it and adds any private keys you desire.
# It will only do this once per Windows session, or until ssh-agent is killed.
# Now you don't have to keep loading your key or typing in your git remote
# password repeatedly. I have no reason to believe this would not work equally
# well in a Linux environment. Save it as (or append to) ~/.bashrc
SSH_ENV=~/.ssh/environment
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo " success!"
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add ~/.ssh/id_rsa # or wherever your private keys are
# Add as many private keys as you like, but be warned: ssh will try to
# establish a connections with each loaded identity, even the wrong ones.
# After so many fail, it will quit, even if you have the correct identity
# loaded, several private keys down the line.
# If that happens, simply make your ssh connections with the -i flag. E.g.,
# `ssh -i ~/path/to/id_rsa user@10.10.1.220`
}
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
# This will ensure that bash always starts in your project directory, saving
# you a couple "cd"s each time you start it. Change the path below as needed.
proj() {
cd ~/git
}
proj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment