This article is about editing the / etc / hosts file on Debian 10 Buster. But let’s first see what a Hosts file is.
Understanding the Hosts file
All operating systems store host files on the machine in order to translate host names to IP addresses. Every time you open a website by entering its hostname, your system will read the hosts file to check the corresponding IP address and then open it. The hosts file is a simple text file located in your Debian’s etc folder.
This is how it looks:
From what we mentioned, you might expect this file to have a very long list of IP addresses and corresponding host names; But this is not true. The hosts file contains only a few lines;
- The first part contains the names and IP addresses of your localhost and computer by default. This is the part that you usually modify to make the changes you want.
- The second part contains information about hosts with IPv6 support, and you are unlikely to edit these lines.
Whenever you enter an address, your system checks for a hosts file; if it is there, you will be redirected to the corresponding IP address. If the hostname is not defined in the hosts file, your system will check your Internet’s DNS server to find the corresponding IP address and redirect you accordingly.
Why edit the Hosts file?
By editing host files, you can achieve the following and more:
- Block site
- Reflect an attack or play a rally
- Create an alias for locations on your local server
- Replace addresses provided by your DNS server
- Controlling access to network traffic
How do I edit the Hosts file?
You can only edit the hosts text file located in / etc / hosts as superuser. First you need to open it in text editors like VI editor, Nano editor or gedit, etc. in Debian terminal. You will then make the necessary changes and save the file for these changes to take effect.
So, first, let’s open Terminal via search in the app launcher like this:
The Debian Application Launcher can be accessed with the Super / Windows key.
In this article, we will use the Nano editor to edit the hosts file. Please enter the following command to open the hosts file:
$ sudo nano /etc/hosts
$ sudo gedit /etc/hosts
As mentioned earlier, we will be editing the first part of the hosts file, which defines the IP addresses and hostnames. We will explain the following two ways of using the hosts file:
- Block site
- Accessing a remote computer through an alias
Block the website with the / etc / hosts file
You can block a website by redirecting it to the IP address of your localhost or to the default route.
For example, if we want to block google.com, we can add the following text to our file:
Now, when we open the Google website, our system takes the IP address of our localhost (127.0.0.1) from the hosts file and redirects us to it instead of the IP address of Google from our DNS server.
Now when we open the Google website, our system takes the default route IP (0.0.0.0) from the hosts file and redirects us to it instead of the Google IP from our DNS server.
This is what the edited file will look like. Save your changes by pressing ctrl + X and then typing y in the filename line.
Now when you try to open www.google.com in the browser you will see the following error message:
Please note that we have defined the full address www.google.com instead of the hostname google.com in the hosts file, because modern browsers sometimes bypass the block if we only define it later.
Accessing a remote computer through an alias
Now, suppose we have a server located on a local network that we want to access. Usually we need to enter the IP address of the server to access it if it is not defined in our local DNS. One way to avoid retyping the IP address is to assign an alias to the server in the hosts file as follows:
The IP address corresponds to the location of the server we want to access, and myserver is the new alias we want to use.
Now when we save the file and enter myserver in the address bar, we will be redirected to our remote server.
We learned that by making very simple changes to the hosts file, we can customize and thus redirect network traffic according to our needs. We can also get rid of a network attack or hoax by restoring the default hosts file.
How to edit the Hosts file in Debian