Can i configure ubuntu to append a domain to the end of an ssh hostname request?

17,395

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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/

Solution 5

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...

Share:
17,395

Related videos on Youtube

Tim Lassie Freeborn
Author by

Tim Lassie Freeborn

Well i'm me ... Ubuntu Desktop user and server administrator for others :-)

Updated on September 18, 2022

Comments

  • Tim Lassie Freeborn
    Tim Lassie Freeborn over 1 year

    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 [email protected]
    

    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

    • Hennes
      Hennes about 11 years
      Is there any reason you can not just add search company.net to /etc/resolv.conf ?
    • Tim Lassie Freeborn
      Tim Lassie Freeborn about 11 years
      @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 ?
    • Hennes
      Hennes about 11 years
      I would love to answer that and rake in the extra rep, but this post already answers that quite well: :-) askubuntu.com/questions/157154/…
    • jdthood
      jdthood about 11 years
      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.
    • OrangeDog
      OrangeDog almost 6 years
      Rather than messing with local resolver config this solution is simpler, and portable.
  • Tim Lassie Freeborn
    Tim Lassie Freeborn about 11 years
    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
  • Flimm
    Flimm about 11 years
    @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.
  • jdthood
    jdthood about 11 years
    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
    jdthood about 11 years
    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.
  • OrangeDog
    OrangeDog almost 6 years
    @TimLassieFreeborn see my answer
  • Adam Wallner
    Adam Wallner almost 6 years
    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.
  • OrangeDog
    OrangeDog almost 6 years
    @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.
  • slm
    slm almost 6 years
    This should be the accepted answer.
  • Philippe Gachoud
    Philippe Gachoud almost 6 years
    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
  • bksunday
    bksunday about 5 years
    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.
  • OrangeDog
    OrangeDog almost 4 years
    @deed02392 that's exactly what it does, as the documentation says.