Network Integration  «Prev  Next»
Lesson 5IP addresses in the Host Table
ObjectiveUse the Host Table to look up network IP addresses and hostnames.

IP addresses in the Host Table using Linux Network Integration

Hostnames are very convenient when you need to identify networked computers. They convey more meaning for most people than IP addresses because they are easier to remember. The opposite is true from Linux's point of view: IP addresses are orderly and regular, while hostnames are seemingly random.
Linux overcomes this "randomness" by converting hostnames you supply into IP addresses. The first source checked is the /etc/hosts file, which holds number and name pairs. For this reason, /etc/hosts is referred to as the Host Table[1].

/etc/hosts file

The MouseOver below shows a typical /etc/hosts file. The left column shows an IP address and the right column the corresponding hostname. Roll your mouse over each line of the Host Table to see an explanation of each number and name pair.
etc hosts out
etc hosts example

Example /etc/hosts file

As your machine gets started, it will need to know the mapping of some hostnames to IP addresses before DNS can be referenced. This mapping is kept in the /etc/hosts file. In the absence of a name server, any network program on your system consults this file to determine the IP address that corresponds to a host name.
The hosts file is a computer file used by an operating system to map hostnames to IP addresses. The hosts file is a plain text file, and is conventionally named hosts. Originally, a file named HOSTS.TXT was manually maintained and made available via file sharing by Stanford Research Institute for the ARPANET membership, containing the hostnames and address of hosts as contributed for inclusion by member organizations. The Domain Name System, first described in 1983 and implemented in 1984, automated the publication process and provided instantaneous and dynamic hostname resolution in the rapidly growing network. In modern operating systems, the hosts file remains an alternative name resolution mechanism, configurable often as part of facilities such as the Name Service Switch as either the primary method or as a fallback method.

Purpose of hosts file

The hosts file is one of several system facilities that assists in addressing network nodes in a computer network. It is a common part of an operating system's Internet Protocol (IP) implementation, and serves the function of translating human-friendly hostnames into numeric protocol addresses, called IP addresses, that identify and locate a host in an IP network.
In some operating systems, the contents of the hosts file is used preferentially to other name resolution methods, such as the Domain Name System (DNS), but many systems implement name service switches, e.g., nsswitch.conf for Linux and Unix, to provide customization. Unlike remote DNS resolvers, the hosts file is under the direct control of the local computer's administrator.

File content

The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more host names. Each field is separated by white space tabs are often preferred for historical reasons, but spaces are also used. Comment lines may be included; they are indicated by a hash character (#) in the first position of such lines. Entirely blank lines in the file are ignored. For example, a typical hosts file may contain the following:
127.0.0.1  localhost loopback
::1        localhost
This example only contains entries for the loopback addresses of the system and their host names, a typical default content of the hosts file. The example illustrates that an IP address may have multiple host names (localhost and loopback), and that a host name may be mapped to both IPv4 and IPv6 IP addresses.

etc-host Example consisting of IP Address and DNS
etc-host Example consisting of IP Address and DNS
  1. This is the reference to the local machine. 127.0.0.1 is always reserved for the loopback or lo interface
  2. This line is a reference to the local machine, using its network address.
  3. This refers to another machine on the same network. Now you refer to host2 instead of its IP address.
  4. This is an alias for the gateway on your network.
  5. This is a machine on a different network; note the difference in the network portion of the address.

It is impractical to list every computer you will ever want to contact in one file. For example, imagine an office with 100 Linux computers. If the /etc/hosts file was the only name to number mapping, then when a new host was added, all 100 computers would need to be updated.
This would become an unwieldy task, so you need a way to more easily distribute name to number mappings. This is where the Domain Name System (DNS )[1] steps in.
In the next lesson, you will learn how to configure your network interface to use DNS.
[1]Host Table: A database of remote, network connected hosts maintained by each host on the network
[2]DNS: The Domain Name Server protocol. It replaces the Host Table as the primary means of resolving hostnames into IP addresses on large networks.