How to ssh to servers internally by hostname without domain name?
You can wildcard and use %h
in your config
eg
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.
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.
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.