Notes

Add SSH Key to MacOS Keychain permanently

Edit on GitHub

Linux
2 minutes

Run ssh-agent if it’s not already running

1eval "$(ssh-agent -s)"

Add SSH key to ssh-agent

1ssh-add -K ~/.ssh/id_rsa

where id_rsa is the actual filename of the key you’re adding. This should do it for OSX before Sierra.

MacOS Sierra

Create an SSH config file

1nano ~/.ssh/config

Add the following to it

1Host *
2UseKeychain yes
3AddKeysToAgent yes
4IdentityFile ~/.ssh/id_rsa
  • UseKeychain yes tells SSH to look in your OSX keychain for the key passphrase.
  • IdentityFile specifies the key you want to load. If you want to load multiple keys just add more IdentityFile entries, one per line.

Now add your key(s)

1ssh-add -K ~/.ssh/id_rsa

For multiple keys, the config file will look like this:

1Host *
2UseKeychain yes
3AddKeysToAgent yes
4IdentityFile ~/.ssh/id_rsa
5IdentityFile ~/.ssh/foo
6IdentityFile ~/.ssh/bar

More about the ~/.ssh/config file

The SSH config file lets you set defaults for all servers (host) as well as save settings like which key/port/user to use for specific ssh connections. Usually, i use Shuttle for one-click connections, and it picks up and lists all hosts from the ssh config file as well.

If you’re using the config file to add shortcuts for different servers, your config file may look like this

 1Host server1
 2     HostName server1.cyberciti.biz
 3     User nixcraft
 4     Port 4242
 5     IdentityFile /nfs/shared/users/nixcraft/keys/server1/id_rsa
 6
 7Host nas01
 8     HostName 192.168.1.100
 9     User root
10     IdentityFile ~/.ssh/nas01.key

Once you have servers saved in your config file, you can connect to them by specifying their names, like so

1ssh server1
NOTE: You can not have `@` in the `Host` value. For example, i tried configuring `Host user@server` because i had multiple users, and it didn't work. Will give a `ssh: Could not resolve hostname titan: nodename nor servname provided, or not known` error.

Related