Skip to content

Instantly share code, notes, and snippets.

@macouella
Last active February 12, 2018 01:33
Show Gist options
  • Save macouella/d61521f2e476d63bd66d3b2195323f94 to your computer and use it in GitHub Desktop.
Save macouella/d61521f2e476d63bd66d3b2195323f94 to your computer and use it in GitHub Desktop.
Persist ssh-add across reboots | Mac OSX

Add to ~/.ssh/config

Host *
  UseKeychain yes
  AddKeysToAgent yes

Add ssh key to keychain

Using /usr/bin/ssh ensures that you use the system ssh-add and not the one installed by brew (if ever)

/usr/bin/ssh-add -K ~/path_to_your_private_key

Create a new plist that launches on startup and executes ssh-add -A

~/Library/LaunchAgents/sshadd.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>ssh-add-a</string>
    <key>ProgramArguments</key>
    <array>
        <string>ssh-add</string>
        <string>-A</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Testing if the ssh-key has successfully been added to the keychain

# (l)ist existing ssh keys
ssh-add -l

# (a)dd existing keychain ssh keys
ssh-add -A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment