I need to log into multiple servers at work to get my work done. I'm getting tired of typing the FQDN of a server to access. I'm logging in/out via ssh on our own private network. I'm 99% sure it's on our own private network b/c all the servers have ip addr 10.x.y.z. Is there a way to ssh into servers with just the hostname and not the domain name?

We have servers in multiple countries. The way our servers are named is very long. It is named as follows:


I am getting carpal tunnel typing in ssh me@hostname1.country.domainname.com

... every time I access one of our servers. If I'm in the US and I try to access another host that's in the US, I can just type ssh me@hostname2 and I connect fine. However, if I'm in the US and try to connect to a server in England, I can't type ssh me@hostname3.eng and connect to hostname3.

The workaround I did was setup an alias in my ~/.ssh/config file for some of the servers. However, I don't think it's feasible to add 1000+ servers into that file. I've already added 20+ servers and my co-workers think I'm crazy, although I think they are crazy for typing the FQDN when sshing around.

Is there an easy way for us to set up something so that we don't have to type in our domainname.com each time?


You can wildcard and use %h in your config


Host *.eng
  Hostname %h.domainname.com

Now when you do ssh foo.eng it will try to connect to foo.eng.domainname.com.

You can add other options to this config as well; eg forcing the username

Host *.eng
  Hostname %h.domainname.com
  User me

Now when you do ssh foo.eng it will try to connect to foo.eng.domainname.com as the user me.

% ssh foo.eng
ssh: Could not resolve hostname foo.eng.domainname.com: Name or service not known

(well, obviously I get an error before it's not a valid hostname for me!)

So now you only need one rule per country.

  • My first thought was DNS aliases, the second was hosts file, but I missed that there's multiple servers. This solution looks excellent. What config gets the line(s) you've posted? – Xalorous Sep 22 '16 at 0:55
  • This goes into $HOME/.ssh/config or you can make it system wide in /etc/ssh/ssh_config – Stephen Harris Sep 22 '16 at 0:59
  • Does it work with user@*.eng? – Xalorous Sep 22 '16 at 1:01
  • 3
    With the rule I wrote you can do ssh foobar@somewhere.eng and it will work as expected. All this does is rewrite the hostname component. It'll even work with scp and sftp and so on. – Stephen Harris Sep 22 '16 at 1:03

If you add

search domainname.com

to /etc/resolv.conf, and use hostname.country, ssh (and other network programs, for that matter) will automatically append domainname.com for you1.

I don't think adding the different country domains to your search path is a good idea because you may get unexpected behavior if two servers in two different countries share the same hostname2

I believe this method is better than changing the ssh configuration because this allows hostname.country to resolve regardless of the program you're using (telnet, VNC,...).

See resolv.conf(5)

1 More accurately, it will append domainname.com if it can't resolve hostname.country by itself.

2 In such a scenario hostname will resolve to the server in the country whose domain is listed first in the search path.

  • 2
    BTW you can also do this as non-root and per process by env variable LOCALDOMAIN. man resolv.conf – rudimeier Sep 22 '16 at 21:30

You could use the CanonicalDomains option in your ssh config.

Adding the following to your ssh config file will make ssh try to append domainname.com to any host that has at most 1 dot in its name :

CanonicalizeHostname yes
CanonicalDomains domainname.com

With this config ssh foo.eng will first try foo.eng.domainname.com, and fallback to foo.eng if the host cannot be found. Likewise, ssh github.com will first try github.com.domainname.com, so if you want to connect to GitHub, your DNS server should not return records for non existing hosts.

The CanonicalizeMaxDots can be used to control how many dots can appear in the hostname before ssh considers it fully qualified and doesn't append domainname.com. it defaults to 1 which should be enough for you given the scheme you currently have, but if you ever get to something like hostname.city.country you would need to increase it.

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.