Linux Commands

How to Modify a Host File in Linux

The host file is a plain text file used by the operating systems to map Domain names to their corresponding IP addresses.

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, 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 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: internal.linuxhint

To edit the host file located in:


Use your favorite text editor:

sudo vim /etc/hosts

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list