hosts file

From LQWiki
(Redirected from Hosts)
Jump to navigation Jump to search

The /etc/hosts is a plaintext file present on most *nix-like operating systems that contains a list of IP addresses in use by the system as well as those of other devices it is aware are connected to the same networks and the name(s) by which they are identified, along with any user-specified aliases created for them. Due to the arrival of the public domain name system the criticality of this file has greatly diminished and is retained primarily for compatibility purposes. Though rarely used today, it can still prove useful on small local networks, and during the boot cycle, before any connections to name servers have been established. Within the context of a local network, provided the system has been pre-configured for it, the first network connections any computer will be able to make as it boots are those recorded in the hosts file.

More relevant to today's use cases is the fact that the hosts file can be hacked to override the addresses supplied by DNS for specific domain names that host undesirable content (i.e. ads, spam or malware) and redirect them to the IP address of the loopback adapter (127.0.0.1 or [::1] on IPv4 and IPv6, respectively, both representing the localhost).

Format

The layout of the file is a simple whitespace-delimited list of IP addresses (one per line) followed by the most-qualified name known for the host, followed by any aliases (domain agnostic names). For example:

# Do not remove the following two lines, or various programs that require
# network functionality will fail.
127.0.0.1          localhost localhost.localdomain
::1                localhost6 localhost6.localdomain6

192.168.1.1        gateway gateway.lan
192.168.1.2        fileserver1.example.net
192.168.1.32       foo1
192.168.1.64       foo2

75.126.162.205     wiki.linuxquestions.org LQWiki lqwiki

Constraints

  1. The fields within each entry must be separated by one or more whitespace characters (spaces and/or tabs, and any combination thereof).
  2. The first character of all hostnames must be an alphabetic character and the last character must be alphanumeric.
  3. Hostnames must be constructed using only alphanumeric characters, the hyphen-minus character (-) and full stop character (.).
  4. If a line contains an octothorpe character (#), anything following it until the next newline is found is ignored.

Location

*nix-like systems

For the vast majority of *nix-like systems, the only acceptable location for the hosts file is /etc/hosts.

Microsoft Windows 2000/XP/Vista/7/8(.1)/10/11

For as much as Microsoft has historically tried to rewrite most widely-adopted computing and networking rubrics in favor of Windows-specific approaches, even they had to acquiesce to the ubiquity of this one. Expressed using the Microsoft filesystem representation, the hosts file on Windows 2000/XP/Vista/7/8(.1)/10/11 is located at %windir%\system32\drivers\etc\hosts.

Use cases

Boot process

As written above, when a system is booting up, the first computers it will know about are the ones listed in the hosts file. For most systems, the entries in the hosts file will contain information about important hosts on the local network for when DNS isn't accessible/available.

NIS

Network Information Service (NIS) is a network management system developed by Sun Microsystems. Networks that use NIS take the entries from the hosts files of the systems on the local network to build the NIS host database. On most NIS networks, every system on the local network will have an entry in their hosts file for every other host on the local network.

Isolated networks

Very small private networks that are isolated from contact with the Internet or any other network use the hosts file instead of DNS. Since the network is not connected to the Internet, setting up a DNS server on such a small network tends to be more work than it's worth.

Considerations

DNS versus hosts

When setting up a hosts file, the question to keep in mind is; Who/What does this machine have to know about? Who/What is going to tell this machine? Well, there is a file that tells the machine which to check first when looking for another machine; nsswitch.conf. This file is also under the /etc/ directory and tells the machine what order it should look at DNS, NIS and the hosts file.

Updating

When using hosts files on a small network, and you have to make a change in a hosts file on one machine on the local network, you need to make sure that the change gets reflected in *all* hosts files on the network.

Possible use cases

Override/Bypass DNS

There are times when a website or server to which you are trying to connect on the internet has incorrect/false entries in its DNS records. You know the server is there by IP address, but can't connect to the domain because of the errant DNS entries. In cases like this, you can just make an entry in your hosts file. An old example from my hosts file:

195.56.146.238        openwrt.org dev.openwrt.org downloads.openwrt.org forum.openwrt.org wiki.openwrt.org www.openwrt.org

This was an entry I had in my hosts file during a period that the OpenWrt project was moving their servers. The new DNS had propagated already and where DNS was pointing, there was no server yet. So, I added the above line to be able to access the server at the previous location. As of this writing, openwrt.org is correctly responding at 78.24.191.177, so I have since commented out the entry.

Content blocking

Many folks get tired of those floating flash-banner ads that all but completely cover the screen, forcing you to click to just get to the page you asked for. Then there are all the banner ads that will also create a bunch of popups on your screen. Well, when you have, at long last, had enough of those servers, you can stop the loading of the content they serve with a simple entry in your hosts file. Here's an example:

127.0.0.1        alternate.exampleadserver.com irritating.exampleadserver.com

Looping the entry for the offending advertising server back to your localhost will cause the areas mapped for the ads to give 404 errors in those places on the webpage where their content would have been displayed.

See also