How to set up an alias server name

15

8

I want to set up an alias server name on my laptop (Linux). I do not want to use the /etc/hosts/ file since the IP address of the remote server changes. The reason I want to do this is because the server name is 27 characters long. I want to do this:

ssh server

Instead of:

ssh server.subdomain.domain.com

I have several subdomains that I use. How do I set this up?

PS: I do not consider this a dupe because other similar answers do not address the fact that an IP address will change.

Jess

Posted 2013-03-29T14:24:28.443

Reputation: 594

~/.ssh/config and /etc/resolve.cnf below are both excellent answers. – Jess – 2013-03-30T11:47:19.237

Answers

20

Use file ~/.ssh/config

example content:

Host jane
HostName long.server.name
User root

then you can use ssh jane instead of ssh root@long.server.name

If IP address changes and you do not know the revDNS of this server you can try to use command host 1.0.0.1 where 1.0.0.1 is the IP address - this wil give you current revDNS name that you will be able to configure.

If hostname (reverse DNS) changes with the ip change or your server is behind a NAT - you can either use Dynamic Dns (dyndns.org) and/or use port forwarding.

mnmnc

Posted 2013-03-29T14:24:28.443

Reputation: 3 637

7

In order to use the shorter "ssh server" instead of "ssh server.subdomain.domain.com" you simply need to append "subdomain.domain.com" to the search field in /etc/resolv.conf. If there is no search field you can create one.

For example - suppose your /etc/resolv.conf looks like this:

search domain1.com domain2.com domain3.com
nameserver 1.2.3.4
nameserver 5.6.7.8

Modify the search line to look like this:

search domain1.com domain2.com domain3.com subdomain.domain.com

You can place subdomain.domain.com at the front of this list if you want it to be searched first.

User123456

Posted 2013-03-29T14:24:28.443

Reputation: 501

1I like your answer . Since it will allow the usage of sorter name of the server across multiple applications. Still, I think it might generate more DNS traffic with each connection attempt - but i wouldn't bet on this. – mnmnc – 2013-03-29T19:48:26.863

1Many modern operating systems have the ability to cache the responses to dns queries locally. The first time the record is queried the answer would come from dns but subsequent queries would be answered from cache until the ttl of the record expires. You can view the contents of the cache in windows with "ipconfig /displaydns" and with "sudo killall -INFO mDNSResponder" and then viewing the contents of /var/log/system.log on mac osx. – User123456 – 2013-03-29T22:54:46.007

So i guess if the entry is cached and not expired - if at this point the ip of the destination changes - you will get connection timeout. Unless I'm missing something? – mnmnc – 2013-03-30T00:43:17.680

1

If this is just for ssh, you can configure a 'short cut' name in ~/.ssh/config

After that is done then ssh server will work every time assuming the fqdn resolves to an ip address.

For details, see http://kb.mediatemple.net/questions/1625/Using+an+SSH+Config+File or the man page for ssh_config.

ewm

Posted 2013-03-29T14:24:28.443

Reputation: 131

1

You could add an alias in your .bashrc or .zshrc:

alias server1='ssh server1'

With server1 added in your ~/.ssh/config for example:

Host server1
Hostname address
User username-on-this-server

Atropo

Posted 2013-03-29T14:24:28.443

Reputation: 1 513