Black Friday Deals for Linux Users are Now Live!
Learn more
Nov 19, 2022 4 min read

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

Table of Contents

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 ( to human-readable form ( 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
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 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
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

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

ping -c1
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.

Linux Foundation Training
Sagar Sharma
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.