You are currently viewing MacOS: How to permanently add ssh private key to keychain

MacOS: How to permanently add ssh private key to keychain

If your private ssh key is secured by passphrase you have to run ssh-add command to add it to keychain. It is a mechanism that allows OSX to load ssh keys on login without passing passphrase each time you want to use the key. You can skip the following step if you key is not secured by passphrase.

Step 1: Add private key to keychain

ssh-add --apple-use-keychain ~/.ssh/<private-key-name>
ssh-add -K ~/.ssh/<private-key-name>

Step 2: Configure ssh to use keychain

The next step is to configure ssh to use keychain for specific hosts or for any hosts depends on your needs. You can do this by editing the ~/.ssh/config file. If the file does not exists you can simply create it. Add the following configuration to the file, or modify existed one.

 Host *
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/<private-key-name>
 Host better-coding.com
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/<private-key-name>

That’s all what I’ve prepared for you in this tutorial, if I helped you, please consider sharing this post to help me gain a wider audience.
Thanks and I hope to see you in my next tutorial.