109

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.

1
  • I guess I may be missing something. Are you saying you couldn't have /etc/hosts look like 10.0.3.4 some.host.org another.domain.com You have to use a reference for some programmatic reason? Nov 16, 2012 at 23:23

8 Answers 8

80

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 real-hostname

Host fakehost2
  Hostname real-hostname2

Host fakehost3
  Hostname real-hostname3
2
41

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.

12
  • 4
    didn't work for me on Ubuntu 12.04 :(
    – Dimitry K
    Nov 17, 2014 at 17:36
  • 3
    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, 2015 at 14:13
  • 2
    Also does not work for curl
    – Benubird
    Jun 17, 2015 at 9:55
  • 8
    it will rarely end up in the expected result. HOSTALIASES only works for applications using getaddrinfo(3) or gethostbyname(3) - This mean that it will work for applications trying to explictly resolve the hostname by a specific system call: It is never the case. see unix.stackexchange.com/questions/10438/… so the only solution is local dns (dnsmasq)
    – Nadir
    Sep 17, 2016 at 9:19
  • 1
    Seems this is trick is deprecated. The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() functions are obsolete. If your system runs DNSMasq locally you can alias in that for any resolution that uses DNS.
    – teknopaul
    Mar 16, 2017 at 15:44
26

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 

1
  • 5
    a light-weight DNS server to try would be dnsmasq (it also serves DHCP).
    – sybreon
    Sep 14, 2009 at 16:25
22

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

11

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

3
  • 1
    Yeah, now it seems the only solution. Thanks. Cannot upvote since I don't have enough reputation.
    – user10178
    Sep 14, 2009 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, 2009 at 16:16
  • 1
    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. Sep 14, 2009 at 16:37
6

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.

5

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

1
  • I tried using /etc/hosts.aliases in CENTOS 6 but this feature does not seem to work. Nor can I find any documentation on its use or existance. Where should I be looking?
    – mdpc
    Jun 15, 2017 at 19:43
0

This is how I do

in .basrhc

[distro defaults]
source ~/.bashrc_custom # only relevant to this host
source ~/.bashrc_common # useful to have on any machine
                # sync this to any host I manage.

in ~/.bashrc_custom

[host relevant config]
source ~/.bash_lib/net.host

in ~/.bash_lib/net.host

#-*- shell-script -*-

#             /\             /\
#            |`\\_,--="=--,_//`|
#            \ ."  :'. .':  ". /
#           ==)  _ :  '  : _  (==
#             |>/O\   _   /O\<|
#             | \-"~` _ `~"-/ |
#            >|`===. \_/ .===`|<
#      .-"-.   \==='  |  '===/   .-"-.
# .---{'. '`}---\,  .-'-.  ,/---{.'. '}---.
#  )  `"---"`     `~-===-~`     `"---"`  (
# (            Infra système             )
#  )                                     (
# '---------------------------------------'
alias net.host.ad.principal.rdp='krdc rdp://[email protected]'
alias net.host.dns.rdp='krdc rdp://[email protected]'
alias net.host.dns.ssh='ssh [email protected] -p <port>'
[...]

#             /\             /\
#            |`\\_,--="=--,_//`|
#            \ ."  :'. .':  ". /
#           ==)  _ :  '  : _  (==
#             |>/O\   _   /O\<|
#             | \-"~` _ `~"-/ |
#            >|`===. \_/ .===`|<
#      .-"-.   \==='  |  '===/   .-"-.
# .---{'. '`}---\,  .-'-.  ,/---{.'. '}---.
#  )  `"---"`     `~-===-~`     `"---"`  (
# (               Infra web              )
#  )                                     (
# '---------------------------------------'

alias net.host.appsrv.ssh='ssh [email protected] -p 22022'
alias net.host.cloud.ssh='ssh [email protected]'

#             /\             /\
#            |`\\_,--="=--,_//`|
#            \ ."  :'. .':  ". /
#           ==)  _ :  '  : _  (==
#             |>/O\   _   /O\<|
#             | \-"~` _ `~"-/ |
#            >|`===. \_/ .===`|<
#      .-"-.   \==='  |  '===/   .-"-.
# .---{'. '`}---\,  .-'-.  ,/---{.'. '}---.
#  )  `"---"`     `~-===-~`     `"---"`  (
# (            Infra messagerie          )
#  )                                     (
# '---------------------------------------'
alias net.host.messagerie.backup.ssh='ssh -lroot 192.168.100.25 -p 44044'
alias net.host.messagerie.prep.ssh='ssh [email protected] -p 44044'
alias net.host.messagerie.secours.ssh='ssh [email protected] -p 44044'
[...]




[... plus other sections ...]

On the command line, I can do

$ net.host.bandit.ssh 

this will connect to bandit.labs.overthewire.org changing the destination of this is as easy as editing my .bash_lib/net.host file

I can also do :

$ net.host.ksc.rdp 

to rdp to 192.168.100.12 as user a.chaouche

etc.

You can think of any command you like. The general template is

alias net.host.<host>.<command>='<command> <host address specification>'

Another approach would be to store the host address specifications as global env variables as suggested by christopher. I do this for files and directories I like to cd to. For example :

~ $ cd $MAIL_<tab>
$MAIL_LASTPASS         $MAIL_NEWUSERS         $MAIL_ROUNDCUBEFOLDER  $MAIL_SCRIPTS
~ $ cd $MAIL_SCRIPTS
/usr/local/scripts/mail $ ls.count
219
/usr/local/scripts/mail $ type ls.count
ls.count is a function
ls.count ()
{
    set -- $1*;
    echo $$
}
/usr/local/scripts/mail $

So you can have the same type of variables to use for hosts

ssh  $HOST_MAIL
nmap $HOST_WEBSERVER
ping $HOST_SVNSERVER

You must log in to answer this question.