What is the purpose of /etc/hosts?

The file /etc/hosts started in the old days of DARPA as the resolution file for all the hosts connected to the internet (before DNS existed). It has the maximum priority, meaning this file is preferred ahead of any other name system.1

However, as a single file, it doesn't scale well: the size of the file becomes too big very soon. That is why the DNS system was developed, a hierarchical distributed name system. It allows any host to find the numerical address of some other host efficiently.

The very old concept of the /etc/hosts file is very simple, just an address and a host name:

127.0.0.1      localhost

for each line. That is a simple list of pairs of address-host.2

Its primary present-day use is to bypass DNS resolution. A match found in the /etc/hosts file will be used before any DNS entry. In fact, if the name searched (like localhost) is found in the file, no DNS resolution will be performed at all.


1 Well, the order of name resolution is actually defined in /etc/nsswitch.conf, which usually has this entry:

hosts:          files dns

which means "try files (/etc/hosts); and if it fails, try DNS."

But that order could be changed or expanded.


2 (in present days) 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 an octothorpe (#) 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 localhost6 ipv6-localhost ipv6-loopback mycomputer.local
192.168.0.8 mycomputer.lan
10.0.0.27   mycomputer.lan

This example contains entries for the loopback addresses of the system and their host names, the first line is a typical default content of the hosts file. The second line has several additional (probably only valid in local systems) names. 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, as shown on the first and second lines respectively. One name (mycomputer.lan) may resolve to several addresses (192.168.0.8 10.0.0.27). However, in that case, which one is used depends on the routes (and their priorities) set for the computer.

Some older OSes had no way to report a list of addresses for a given name.


Using the /etc/hosts file to give a human readable name to a local system within a desktop environment is perfectly reasonable. The hosts file is great to use in a home network or even in a small business environment. This cannot be public side like internet addressing -- then you need DNS. If the local network is large enough, or simply cut in different sub-networks, or any other useful reason, DNS is preferred.

The local hosts file & DNS are managed with different priority so there is never any conflicts.


in linux, dare I say there should always be at least 127.0.0.1 localhost within the file /etc/hosts as well as the corresponding IPv6 address for localhost.

It is simply host name resolution to a numerical IPv4 or IPv6 address. When you try to network to (a) unix.stackexchange.com somewhere on earth that is likely not near you, or (b) myotherpc on the local area network in your home, it is either /etc/hosts or some domain name server (DNS) that is doing the host resolution so when you type unix.stackexchage.com which as a human is what you care about, you don't have to remember 185.53.179.7 for it which is what the computer really needs to know.

As was pointed out, in linux there is /etc/nsswitch typically that defines host name resolution order, meaning check DNS first before checking NIS then lastly check the file /etc/hosts otherwise host not found

How you manage the order checking and what/where/who manages the DNS or NIS service is up to you, or you can override everything by modifying /etc/nsswitch to only check in /etc/hosts and never check with DNS or NIS or...

For a simple home network of few computers, say 2 to N, an admin would simply edit /etc/hosts on every computer to be correct, would be easier, faster, cheaper than setting up a Domain Name Server or some other service to have one master location or authority to do host name resolution to IP address.

for linux web search the history & reason behind localhost. Many things in linux rely on the name localhost resolving to 127.0.0.1 and if that does not happen will cause problems, and this localhost definition would/should happen in a local system file such as /etc/hosts.

/etc/hosts is by default usually the final say and last thing for the linux operating system to fall back on for host name resolution. in Windows XP and 7 the corresponding file is C:\Windows\System32\drivers\etc\lmhosts I don't know about windows 8 or 10.

Tags:

Dns

Hosts

Etc