How To Edit & Modify HOSTS File in Linux

How to edit and modify the IP address hosts file on Linux systems.

When entering a website name, for example, TheLinuxCode.com in the address bar, the operating system will search in that file for the IP address to establish the connection.

Therefore all the changes we make in the hosts file will be analyzed first.

To know the web addresses that are currently registered in the hosts file, we go to the etc/hosts path:

sudo nano /etc/hosts

This will be the result:

How To Edit HOSTS File in Linux

We have seen that an editor is required to access the hosts file and know its contents, it is vital that we access it with administrative privileges since otherwise, we can not make any changes to it.

The syntax to use to prevent access to a website is as follows:

127.0.0.1     (website_url)

For example, if we want to block one of the sites where we spend more time, Facebook, we will use the following line:

127.0.0.1 facebook.com

But not only can we block websites, but we can also allow the redirection to our own servers by entering just the name of this, for example, in the hosts file we can create the following entry:

192.168.0.15 Server_TheLinuxCode

In this way, in the browser, it will no longer be necessary to enter the IP address but the assigned name, and it will redirect us automatically.

We save the changes using the key combination Ctrl + O, and we left the editor using Ctrl + X.

The results of blocking, for example, a website are immediate:

This process applies to all Linux distros.

With this, we check what a file as simple as the host can do in the behavior of web accessibility

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *