Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to keep a network alias - without specifying the IP address in the hosts file? For instance, I have abcd.efgh.com but want abcd -> abcd.efgh.com so that ping and ssh work as they normally would.

I want it to work with dynamic IP on abcd.efgh.com, that's why I don't want to state the IP address explicitly.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

A slightly less convienent way is to set up aliases for both ping and ssh, for example:

alias pingfoo="ping foohost.domain.tld"

OpenSSH supports adding hostname aliases (and many more options) to ~/.ssh/config:

Host foo
    Hostname foohost.domain.tld
    # You can also add User, Port, every possible ssh(1) option.
share|improve this answer
    
Excellent; I can do without ping now that I think of it, 99.9 % of my use is ssh. –  Jonas Byström May 28 '10 at 12:08

With dnsmasq's option --cname=<cname>,<target> you can make a CNAME that indicates that <cname> is actually <target>. Note that this works only if the DNS name of <target> is known to dnsmasq (either in hosts file or through DHCP).

If dnsmasq doesn't work, Unbound is a DNS resolver that supports also unknown targets in a similar situation.

share|improve this answer
    
That's in DNS, not in the hosts file. –  mpez0 May 28 '10 at 11:05
    
@mpez0: It would still work (dnsmasq is intended to be run on one's LAN or a personal computer, as a caching DNS resolver) –  grawity Oct 10 '12 at 16:08

If your OS uses GNU glibc, you can use its $HOSTALIASES feature.

From the manual page of gethostbyname(3):

DESCRIPTION

       [etc etc etc] If name doesn't end in a dot and the environment variable
       HOSTALIASES is set, the alias file pointed to by HOSTALIASES will first
       be searched for name (see hostname(7) for the file format). The current
       domain and its parents are searched unless name ends in a dot.

This works with both gethostbyname() and the newer getaddrinfo(); however, it doesn't seem to affect ping at all...

share|improve this answer
    
Unfortunately not using libc. –  Jonas Byström May 28 '10 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.