Linux – SSH Config setting default user for multiple hosts under the same domain + aliases

linuxssh

This thread: In your ssh config is it possible to have one host entry for multiple machines on the same domain explained the 'aliases' part of my question, I was wondering if it's possible to set default username AS well as use domain aliases, so I can simply have to write something like:

ssh subdomain

I have the following in my /etc/resolv.conf file:

search my_cool_domain.com

and the following in my ssh_config file:

Host *.my_cool_domain.com
    User deploy

So I figured the following will work:

ssh subdomain

will try to connect to subdomain.my_cool_domain.com with user deploy but this does not work for some reason.

Although the following:

ssh deploy@subdomain does work, as well as:
ssh subdomain.my_cool_domain.com

Any idea how can I fix this (If it's even possible) in an elegant way?

Best Answer

The patterns after the keyword "Host" are not resolved in anyway to real host names, so you cannot detect that a subdomain that your dns lookup is configured for it actually a subdomain of your main domain.

From the ssh_config manpage -

         ... The host is the hostname argument given on the command line (i.e. the
         name is not converted to a canonicalized host name before match-
         ing).

Your alternative is to, either

  1. Use "Host *" and set the default username for all hosts.
  2. When/if you have some kinds of prefixes to your subdomains, use "Host pfx1* pfx2*".
  3. Configure your shell completion and always use full host names, "Host *.mydomain" and "ssh subdomain" that completes it to "ssh subdomain.mydomain".
  4. Shell aliases and function can also be used to add the ".mydomain" part automagically. For example, "ssh-alias subdomain" that is an alias for "ssh subdomain.mydomain".