What is the Purpose of /etc/hosts File in Linux

The /etc/hosts file in Linux or any other operating system is used to map connections between IP addresses and domain names.

Quite a generic answer right? Well, this guide is going to get some feet deeper so everyone can benefit from its offering.

What is the /etc/hosts file in Linux?

In the earlier days of networking, the /etc/hosts file was used to translate the IP addressed (8.8.8.8) to human-readable form (www.google.com) and over time it lost its relevance. In modern systems, the whole process of resolving domain names is done through DNS (Domain Name System).

Then why do we still get the hosts file in every operating system?

Rarely but still being used in given cases:

  • When you want to block certain websites.
  • It can be used as a backup in the case when DNS is broken.
  • You can also use it as a local DNS server.

You can think of many other tasks such as blocking ads, enhancing local firewalls, and many other tasks that involve the usage of IPs and DNS!

So now it's time for some practical examples of how you can utilize the /etc/hosts file.

Use the /etc/hosts file to redirect URLs

The hosts file needs two components to work: domain name and IP.

So what I'm going to do is use the DNS of itsfoss and will redirect it to the IP of LHB.

For that, I'll be using the ping command to find the IP of LHB:

ping -c1 linuxhandbook.com

Now, let's open the hosts file (I'm using the nano text editor here):

sudo nano /etc/hosts

What I've done here is:

<IP address><space><Root Address> 
<IP address><space><WWW domain address>

It is considered good practice to have both!

Now, when I pinged itsfoss, it is being redirected to the IP of Linuxhandbook:

You can use this method for migrating your website from one server to another. Before you actually change the DNS of the website to the new server, you make all the changes on the new server which is accessible through the domain name on your system.

Gridpane uses this trick for migrating WordPress websites.

GridPane
GridPane helps WordPress agencies and developers build their own self managed WordPress hosting business.

Block unwanted websites using the /etc/hosts file

So what I'm going to do here is assign my local domain to the sites that I no longer want to use!

For example, I'll be blocking Facebook! So let's get the IP address of Facebook:

ping -c1 facebook.com

Now, let's open the hosts file:

sudo nano /etc/hosts

And when I used the browser to access Facebook, it showed me this:

Create Website shortcuts using the /etc/hosts file

In this example, I'll be creating a shortcut for Linuxhandbook named lhb.com.

The first step will be similar to what I explained earlier (getting the IP of a domain):

ping -c1 linuxhandbook.com

Now, let's modify the hosts file by the given command:

sudo nano /etc/hosts

And make changes as given:

Once you're done, you put it to the test. I'm using the ping command with a recently made shortcut.

As you can clearly see, there's no difference between the IP of the original domain and the shortcut.

Wrapping Up

In this tutorial, I explained the purpose behind having a hosts file and some examples of how you can tweak DNS settings to suit yourself using the hosts file.

And if you still have any queries, the comments section is open for discussion.