Multi-SSH key profile config Linux/MacOS

What if you need to SSH with key authentication to more than a single host from your laptop or server? This came up while rebuilding a host, its worth sharing…

If you only ssh to a single host, you’d never have an issue, but when you need to generate a second key, you’ll need to create a config file in your ssh directory. The config file can then know where to find the private key to match for your ssh session on the remote host. Here is how this works. I have subfolders for each server and use the config file to point to them (You can configure this how you wish, just ensure you have rights to the path!).

Files and folder layout:

/Users/Gabe/.ssh/Server1/(contains id_rsa and id_rsa.pub files)
/Users/Gabe/.ssh/Server2/(contains id_rsa and id_rsa.pub files)

The config file is placed here and does the magic:

/Users/Gabe/.ssh/config

Here is what is the config file looks like when firing up a terminal:

# nano /Users/Gabe/.ssh/config

2015-07-23_02-55-20

 

To connect an ssh session, simply type:

# ssh Server1

Let’s say we needed to add a third local key-pair entry to an SSH host at 192.168.1.5 for remote user named Spiderman. It would look like this:

 

2015-07-23_02-57-24

 

2015-07-23_02-56-35

Win.

Side note – probably best to configure limited permissions on your private id_rsa files!

# sudo chmod 600 /path/to/id_rsa