Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to alias a hostname in Linux?

It has been asked by jmillikin at various Ubuntu forums as follows:

Is it possible to create a hostname alias? Sort of like /etc/hosts, but with other hostnames rather than IP addresses. So that with some file like this, you could ping "fakehost1", and it would be re-mapped to "realhost", and then "realhost" would be resolved to an IP address.

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

share|improve this question
I guess I may be missing something. Are you saying you couldn't have /etc/hosts look like You have to use a reference for some programmatic reason? – jim_m_somewhere Nov 16 '12 at 23:23

For those who don't have an account on the forums (or don't wish to login):

if your main issue is not to ping but to ssh, you can create/edit your ~/.ssh/config adding lines like these:

Host fakehost1
Hostname realhost

Host fakehost2
Hostname realhost

Host fakehost3
Hostname realhost
share|improve this answer
The Hostname part needs to be on a new line. Here is a good tutorial on how to make a config file like this: – Code Commander Aug 6 '12 at 18:41
good workaround, thanks. But of course, better solution would be to use cnames on dns – Yura Vasylenko Jan 19 at 22:54

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 :

fakehost1 IN CNAME realhost 
fakehost2 IN CNAME realhost 
fakehost3 IN CNAME realhost 

share|improve this answer
Thanks, I will try it tomorrow after setting up my local dns server. – user10178 Sep 14 '09 at 16:12
a light-weight DNS server to try would be dnsmasq (it also serves DHCP). – sybreon Sep 14 '09 at 16:25

Linux supports aliasing by setting the HOSTALIASES env variable.

echo "fakehost realhost" > /etc/host.aliases
echo "export HOSTALIASES=/etc/host.aliases" >> /etc/profile
. /etc/profile

then you can

ping fakehost

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.

share|improve this answer
didn't work for me on Ubuntu 12.04 :( – Dimitry K Nov 17 '14 at 17:36
The HOSTALIASES only work for executables that are don't have the setuid flag set. So it won't work for ping (unless you're already root when executing ping). I guess this is done to avoid tricking setuid-executables into connecting to a different host than it intented. – Boris Jun 4 '15 at 14:13
Also does not work for curl – Benubird Jun 17 '15 at 9:55
Works great for maven with poms from Windows users :-) – selalerer Jul 19 '15 at 10:51
does not work on centos 6.4 either. – minghua Jun 9 at 23:45

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=""

then, in a fresh shell:

ssh christopher@$fakehost

share|improve this answer

The only way this can be done is if you have your own local dns server.

share|improve this answer
Yeah, now it seems the only solution. Thanks. Cannot upvote since I don't have enough reputation. – user10178 Sep 14 '09 at 16:11
If the ever-changing IP address of a DSL or dialup connection is the issue, dyndns is a better solution, IMHO. – Sven Sep 14 '09 at 16:16
No, it's not a better solution. Once the server has a hostname that is handled by something else then all the OP needs is a CNAME for the first hostname. DYNDNS and similar rely on the host informing the dns server that it's ip has changed. – theotherreceive Sep 14 '09 at 16:37

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

share|improve this answer

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.

( is a good choice, IMHO).

share|improve this answer

Your Answer


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