Multiple SSH host aliases

October 16, 2018

When I'm dealing with SSH connections using key pairs, usually I put this in my ~/.ssh/config:

Host <easy-to-remember-host>
Hostname <ip-address>
 User <user>
 IdentityFile ~/.ssh/id_rsa_host_key

Running:

$ ssh <easy-to-remember-host>

SSH client will then automatically select correct key, find matching user and appropriate IP address. Works as expected, especially if there are multiple hosts and multiple keys (you don't use single key for all your servers, right?).

For some time I was looking for solution to have multiple Host values, but single Hostname and everything what goes in that block. In short, I wanted to be able to say ssh foo or ssh baz and in both cases, SSH client to use the same IP address, keys and other options.

This failed miserably of course:

Host foo
Host baz
Hostname <ip-address>
 User <user>
 IdentityFile ~/.ssh/id_rsa_host_key

but when I found this post, I learned that actual solution was very simple:

Host foo baz
Hostname <ip-address>
 User <user>
 IdentityFile ~/.ssh/id_rsa_host_key

To be honest, I should be blamed; after re-reading ssh_config man page once again, but with more focus now, this is stated:

Host  Restricts the following declarations (up to the next Host or
      Match keyword) to be only for those hosts that match one of
      the patterns given after the keyword.  If more than one pattern
      is provided, they should be separated by whitespace.
      ...	 

I have to remind myself (once again) that Open(SSH|BSD) man pages are on totally different level - succinct, precise and usually answers the question.