SSH config wildcard on expanded Hostname

26

10

I want to have a wildcard in my SSH config to set my default username to a particular value for all hosts on a certain domain. But I also want to have some short names for some particular hosts. I expected something like this to work:

Host *.mydomain.com
    User myusername
Host host1
    Hostname host1.mydomain.com

With those settings, if I type ssh host1.mydomain.com it evaluates to myusername@host1.mydomain.com, but if I type ssh host1 it doesn't apply my User setting and I instead see mylocalusername@host1.mydomain.com.

Is there a way to have the wildcards match on the final expanded hostname so I can type the short or long form and get the same results?

Mu Mind

Posted 2012-09-02T22:30:29.340

Reputation: 433

What if you switch the order, putting the wildcard section second. – esmit – 2014-02-27T22:45:49.700

Have you tried it? I'm pretty sure that just gets you the right username with the domain wrong, if anything. – Mu Mind – 2014-02-28T03:06:00.580

I haven't tried it, that's why I made it a comment answer as something to try, instead of a downvotable answer. – esmit – 2014-03-01T06:28:40.983

Answers

14

Simply use:

Host *.mydomain.com host1
User myusername

Host host1
Hostname host1.mydomain.com
  • Alternative patterns are supplied by a delimiting blank in a Host line.
  • All matching Host Patterns are applied.
  • If an option occurs multiple times, only the 1st occurance is used

user86064

Posted 2012-09-02T22:30:29.340

Reputation:

It would be great if SSH could just try adding the default domain, i.e. "lan" or "mycompany.com", instead of having to hard code it in the configuration file. – Saustrup – 2017-07-27T18:13:16.743

3That saves a little typing at least. Sounds like you're not aware of any way to avoid duplicating the domain information? – Mu Mind – 2012-09-03T15:39:04.490

19

You can simply set CanonicalizeHostname to yes to reparse ssh_config with the canonical hostname of your alias. For example:

CanonicalizeHostname yes
Host *.mydomain.com
    User myusername

Host host1
    HostName host1.mydomain.com

Alternatively, if you want to also remap hostnames, you can use Match instead of Host to match only the canonical hostnames. For example, in:

Match canonical host="*.mydomain.com"
    User myusername

Host host2.mydomain.com
    HostName host2.otherdomain.com

The user directive will not be set when you connect to host2.mydomain.com.

Timesquare

Posted 2012-09-02T22:30:29.340

Reputation: 191

Note that there are still old versions of ssh out there that don't support these options - if you run into one of those, as I did, the Host solution will still work. – Kyrstellaine – 2017-12-26T17:59:49.803

2

Making use of CanonicalDomains will also work.

CanonicalizeHostname yes
CanonicalDomains mydomain.com

Host *
User myusername

If you want to have a short name for a particular host, for example:

Host h
Hostname host1

I wouldn't recommend a name that short, however.

jacob

Posted 2012-09-02T22:30:29.340

Reputation: 31

2

You can use %h.mydomain.com in your .ssh/config.


 Host host?
 User myusername
 Hostname %h.mydomain.com

Host host?? User myusername Hostname %h.mydomain.com

Host host??? User myusername Hostname %h.mydomain.com

Now you can do:

ssh host1
instead of
ssh host1.mydomain.com
ssh host10
instead of
ssh host10.mydomain.com
and ssh host100

Regards, Bert

Bert de Ridder

Posted 2012-09-02T22:30:29.340

Reputation: 21