Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Created December 22, 2018 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/cfdc69ec0f3edd941dccdacbab45c4e2 to your computer and use it in GitHub Desktop.
Save RichardBronosky/cfdc69ec0f3edd941dccdacbab45c4e2 to your computer and use it in GitHub Desktop.
ssh-add keys in your bashrc
:<<'DOCS'
* This script ensures that your shell always has an agent with your preferred keys added.
On macos you do not need to start the agent explicitly. Just add the keys. If your key
requires a password, and it should, you will not be prompted for it again as long as
agent maintains your key. Simply putting ssh-add ~/.ssh/id_rsa in your .bash_profile
causes you to get prompted for your password for every shell and encourages people to
remove the password, which they shouldn't. This was created to solve that properly.
* Place this file at ~/.ssh/agentrc use it in ~/.bash_profile with:
[[ -f ~/.ssh/agentrc ]] && source ~/.ssh/agentrc
* You can copy-paste the next 5 lines to get and use this script:
curl -sLo ~/.ssh/agentrc https://gist.githubusercontent.com/RichardBronosky/cfdc69ec0f3edd941dccdacbab45c4e2/raw/agentrc
cat >> ~/.bash_profile <<'EOF'
[[ -f ~/.ssh/agentrc ]] && source ~/.ssh/agentrc
EOF
DOCS
for key in ~/.ssh/id_rsa # Add additional keys here
do
ssh-add -l | \
grep -q "$key" || \
ssh-add $key
done
# vim: ft=sh et sw=2 ts=2 sts=2
@RichardBronosky
Copy link
Author

Yes, I know I specify .bash_profile and not .bashrc as stated in the description. Modern macOS doesn't have a .bashrc by default, though I still google search for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment