How to Edit the Hosts File on Debian

This article is about editing the /etc/hosts file on a Debian 12 system. But let us first see what the Hosts file is.

Understanding the Hosts File

All major operating systems have a hosts file to translate hostnames to IP addresses. The hosts file has priority over DNS lookups. Whenever you open a website by typing its domain name, 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 of your Debian system, the full path is /etc/hosts.

Here is what it looks like:

Debian hosts file

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 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; 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 the Hosts File?

By editing the hosts files, you can achieve the following and much more:

  • 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 the 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 Debian terminal. Then, you will then make the required changes and save the file for these changes to take effect.

So first, let us open the Terminal through the Application Launcher Search as follows:

Debian Terminal

The Debian Application Launcher can be accessed through the Super/Windows key.

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

$ sudo nano /etc/hosts

Or

$ sudo gedit /etc/hosts

(for gedit)

Edit the hosts file

As mentioned, 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 using the /etc/hosts file

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 google.com, we can add the following text to our file:

127.0.0.1 www.google.com

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

OR

0.0.0.0 www.google.com

Now when we open the google website, our system will take the IP of the default route (0.0.0.0) 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 and entering y on the filename prompt.

Block certain websites using /etc/hosts file

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

Website blocked

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

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:

192.168.1.10 myserver

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

Use hosts file to define server name aliases

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.

Frequently Asked Questions

What is the hosts file?

The hosts (or /etc/hosts) file in Debian Linux is a simple text file used by the operating system to map hostnames to IP addresses. It allows the system to translate domain names (like `www.example.com`) into IP addresses before querying external DNS servers.

Where is the hosts file located in Debian Linux?

In Debian, the hosts file is located at /etc/hosts.

How can I edit the hosts file in Debian Linux?

To edit the hosts file, you need root privileges. You can use a text editor like nano or vi. For example, use:

sudo nano /etc/hosts

or

sudo vi /etc/hosts

to open and edit the file.

Why would I need to modify the hosts file?

Common reasons include setting up a local testing environment for web development, blocking access to certain websites, or redirecting traffic for specific domains.

What is the format for entries in the hosts file?

Each entry in the hosts file should be on a new line, starting with the IP address followed by the hostname. For example:

127.0.0.1 localhost

Can editing the hosts file affect my system's security?

Incorrect configurations in the hosts file can lead to network issues or security vulnerabilities, such as redirecting traffic to malicious sites. Always ensure accurate and intentional changes.

How do I block a website using the hosts file?

To block a website, add a line to the hosts file with 127.0.0.1 followed by the domain name of the website. This will redirect any requests for that site to your local machine, effectively blocking it.

Will changes to the hosts file take effect immediately?

Changes usually take effect immediately, but in some cases, you might need to flush your DNS cache or restart networking services.

How do I comment out a line in the hosts file?

To comment out a line, simply add a # symbol at the beginning of the line. This makes the line a comment, and it will be ignored by the system.

Is it possible to use the hosts file for load balancing?

The hosts file is not suitable for load balancing as it can only map a hostname to a single IP address. For load balancing, more sophisticated methods like DNS round-robin or dedicated load balancers are needed.

Can I use wildcards in the hosts file?

No, the hosts file does not support wildcards. Each entry must be a specific IP address and hostname pair.

How can I troubleshoot issues with my hosts file?

Ensure the file's syntax is correct, with no typos in IP addresses or hostnames. You can also use tools like ping or nslookup to test the resolution of the hostnames you've added or modified.