Understanding Ubuntu’s hosts file

The hosts’ file is an extremely useful albeit small text file that stores host names with associated IP addresses. It is basically a mapping from hostnames to IP addresses.

Any network protocol uses this mapping to convert hostnames to numeric IP addresses. The mapping is because the Network Layer does not work with the hostnames.

Only the actual IP Addresses are used in any networking protocol. But since we humans find it easier to work with characters and names, it becomes convenient to use the hostname to remember a particular endpoint, rather than its IP Address.

As we know now, on the internet, we use Domain Name System (DNS) Resolution to resolve web addresses to IP Addresses.

When we are on a Local Area Network (LAN), it makes more sense to map IP addresses to hostnames directly, since there aren’t any DNS servers to query from. So, whenever a DNS server is NOT available, applications will consult this file directly.

Hosts file Functionality

The hosts file enables the unique assignment of IP addresses to hosts. This ensures that even if the names are the same, both these hosts will be searched for. This is common, especially as you move from network to network.

In our Ubuntu system, the hosts file is located at /etc/hosts.

You can use your favorite editor and open this file. I’ll be using vi editor.

root@ubuntu:~# vi /etc/hosts

The file will look somewhat similar to the below screenshot

The Etc Hosts File
The Etc Hosts File

By default, there are only a few entries listed. The first line corresponds to the local address of your machine.

This is commonly called localhost and is standardized as 127.0.0.1. So, any machine can access an application server on their own local address using this, but not anything else. This is the address that the machine uses to communicate with itself.

This entry must NOT be changed, as otherwise, there may become a problem when trying to communicate with applications.

The second line 127.0.1.1 ubuntu stands for the same thing as above. This is another alias mapping for the localhost address. But this line has more to it than just being an alias.

The second line ensures that other applications, when trying to communicate with the local machine, actually reach the destination hostname on a local network. If you change your hostname, you must change the ubuntu to your desired hostname in this file too.

Similarly, you can also change your static IP address by modifying the second line. Again, leave the first line unaltered.

Add an entry to /etc/hosts

Let’s add an entry to our hosts’ mapping. We’ll try to map onto this website (www.howlinux.com). But before that, let’s get the IP of the server, by using the ping command.

ping howlinux.com

Since this is outside our local network, we need to get the IP address by querying DNS servers. If a match is found, we’ll get back the IP address.

Ping Website
Ping Website

Okay, so the server is running, and it has the IP address as mentioned in the screenshot. So let’s add it to etc/hosts.

Hosts File After Adding Entry
Hosts File After Adding Entry

Let’s now save and exit. Now, if we want to go to howlinux.com, the system will directly refer to our /etc/hosts file, instead of looking up the DNS servers.

You can test it out, by opening the IP address on our browser while disconnected from the internet. Now obviously, it won’t connect you to the website but it will resolve the domain name even without a network. So essentially, this is the same as what the DNS protocol does! No wonder why this is used everywhere now, right?

Similarly, the next few lines are for IPv6 addresses. The first of those ::1 defines the localhost endpoint for a loop-back communication to itself, just like 127.0.0.1.

The next two lines are for multicasting to nodes and routers in the network since IPv6 relies heavily on UDP multicasting.

Summary

The main point of /etc/hosts was to start up communication with local applications itself, before querying other servers on the internet. Other than that, it has no significant impact on applications today. So, there is no need to worry about this file too much, other than the historical significance that this was used before DNS came into the picture.


References