18

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

6 Answers6

31

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
deed02392
  • 103
  • 4
Mauricio
  • 326
13

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.

OrangeDog
  • 909
  • 11
  • 20
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/
Flimm
  • 44,031
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

Mat
  • 103
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

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

Flimm
  • 44,031