45

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 [email protected], but if I type ssh host1 it doesn't apply my User setting and I instead see [email protected].

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
    Commented 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
    Commented 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
    Commented Mar 1, 2014 at 6:28

6 Answers 6

30

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
    Commented 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
    Commented Jul 27, 2017 at 18:13
29

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

You can use %h as a token for Host in the HostName of 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
  • ssh host100 instead of ssh host100.mydomain.com
1
  • nice answer that did it for me! Commented Sep 4, 2020 at 8:31
5

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? Commented 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
    Commented Oct 12, 2021 at 17:49
1

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
1

Instead of any free-floating parameters in my .ssh/config, I prefer encapsulation in Host blocks so that parameters can be overridden without a Match block. This way is still only 3-6 lines except for the annotating comments, and none of the configuration leaks to other hosts. Any generic changes to the default parameter values can go in a Host * at the bottom, in line with the manpage suggestion:

Since the first obtained value for each parameter is used, more host-specific declarations should be given near the beginning of the file, and general defaults at the end.

Otherwise, you discover that you've set everything to CanonicalizeHostname yes, and you have to refactor when that turns out be a hindrance for some other Host.

# Wildcard matches host1, host2, etc.
# Alternatively, `host*` to match multiple suffix characters.  You can also
# add additional patterns like `files?` or `app` to append the domain to them.
Host host?
  # Token replacement with `%h`, so host1, host2, etc. map correctly
  HostName %h.example.com
  CanonicalizeHostname yes

  # This is optional, but you can have a separate user for host1 and friends
  # than you do for the rest of `*.mydomain.com` if that's helpful.
  User specialusername

# If you ONLY use host1 and friends when you visit `*.mydomain.com`, you can
# just have the `User` parameter above and drop this block entirely.
Host *.example.com
  User myusername

The previous answers to this question don't combine things in quite the same way I do, although Max's post comes close. Max is only missing the %h token, which is pretty useful for combining rules on big systems.

There's tab-completion from ssh htab to ssh host, and you can type 1enter to visit host1.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .