Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH config file setup with wildcards and DRY

Tags:

ssh

config

dry

My requirements are as follows

  1. All of our cloud machines need the same config (User, Port, IdentityFile)
  2. I need these settings to be applied to them based on domain (*.xyz.com)
  3. I also need these settings to be applied when i setup short hosts (See below)

My current SSH config is a

Host shortname1?
    Hostname %h.prod.xyz.com

Host test-myname
    Hostname combo.test-myname.xyz.com

Host *.xyz.com
    Hostname %h

Hostname *.xyz.com
    User myuser
    Port 12345
    IdentityFile ~/.ssh/id_rsa

The exact use cases are ssh shortname1a; ssh shortname1b; ssh test-myname; ssh combo.test-myname.xyz.com

Host *.xyz.com was a recent addition after debugging for the last use case, and this somehow seems to break the first 2.

Is this the right way or is there a neater way?

like image 621
Karthik T Avatar asked Jul 05 '16 03:07

Karthik T


1 Answers

On top of figuring out why the config was "breaking", I discovered a new directive Match. This lets me do what I intend

Host shortname1?
    Hostname %h.prod.xyz.com

Host test-myname
    Hostname combo.test-myname.xyz.com

Host *.xyz.com
    Hostname %h

Match Host *.xyz.com
    User myuser
    Port 12345
    IdentityFile ~/.ssh/id_rsa

This config does exactly what I need. I needed to use Host instead of Hostname in the match because it didnt seem to accept Hostname but Host seems to be exactly what I need.

I was having arbitrary behaviour because I missunderstood that Hostname could not be used as I was using before, as a filter or match like statement. It can only be used inside a Host, or Match and as such, was implicitly below the last Host statement.

like image 117
Karthik T Avatar answered Oct 29 '22 19:10

Karthik T