How to alias a hostname?

16,790

Solution 1

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.

Solution 2

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.

Solution 3

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:
16,790

Related videos on Youtube

Jonas Byström
Author by

Jonas Byström

Python haxxorz and parapsychology student. Creator of the eminent finplot library: https://github.com/highfestiva/finplot. Once upon a time made a UFO short documentary in Swedish: https://www.youtube.com/watch?v=HFF528pc3O8. Constantly at a fork in the road.

Updated on September 17, 2022

Comments

  • Jonas Byström
    Jonas Byström almost 2 years

    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.

  • mpez0
    mpez0 about 14 years
    That's in DNS, not in the hosts file.
  • Jonas Byström
    Jonas Byström about 14 years
    Unfortunately not using libc.
  • Jonas Byström
    Jonas Byström about 14 years
    Excellent; I can do without ping now that I think of it, 99.9 % of my use is ssh.
  • user1686
    user1686 over 11 years
    @mpez0: It would still work (dnsmasq is intended to be run on one's LAN or a personal computer, as a caching DNS resolver)
  • Boris
    Boris about 9 years
    It doesn't affect ping because ping has setuid set and HOSTALIASES only works with executables that don't have this flag. If you already are root it works, but not when using ping as a normal user.
  • mightyiam
    mightyiam almost 9 years