Skip to main content
Explain

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

The /etc/hosts file is an integral part of the Linux system. But what is it used for?

Sagar Sharma

Warp Terminal

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
use ping command in linux

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

sudo nano /etc/hosts
how to redirect url in linux

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:

traffic has been redirected to different IP in linux

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
check ip using ping command in linux

Now, let's open the hosts file:

sudo nano /etc/hosts
block specific website in linux using the hosts file

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

how to block website using the hosts file in linux

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
use ping command to check connectivity of domain

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

sudo nano /etc/hosts

And make changes as given:

create website shortcuts using hosts file in linux

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

how to create url shortcuts using the hosts file in linux

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.

Sagar Sharma