Personal Programming Notes

To err is human; to debug, divine.

SSH Cookbook: Ssh-add

Recipes with ssh-add command.

Simple usages

Adding identity file
1
ssh-add /path/to/file/id_rsa.pem
Listing identities
1
2
3
4
5
6
# Display the keys' fingerprints only
ssh-add -l
# Display same in MD5 format
ssh-add -l -E md5
# Display the entire public key
ssh-add -L
Deleting identity
1
2
3
4
# Delete a key
ssh-add -d /path/to/file
# Clear all keys
ssh-add -D

OSX specific

On OS X ssh-add is integrated with the system keychain. If you give the -K option, as in ssh-add -K, when you add a key, that key’s password will be added to the keychain. As long as your keychain is unlocked, a key that has been stored in this way doesn’t require a password to be loaded into the agent.

All keys with their password stored in the keychain will automatically be loaded when you run ssh -A. This happens automatically on login.

When a password has been stored in keychain, ssh -K -d key-file both removes the key from the agent and removes it password from the keychain. Without -K, -d does not change the keychain and the key can be reloaded without a password. -D silently ignores -K.

Recipe: Connecting without a passphrase

ssh-add is commonly used to simplify ssh command. In the following example, you need to specify a private key file in some location.

Before
1
2
3
4
5
6
mymac:~ tdongsi$ ssh -i ~/.ssh/private.key centos@k8s-worker-10
Enter passphrase for key '/Users/tdongsi/.ssh/private.key':
Last login: Mon May 15 20:17:13 2017 from 10.3.52.223
[centos@k8s-worker-10 ~]$ exit
logout
Connection to k8s-worker-10 closed.

By adding the private key to the authentication agent with ssh-add, you can simplify the ssh command as follows:

After
1
2
3
4
5
6
7
8
9
mymac:~ tdongsi$ ssh-add ~/.ssh/private.key
Enter passphrase for /Users/tdongsi/.ssh/private.key:
Identity added: /Users/tdongsi/.ssh/private.key (/Users/tdongsi/.ssh/private.key)
mymac:~ tdongsi$ ssh-add -l
2048 SHA256:WKysqi9jq735mRK0U2MNS5A /Users/tdongsi/.ssh/private.key (RSA)

mymac:~ tdongsi$ ssh centos@k8s-worker-10
Last login: Mon May 15 20:23:46 2017 from 10.10.74.67
[centos@k8s-worker-10 ~]$ exit

Reference