Skip to content

Instantly share code, notes, and snippets.

@gitjonez
Last active November 24, 2022 19:24
Show Gist options
  • Save gitjonez/fa031c87efbd36d5140c0abfe9c53540 to your computer and use it in GitHub Desktop.
Save gitjonez/fa031c87efbd36d5140c0abfe9c53540 to your computer and use it in GitHub Desktop.
ssh-agent on macos ~/.zshrc

What to put in your ~/.zshrc

Brainless ssh-agent code for ZSH (or BASH) on macos

Quickstart

Probably near the top, after the point where the macos ssh-agent -l is running via launchd, but could be anywhere:

~/.zshrc

if ! ssh-add -l > /dev/null; then
    ssh-add
fi

This ^ will call ssh-add and prompt you for your passphrase if, ssh-add -l returns nonzero status (it prints The agent has no identities.)

If ssh-add is storing a key, nothing happens so you WON'T be prompted for your ~/.ssh/id_rsa (ish) passphrase.

Assumptions

  • launchd starts ssh-agent magically, by default (does it?):
❯ ps aux | grep -E 'ssh-agent|PID' | grep -v grep
USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
leme               755   0.0  0.0 33626532   1400   ??  S    10:59PM   0:00.04 /usr/bin/ssh-agent -l

Notes

  • I removed all the other junk dealing with ssh-agent in my [.zshrc | .bash_profile], rebooted and ran the above ps command to check the baseline
  • It's also likely that Gnome or X11/XDM might startup an agent on login, in which case, this is also all you may need
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment