How to Edit Host Files on Debian 12

Host files in different operating systems including Debian are used to map the IP address with their domain name. When the IP address is provided to the computer to send information or to open it, the computer first navigates the request to its host file. If there is no domain name found against that IP address in the host file, then the request is forwarded to the Domain Name Server (DNS) to provide the domain name. 

There is the need to edit the host file for multiple purposes such as:

  • To test the new website
  • To redirect the applications to the specific IP addresses
  • To create the alias of multiple locations on a local server
  • To access the control of the network traffic

For these purposes, the host file can be edited on Debian and other operating systems. This post will explain the steps to edit the host file on Debian Bookworm using the terminal. The outline of the post will be:

  1. How to Edit Host Files on Debian
  2. How to Block the Websites Using the Host Files on Debian
  3. How to Access the Remote Computer through an Alias on Debian
  4. How to Access the Control Files on Debian

How to Edit Host Files on Debian?

To edit the host files in Debian, follow the instructions explained in the next steps. 

Step 1: Open the Terminal

First, open the terminal by using the CTRL+ALT+T shortcut key or from the Application’s menu as shown below:

Step 2: Find the Host Files

The host files are located in the /etc/ directory and to confirm this, list down the contents of the host files:

$ ls /etc/

Step 3: Open the Host Files

When the host files are being seen in the /etc/ directory, then use the text editor to open it for editing purposes:

$ sudo nano /etc/hosts

Now the file of the /etc/hosts can be edited and to enter the IP address, follow the syntax mentioned below:

0.0.0.0 Domain_Name

This means first write the IP address and then the domain name that is supposed to be assigned to this IP address. 

How to Block the Websites Using the Host Files on Debian?

Some websites are supposed to restrict Debian such as to block the social media platforms. Open the host file using the text editor as explained in the above section and follow the below-mentioned command syntax:

IP_ADDRESS_LOCAL_MACHINE <a href="http://www.website_name.com">www.website_name.com

Similar to blocking the website on the local route, use the following syntax:

IP_ADDRESS_DEFAULT_ROUTE www.website_name.com

How to Access the Remote Computer through an Alias on Debian?

Users are required to know the IP address of the machine connected within the same network to share files. One method is to create an alias of the respective machine in the host files and access it by using the alias. 

For this purpose, open the host’s files and type the IP address of the computer with the alias name:

IP_Address mycomputer

Now the computer can be accessed by using the “mycomputer” rather than its IP address. 

How to Access the Control Files on Debian?

The websites can either be allowed or restricted by using the /etc/hosts.allow and /etc/hosts.deny files with the text editor. 

This is all about the editing of the host files on Debian using the command line interface. 

Conclusion

To edit the host files on Debian 12, open the terminal and use the text editor to open the host files located at /etc/hosts as explained in this writeup. 

This file is accessed to test the new websites, to block the websites, and to create the alias. All the cases are explained in this post with their general command syntax.