Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
What if you switch the order, putting the wildcard section second. – esmit Feb 27 '14 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 '14 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 '14 at 6:28
up vote 7 down vote accepted

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
share|improve this answer
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 '12 at 15:39

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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