JavaScript Editor Source code editor     Website development 

Main Page

Previous Section Next Section

Name Resolution Using Hosts Files

As you learned earlier in this hour, a hosts file is a file containing a table that associates hostnames to IP addresses. Hostname resolution was developed before the more sophisticated DNS name resolution, but hostname resolution is still used on some networks, especially smaller networks that do not require the overhead and expense of operating DNS. Some networks might use a hosts file for local lookups and still use DNS for remote queries, such as lookup for Internet access.

Configuring hostname resolution on a small network is usually very simple. Operating systems that support TCP/IP recognize the hosts file and use it for name resolution with little or no intervention from the user. The details for configuring hostname resolution vary, depending on the implementation. The steps are roughly as follows:

  1. Assign an IP address and hostname to each computer.

  2. Create a hosts file that maps the IP address to the hostname of each computer. The hosts file is often named hosts, although some implementations use the filename hosts.txt.

  3. Place the hosts file in the designated location on each computer. The location varies, depending on the operating system.

The hosts file contains entries for hosts that a computer needs to communicate with, allowing you to enter an IP address with a corresponding hostname, an FQDN, or other aliases statically. Also, the file usually contains an entry for the loopback address, The loopback address is used for TCP/IP diagnostics and represents "this computer."

To statically enter an IP address means that after it is entered, the address must be changed manually.

The following is an example of what a hosts file might look like (the IP address of the system is on the left, followed by the hostname and an optional comment about the entry):


#this machine


#Bob's workstation



When an application on a computer needs to resolve a name to an IP address, the system first compares its own name to the name being requested. If there is no match, the system then looks in the hosts file (if one is present) to see whether the computer name is listed.

If a match is found, the IP address is returned to the local computer and, as you learned in earlier hours, is used with ARP to obtain the hardware address of the other system. Now communication between the two can take place.

If you're using hosts files for name resolution, a change to the network forces you to edit or replace the hosts file on every computer. You can use a number of text editors to edit the hosts file. On a Unix system, use a text editor such as vi, Pico, or Emacs; on Windows, use Notepad; on DOS-based computers, use Edit. Some systems also provide TCP/IP configuration tools that act as a user interface to configure the hosts file.

When you create or edit the hosts file, be sure to keep the following points in mind:

  • The IP address must be left justified and separated from the hostname by one or more spaces.

  • Names must be separated by at least one space.

  • Additional names on a single line become aliases for the first name.

  • The file is parsed (that is, read by the computer) from top to bottom. The IP address associated with the first match is used. When the match is made, parsing stops.

  • Because it is parsed from top to bottom, you should put the most commonly used names at the top of the list. This can help speed up the process.

  • Comments might be placed to the right of a # symbol.

  • Remember that the hosts file is static; you must manually change it when IP addresses change.

  • Incorrectly configured hosts files (that is, typographic errors within hosts files) can cause problems with name resolution. If the wrong address is returned to the requesting application during the resolution process, the application might not function properly.

  • Although FQDNs are allowed and work in hosts files, their use in hosts files is discouraged and can lead to problems that are difficult for an administrator to diagnose. The local administrator who controls the hosts file does not have any control over the allocation of IP addresses and hostnames on a remote network. Therefore, if a server on the remote network is assigned a new IP address, and the FQDN in the local hosts file is not updated, the hosts file continues to point to the old IP address.

A hosts file is a very efficient and simple way to provide name resolution for a small, isolated TCP/IP network. Some of the implementation details differ, depending on the operating system. Consult your vendor documentation.

    Previous Section Next Section

    JavaScript Editor Source code editor     Website development