How to Edit Hosts File on Linux

Wondering how to edit Hosts file on Linux?

The hosts file is a text file present on every computer. No matter what operating system you are using, if you want to access different websites on the internet, you require the hosts file. The function of the hosts file is that it essentially maps down the IP (Internet Protocol) addresses of the websites you are trying to access. 

As we all know, every website on the internet has a URL (Uniform Resource Locator), also known as the Hostname. So whenever you try to open a website, the URL is converted into an IP, which is then compared to the IP stored in the hosts file. If the IP addresses match, you can then enter the website and vice versa. 

If it seems confusing, here is an example. Let’s say that you want to check out your Facebook timeline on your computer. You will proceed by opening your web browser. In the search bar, you will type Facebook and press enter. The internet system then proceeds by first converting the URL of your newsfeed into an IP address. Then it compares it in the hosts file to permit access. 

So, for using the internet and its unlimited resources, you now understand what an important role the hosts file plays. 

In the early days of the internet, you only had the hosts file for storing and managing the IP addresses of the different websites. The addresses had to be downloaded manually from a central repository. But as time progressed, the number of websites being added per day kept increasing to the point where it became impossible to check every IP on your hosts file. 

To solve this problem, DNS (Domain Name System) was introduced which kept everything in check automatically. So, these days it’s the DNS that provides the system with the converted URL for the hosts file. 

Modifying and editing your hosts file on Linux and any other OS is an engaging activity. The hosts file provides us with different ways to play around with it. If you want to learn how to edit Hosts File on Linux, this article will guide you on how to do it. 

So hop on for an interesting ride!

How to Open and Edit Hosts File on Linux.

If you are wondering why would someone want to modify the hosts file? The answer to this question is that you can block a website, handle a cyberattack, or create an alias of your location on the local server. 

You can edit the hosts file on Linux and any of its kernels using the command terminal. You can either use a graphic editor such as Gedit or text editors such as Nano or Vim on the terminal. With the prerequisites out of the way, you are now ready to learn how to edit Hosts file on Linux.

Follow these steps to open and edit the hosts file on Linux.

  1. Open the terminal by pressing Ctrl + Alt + T.
  2. Type the following command in the terminal.
$ sudo nano /etc/hosts
  1. For editing through a text editor, replace nano with gedit. On pressing enter, the terminal would ask for your password. 
$ sudo gedit /etc/hosts
Edit Hosts File on Linux
  1. As we have opened the hosts file, we can move on to the editing part. For editing, you first need to know the format. The IP addresses come first and then the hostname. For example:
0.0.0.0 hostname
Edit Hosts File on Linux
  1. From here on, everything is pretty basic. To make changes according to our own choice, you can simply need to assign IPs to hostnames. For example, if you have your website, you can assign an IP of your choice by typing this in the editor:
195.2.0.1 <nameofyourwebsite>

As you can see, editing a hosts file is very easy as long as you know what you are doing. Doing something wrong could prove costly. 

How to Block a Website Using the Hosts File.

Users might want to block a website for different reasons. Some websites can prove to be a nuisance in a smooth browsing experience. So, if you want to block a website, there are different methods, but you can do it through the hosts file as well.

The mechanism for blocking a website using the hosts file is pretty simple. We can assign an arbitrary IP to the website we want to block. So, when someone or the browser attempts to access that website, the IPs won’t match, and thus the website gets blocked. 

To do this, just follow these steps:

  1. Open the hosts file using the terminal, as explained before.
  2. After opening the editor, you should see this window open. 
Edit Hosts File on Linux
  1. Now, to block the website, just assign an IP to the website. As we have assigned an arbitrary IP to Twitter.

Blocking a website is this easy using the hosts file. Time to say goodbye to the irritating websites. 

Setting Up an Alias for your System.

People who are a part of a localized internet network (such as in offices) know that for accessing a computer on the local server, they have to type the IP address of that computer on the search bar. 

If that system is being used regularly, the alternate method to typing the IP of that system again and again. is to assign it an arbitrary hostname. With this, you can access any machine using the hostname assigned by you.

To achieve this, follow these steps:

  1. Open hosts file using the terminal as explained before.
  2. Access the hosts file through an editor.
  3. Once opened, just assign the IP of that particular system to an arbitrary name.
open hosts file in terminal

With this, the user only has to type mainsystem in the search bar to access that machine. Another way the hosts file can be used to your advantage. 

We hope this article helped you learn how to edit Hosts file on Linux. We explained the different ways the hosts file can be edited to our advantage using the “nano” text editor. You can also perform these tasks using gedit. 

Speaking of gedit, having problems with highlighted words in gedit dark mode? You can check out this article for an easy fix.

Knowing about the hosts file and its functions can be very useful for certain purposes, as we have seen. Understanding how the internet works is the only way to learn that it is complex yet beautiful and brilliant. We hope that this article enables you to edit your hosts file in Linux.

If this guide helped you, please share it.  🙂

Leave a Reply

Your email address will not be published.

Previous Post
Discord Logo

How to Install Discord Application in Ubuntu and Other Linux Distributions

Next Post
NMAP

How To Use Nmap With Proxychains

Related Posts