Terminal

Bash scripts, command line tips, etc.

Add a private key to macOS keychain

Add private key to keychain

Adding a private key to the Mac OS X keychain will allow you to access servers protected by a private key from various applications (Terminal, Versions, etc.). Adding is fairly easy:

chmod 600 ~/.ssh/id_rsa
ssh-add --apple-use-keychain ~/.ssh/id_rsa

You might need to update ~/.ssh/id_rsa with whatever your key path is.

Prevent macOS from prompting after restart

Starting on macOS Sierra Terminal no longer accesses the keychain by default. You need to change the configuration by editing ~/.ssh/config:

nano ~/.ssh/config

Then add this to the file:

Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsa

The first time you still need to add your private key using the method above; macOS will then remember it for future runs.

Again, you might need to update ~/.ssh/id_rsa with whatever your key path is.