Most developers will interact with resources that use SSH keys instead of passwords. I recently overheard someone say that he uses the same SSH key for all of his accounts, which is a bad idea from a security perspective. The more places a single key is authorized, the more valuable that key becomes. If that key gets compromised, more targets are put at risk. There are other reasons I might have multiple key pairs:
- Team resources that share the same key
- Older systems that don’t support the newest ed25519 encryption algorithm
- Separate keys for each consulting client I have
When I initially started managing multiple SSH key/password combinations on my personal machine, I learned best practices from a variety of sources. I’m writing this information down in one place for the benefit of others. My current OS of choice is MacOS, but these instructions should work for any *nix system. Atlassian recommends users replace their SSH keys once a year for security. Following these steps will ensure that you can.
First: Generate a new key
Open terminal and navigate to ~/.ssh to generate a new SSH key:
ssh-keygen -t ed25519 -f ~/.ssh/key_name -C "email@example.com"
Here is what each flag means:
- -t specifies the algorithm that makes the key.
- -f specifies a custom name for the key (assuming you’re in the ~/.ssh directory), and an alternate location if it’s in the form of a path. The key_name is the name of the key. Make this as specific as possible.
- -C is an option to include a comment. It can be anything but is usually in the form user@host (who generated the key)
I always use a key name that is specific and makes sense to me. This makes key management easier in the long term. You should use a passphrase when prompted.
Second: Create known_hosts file
When you complete the first step two files are created: key_name and key_name.pub. The first is your private key and the second (with the .pub extension) is your public key.
Create a known_hosts file for each account you have because it makes diagnosing issues easier when you have multiple keys. Ideally the name of this file is similar enough to the key name that you aren’t confused later.
Third: Set up the config file
The config file sets options for each host. Create the config file if it doesn’t already exist and then open it for editing. I label each key for visual neatness and to avoid confusion as the list of keys gets longer over time. Create a comment using the # at the start of a line to label each host. The text in the picture below is available here to save you some typing.
Here is the breakdown of what each line means:
- Host is a pattern matcher that is used to differentiate between these sets of configurations. Keep it the same as the HostName so it matches hosts in connections correctly without additional specification.
- The URL on the HostName line is the base URL where the repository resides. For example, if you have a personal account on github with personal projects, the URL will be github.com.
- User for git based systems will be git. The value of User will be different if you connect to something else (i.e. ec2-user for connecting to an Amazon AWS EC2 instance)
- IdentityFile asks for the location of the identity key we made. Type in the respective path here.
- AddKeysToAgent allows a private key that is used during authentication to be added to ssh-agent if it is running
- UseKeychain (macOS only) allows the computer to remember the password each time it restarts
- UserKnownHostsFile specifies an exact location to store all hosts you connect to when you’re using that profile. Provide the respective paths here and choose a unique known hosts file name (see step 2 above) so that troubleshooting and key maintenance over time is easier.
- IdentitiesOnly specifies that only the keys provided must be used to connect to a host, even if another service like the ssh-agent offers a key for use.
Fourth: Add keys to ssh agent
Add keys to ssh agent if passphrase was used. Skip to the next step if you didn’t use a passphrase. Start the ssh agent in the terminal:
eval "$(ssh-agent -s)"
Add private keys to the agent in terminal:
ssh-add -K path_to_private_keyname
Note that the -K option works only on mac for keychain access.
Fifth: Finishing up
If you’re using a service like Bitbucket or Github, add public keys to the clipboard and paste them into the appropriate account (i.e. bitbucket):
cat key_name.pub | pbcopy
Finally, verify the configuration in the terminal:
ssh -T firstname.lastname@example.org
With multiple keys, I have the option of creating new keys as needed to keep each connection secure. If I have a single compromised key, then I only worry about changing that single key. My config file makes it easy for me to use multiple keys.