etc hosts

All operating systems with network support have a hosts file to translate hostnames to IP addresses. Whenever you open a website by typing its hostname, your system will read through the hosts file to check for the corresponding IP and then open it. The hosts file is a simple text file located in the /etc folder on Linux and Mac OS (/etc/hosts). Windows has a hosts file as well, on Windows you can find it in Windows\System32\drivers\etc\

Here is how it looks like:

The /etc/hosts file on Linux

From what we mentioned, you might expect this file to have a very long list of IPs and corresponding hostnames; but that is not true. The hosts file only contains several lines;

  • The first part, by default, contains the hostnames and IP addresses of your localhost and machine. This is the part you will usually modify to make the desired changes.
  • The second part has information about IPv6 capable hosts and you will hardly be editing these lines.

Whenever you type an address, your system will check the hosts file for its presence; if it is present, you will be directed to the corresponding IP. If the hostname is not defined in the hosts file, your system will check the DNS server of your internet to look up for the corresponding IP and redirect you accordingly.

Why Edit /etc/hosts file?

By editing the hosts files, you can achieve the following things:

  • Block a website
  • Handle an attack or resolve a prank
  • Create an alias for locations on your local server
  • Override addresses that your DNS server provides
  • Control access to network traffic

How to Edit /etc/hosts file?

You can edit the hosts text file located at /etc/hosts only as a superuser. You will first have to open it in text editors such as VI editor, Nano editor or gedit, etc. in the Linux terminal. You will then make the required changes and save the file for these changes to take effect.

In this article, we will use the Nano editor to edit the file. Please type the following command:

$ sudo nano /etc/hosts


$ sudo gedit /etc/hosts

(for gedit)

Edit the /etc/hosts file with nano editor

As mentioned before, we will edit the first part of the hosts file where IP addresses and hostnames are defined. We will explain the following two ways in which you can make use of the hosts file:

  • Block a website
  • Access Remote Computer Through an Alias

Block a Website

You can block a website by redirecting it to the IP of your localhost or the default route.

For example, if we want to block, we can add the following text to our file:

Now when we open the google website, our system will take the IP of our localhost ( from the hosts file and redirect us to that instead of the google IP from our DNS server.


Now when we open the google website, our system will take the IP of the default route ( from the hosts file and redirect us to that instead of the google IP from our DNS server.

This is how the edited file will look like. Please save the changes by hitting ctrl+X.

Edited hosts file to block access to a domain

Now when you try to open from your browser, you will see an error message as follows:

Site blocked successfully on the desktop

Please note that we have defined the complete address instead of just the hostname in the hosts file because modern browsers sometimes circumvent the block if we only define the latter.

There are also some predefined blocklists available that you may use in your hosts file:

Access Remote Computer through an Alias

Suppose we have a server located on a local network that we want to access. We usually have to type the server’s IP to access it unless it has been defined on our local DNS. One way to avoid typing the IP, again and again is to assign an alias to the server in the hosts file as follows: myserver

The IP corresponds to the location of the server we want to access, and myserver is the new alias we want to use.

Redirect domain to new IP

When we save the file and type myserver in the address bar, we will be redirected to our remote server.

We have learned that by making very simple changes to the hosts file, we can customize and thus redirect the network traffic according to our needs. We can also eliminate a network attack or prank by restoring the hosts file to its default.

The Hosts File on Linux

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn