19

As I understand it, the hosts file is one of several system facilities that assists in addressing network nodes in a computer network. But what should be inside it? When I install Ubuntu by default

127.0.0.1 localhost 

will be there. Why?

  • How does /etc/hosts work in case of JVM systems like Cassandra?
  • When is DNS alternative, I guess not on a single computer?
  • Very related: unix.stackexchange.com/questions/388875/… – Kusalananda Feb 2 '18 at 18:02
  • My /etc/hosts contains 127.0.0.2 mycouchdb bookmarkdb myothercouchapp and 127.0.0.3 myapachi website I use these to help me with my development running on my local machine. I have 3 services listening on port 80. I don't want to put them on the DNS, as they are only local. – ctrl-alt-delor Feb 2 '18 at 18:38
  • Have you tried man 5 hosts? – Martin Schröder Feb 6 '18 at 22:30
  • Does Linux not have a man page for this where it is explained in some detail? This is the most fundamental of questions. – Rob Feb 8 '18 at 12:05
46

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.

5

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.

  • 4
    I would disagree about conflicts, I've had a number of hard-to-debug problems because host-provision-by-group-A left stale /etc/hosts entries that in the future caused operations-by-group-B (my group) to fail, and we ended up setting something to sanitize the contents of /etc/hosts – thrig Feb 2 '18 at 18:02
  • conflicts inside the file can exist off course but then it s the machine owner own fault :) – francois P Feb 2 '18 at 18:08
  • 2
    I also disagree with never having any conflicts. I had to debug very strange errors due to stale host entries conflicting with DNS changes. – Rui F Ribeiro Feb 2 '18 at 20:39
  • @thrig: Perhaps the problem is that your groups are too large to effectively use /etc/hosts. Works perfectly well for the 5 machines (at the moment) on my home network. – jamesqf Feb 3 '18 at 4:05
  • Well the system doesn't think there's a conflict, It's got clear instructions as to the priority of things :p – StarWeaver Feb 3 '18 at 10:29
3

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.

  • A typical use of the hosts file, particularly in Windows, is to divert a program's attempt to 'call home' to a null device. Far be it from me to suggest this might be to thwart a check for legality. – Laurence Payne Feb 3 '18 at 16:15
  • I still haven't found the editor on my iPhone that lets me edit /etc/hosts... so I run DNS on my lan – ivanivan Feb 4 '18 at 4:58

protected by Michael Mrozek Feb 2 '18 at 21:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.