Although this tutorial focus on the host file in Linux, the host file is available in all operating systems, including Windows and Mac OS.
How the Host File Works
The host file acts as a local DNS record, but it takes higher precedence than a DNS server. Once a client queries for a domain name such as http://linuxhint.com, the domain name needs to translate to its corresponding IP address.
The system will first search the host file for the entry that points to linuxhint.com. If it does not exist, the system moves on to query the DNS servers such as Google servers, CloudFlare, or one provided by your Internet Provider.
The host file is handy when you’re working on local or internal networks, and you need your machine to recognize the local domains. The host file also allows you to block various domains by adding entries pointing to invalid or local addresses.
Let us discuss how to modify the Linux host file.
How to Edit the Linux Host File
Editing the Linux host file is incredibly easy as it is a simple text file. However, it does follow a specific format:
Here is an example of an entry in the Linux host file:
To edit the host file located in:
Use your favorite text editor:
The first section allows you to add IPv4 addresses and their corresponding domain names. In the second section, you can add IPV6 addresses.
You can add any domain and its corresponding IP to the host file or even an invalid one if you wish to block a website.