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

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


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:

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/