36

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?

3
  • What if you switch the order, putting the wildcard section second.
    – esmit
    Feb 27, 2014 at 22:45
  • Have you tried it? I'm pretty sure that just gets you the right username with the domain wrong, if anything.
    – Mu Mind
    Feb 28, 2014 at 3:06
  • I haven't tried it, that's why I made it a comment answer as something to try, instead of a downvotable answer.
    – esmit
    Mar 1, 2014 at 6:28

5 Answers 5

22

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
2
  • 3
    That saves a little typing at least. Sounds like you're not aware of any way to avoid duplicating the domain information?
    – Mu Mind
    Sep 3, 2012 at 15:39
  • 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
    Jul 27, 2017 at 18:13
26

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.

1
  • 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. Dec 26, 2017 at 17:59
8

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

1
  • nice answer that did it for me! Sep 4, 2020 at 8:31
4

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.

2
  • What is the purpose of Host *? Wouldn't removing it have the same effect? Oct 12, 2021 at 1:33
  • 1
    It matches all hosts. The Host directive restricts the following declarations to the host. So if you have multiple host directives and you just put User myusername at the bottom, it wouldn't do anything, except on the most recent Host declaration. You'd need to put it at the top of the file. This just makes it explicit that it applies to all hosts. You can change it as you like.
    – jacob
    Oct 12, 2021 at 17:49
0

None of the above worked for me. I required the following config

Host *.mydomain.com
    User myusername

Host host1
    CanonicalizeHostname yes
    Hostname host1.mydomain.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.