9

I have many servers and they all end with the same servers.company.net, so for example vded-xx-001.servers.company.net, and was wondering if it is possible to make it so i can just type the vded-xx-001 and have it append the servers.company.net automatically ?

So i would want to type

ssh user@vded-xx-001

and have it actually connect to

ssh user@vded-xx-001.servers.company.net

I have tried setting my DNS-search domain to servers.company.net, in /etc/network/interfaces, but this did not achieve the desired outcome.

Anyone able to point me in the right direction ?

Thanks in advance

5
  • Is there any reason you can not just add search company.net to /etc/resolv.conf ?
    – Hennes
    Apr 16 '13 at 11:21
  • @Hennes Adding search company.net and servers.company.net to resolv.conf has worked. However there is a warning at the top of the file that my changes will be overwritten, if this is the case how do i make them permanent ? Apr 16 '13 at 11:27
  • 1
    I would love to answer that and rake in the extra rep, but this post already answers that quite well: :-) askubuntu.com/questions/157154/…
    – Hennes
    Apr 16 '13 at 11:35
  • Actually, contrary to what is discussed in that question, it's normally best to include a search domain list either in /etc/network/interfaces (using a dns-search option) or in the NetworkManager connection configuration field Search domains.
    – jdthood
    Apr 19 '13 at 14:46
  • Rather than messing with local resolver config this solution is simpler, and portable.
    – OrangeDog
    Jul 13 '18 at 11:02
21

Probably you already solved this, but maybe later it could help someone: you don't need to mess with your resolv.conf, just can use something like this in your ~/.ssh/config:

Host vded-*-001 test-*-something-fixed-*
        HostName %h.servers.company.net
        User someusername

So later you can just use:

ssh vded-alotofstuff-001
ssh vded-somethingels-001
ssh test-02-something-fixed-somethingelse
3
  • This should be the accepted answer.
    – slm
    Jul 14 '18 at 4:28
  • This is a way to solve the problem, but does not solve the problem of name resolution system-wide, it only solves the problem for ssh host names. The best practice for me is to be able to get the right hostname systemwide and not having to customize a particular service (ssh) to be able to resolve particular host names Aug 3 '18 at 13:35
  • 2
    Just in case I forget and look this up again later, if you want to allow both short and long names while sharing same config, simply specify a 1st entry Host vded-*.servers.company.net, with Hostname %h. Then in a 2nd entry Host vded-* with Hostname %h.servers.company.net and all the other params like User, IdentityFile, etc.
    – bksunday
    Mar 2 '19 at 4:24
7

This is the easiest solution. It works for all hosts, does not require root or access to any DNS/resolver systems.

Add to the top of your ~/.ssh/config file (or create if it doesn't already exist):

CanonicalizeHostname yes
CanonicalDomains servers.company.net

Documentation (man 5 ssh_config):

CanonicalizeHostname

Controls whether explicit hostname canonicalization is performed. The default, no, is not to perform any name rewriting and let the system resolver handle all hostname lookups. If set to yes then, for connections that do not use a ProxyCommand, ssh will attempt to canonicalize the hostname specified on the command line using the CanonicalDomains suffixes and CanonicalizePermittedCNAMEs rules. If CanonicalizeHostname is set to always, then canonicalization is applied to proxied connections too.

If this option is enabled, then the configuration files are processed again using the new target name to pick up any new configuration in matching Host and Match stanzas.

3
  • It works best for me. You don't have to add CanonicalDomains, just the 1st row. This way it works even if you connect to other networks. Jul 13 '18 at 10:10
  • 2
    @AdamWallner it you don't add CanonicalDomains then it will only work on the current network's search domain. If you do (and you can list multiple) then it will work for resolving all of those hostnames, on any network you might connect to.
    – OrangeDog
    Jul 13 '18 at 10:36
  • @deed02392 that's exactly what it does, as the documentation says.
    – OrangeDog
    May 22 '20 at 17:32
2

Yes, you can do this by creating a config file named ~/.ssh/config and entering the following contents:

Host vded-xx-001
User user
Port 22
HostName vded-xx-001.servers.company.net

Now you just have to type this (you don't even need the username any more):

$ ssh vded-xx-001

This also works with the command-line utility scp:

$ scp filename vded-xx-001:/path/
3
  • 1
    I am aware that this works for individual hosts but i have hundreds and was hoping i could create a rule that picks up on VDED-* and then for the hostname does host.servers.company.net Apr 16 '13 at 11:24
  • 1
    @TimLassieFreeborn: you could write a script that generates the entries needed for the config file. I don't know if you can do it dynamically, though.
    – Flimm
    Apr 16 '13 at 11:25
  • @TimLassieFreeborn see my answer
    – OrangeDog
    Jun 27 '18 at 13:31
2

The solution to my problem was to add the search domain to resolv.conf:

search servers.company.net

This has allowed me to enter

ssh user@vded-xx-001

for any of my servers and it connect to the correct address.

Thank you @Hennes for the answer

2
  • 2
    Assuming you are running Ubuntu 12.04 or later, you shouldn't edit /etc/resolv.conf directly because the resolvconf utility generates that file. (Actually it generates /run/resolvconf/resolv.conf to which /etc/resolv.conf is a symbolic link.) Instead you should configure DNS settings through the interface configurer, either ifup or NetworkManager. Ifup: Edit /etc/network/interfaces and add a dns-search servers.company.net line to the stanza for the machine's external network interface. NM: Add servers.company.net to the Search domains field on the IPv4 tab for the connection.
    – jdthood
    Apr 19 '13 at 14:30
  • If /etc/resolv.conf is not a symbolic link to ../run/resolvconf/resolv.conf on your machine then run sudo dpkg-reconfigure resolvconf to restore the symbolic link.
    – jdthood
    Apr 19 '13 at 14:31
1

If you're using SSH identities - and with that many servers it's worth looking at - then as OrangeDog said setting CanonicaliseHostname and CanonicaliseDomains will also enable a match to a @cert-authority entry in your known hosts

known_hosts entry @cert-authority *.example.com ssh-rsa AAAddadfkjaeio...

without Canonicalise options you'd need to use "ssh host.example.com"

With Canonicalise options "ssh myhost" will match.

I'd have added this as a comment to OrangeDogs answer if rep permitted...

0

For a quick solution that works across different programs, you can also set the domain vded-xx-001 to redirect to a specific IP address, by editing /etc/hosts to include a line like this:

173.194.41.90  vded-xx-001

This works in your browser: http://vded-xx-001/

As well as with command-line utilities like SSH:

$ ssh user@vded-xx-001

(I personally prefer the SSH config solution though.)

0

For system-wide domain name resolution, I like to set things one time in my network. So I'd set in the DHCP Server the domain name and DNS Server so that it gives all machines the right resolv.conf including

  • Primary DNS Server IP
  • Domain
  • search directive

This depends on the DHCP Server and the network config you want... I personally don't like machine custom config when its something general to the network

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.