How To Configure The "/etc/hosts" File On Linux [ID 242490.1]

Applies to:

Linux OS - Version Enterprise Linux 4.0 and later
Linux x86
Linux x86-64
***Checked for relevance on 17-Jun-2010*** 
***Checked for relevance on 20-Feb-2013*** 


This article will show you how to properly configure the "/etc/hosts" file on a Linux platform.


This is intended for Linux system administrators and Linux DBAs with system administration skills.


What Is /etc/hosts And Why Is It Used?

In order to send network traffic to a host, the numeric Internet Protocol (IP) address for that host must be known.  The IP address is traditionally written as each xxx represents a value from 0 to 255, for an IPv4 network address.  Computers require these addresses but humans find remembering numeric values difficult.  The Domain Name Service (DNS) provides a mechanism to associate one or more alphanumeric names with a numeric IP address.  On a Linux system, these readable names are converted to their numeric IP equivalents by the Resolver Library, contained in the files provided as part of the glibc RPM package.  Programs that need to lookup the numeric IP address for a name issue calls to this library.

Host names and their IP addresses may be found in a variety of places: local files, remote DNS servers, or NIS+ servers, to name a few.  The order these resources are searched depends on the hosts: entry in the /etc/nsswitch.conf file.  This line typically looks like this:

hosts: files dns

This causes the resolver library to consult the local /etc/hosts file first; if the host name is not found there, then consult the remote DNS name servers identified by the/etc/resolv.conf file.

Linux does provide the bind RPM package to allow an administrative domain to configure and maintain its own DNS service, but frequently the size of the local network is only a few hosts and makes justifying the effort of maintaining a DNS service unwarranted.

Format Of The /etc/hosts File

The /etc/hosts file is an ordinary text file. Two types of lines are permitted:

  1. Empty Lines
  2. Host name definitions

Lines may be intermingled as needed.

Comments begin with a hash symbol (#) and continue to the end of the line.

Associating Host Names And IP Addresses

For each host a single line should be present with the following information:

IP_address canonical_hostname [aliases ...]

Fields of the entry are separated by any whitespace (spaces or tabs).

The first field is the numeric IP address to be used for all the host names on this entry.  Either an IPv4 address (, an IPv6 address (2001:0db8:0000:0000:0000:0000:1428:57ab), or an IPv6 abbreviation (::1) may be used, depending on your requirements.

After the IP address, remaining tokens specify the locally-known hostnames associated with that IP address.  By convention, the first name after the IP address is the canonical or fully-qualified domain name.  An example of a canonical name would be; this is the official name of the host.

Any remaining names defined for the IP address are aliases or alternate names for the official host name.  For example, suppose that one of the duties assigned is to be the corporate FTP site.  A suitable alias might then be and this name could be added to the /etc/hosts entry after the canonical name.  Many times, an alias is simply the host name, without any domain suffix. myhost myftp

The advantage to listing the canonical host name as the first definition on the line is that IP-to-hostname conversion (similar to reverse DNS lookups) usually display only the first name found; convention uses the canonical name for this.  In our example, the command:

$ ping myftp
PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from ( icmp_seq=2 ttl=64 time=0.028 ms
64 bytes from ( icmp_seq=3 ttl=64 time=0.028 ms

Note that we pinged myftp but results come from host myhost: this is a reliable hint that you are addressing an alias, not the actual host.

Official Reference

The format of the host table is described in the Internet Request-For-Comments (RFC) 952.  These RFCs are available many places around the Internet, such as the web site.


The example below shows a typical /etc/hosts file for an Oracle Real Application Cluster (RAC).       localhost    rac1      # Oracle 9i Rac node 1 - public network    rac2      # Oracle 9i Rac node 2 - public network        int-rac1  # Oracle 9i Rac node 1 - interconnect        int-rac2  # Oracle 9I Rac node 2 - interconnect

The cluster has two nodes, rac1 and rac2, that are accessible from the external network.

The cluster nodes are connected by a private interconnect that uses an internal IP network.  The int-rac1 and int-rac2 names refer to these network connections.

Note that the content of /etc/hosts is available only to the local host.  In a cluster environment, this means that each host must have its own copy of the /etc/hosts file.