Is it possible to alias a hostname in Linux?
It has been asked by jmillikin at various Ubuntu forums as follows:
# Real host # Aliases
realhost fakehost1 fakehost2 fakehost3
Somebody has answered about ssh, but not about ping, etc. My main
purpose is to use it as an alias for a Subversion server. In my case, realhost
is under a dynamic IP address. So, the "/etc/hosts" alias doesn't work. I want to
access my Subversion server as svn://my_svnserver/my_repos
instead of
svn://realhost/my_repos
.
For those who don't have an account on the forums (or don't wish to login):
Linux supports aliasing by setting the HOSTALIASES env variable.
then you can
N.B. ping requires you to set this up as root, but you can do it as any user for any application that runs as that user. ping suids to root.
You can setup this on your DNS server, CNAME records allow a machine to be known by more than one hostname. So add CNAME records to your DNS server like that :
I often do this with environment variables. I know this only works for the command line, but it is where I am most often craving hostname aliases (I worked with several supercomputer accounts, all with long URLs). Here's an example, if you're using BASH. In ~/.bashrc:
export fakehost="long.ass.annoying.url.org"
then, in a fresh shell:
ssh christopher@$fakehost
The only way this can be done is if you have your own local dns server.
You'll need an dyndns server that will map your current IP address to a hostname. You'll tell this server about your current IP address whenever you login, and it will update your hostname record.
The use of /etc/hosts.aliases is a standard feature of the bind resolver libraries. It's more robust than adding entries to /etc/hosts and can be used if you can not add CNAMES to your DNS (don't have access to it).
In general the best practice is to use CNAMES in the DNS with appropriate SEARCH defined in /etc/resolv.conf.
Updating /etc/hosts is not very robust as entries have to be kept in sync with IP changes. This only really works on a small scale or when you are using a name service to distribute the hosts map (eg via ldap).
Another solution may be DNSMasq http://en.wikipedia.org/wiki/Dnsmasq