Oct 24, 2023 4 min read

How to Modify and Manage the Hosts File on Linux

Modify and manage the host file on Linux with our step-by-step tutorial. Host file is used to map IP addresses to domain names on a local system.

Modify and Manage the Hosts File on Linux
Table of Contents

Introduction

Before we start talking about how to modify and manage hosts file on Linux, let's first understand-What is a Hosts File ?

Host files are used to map IP addresses to domain names on a local system. Editing and managing host files on Linux can be useful for various purposes, such as blocking unwanted websites, redirecting domains, or creating local development environments.

Your computer contains a single file that acts as a small gateway between you and the internet. The hosts file is the name of it. Simply add or modify a few lines in the file if you need to block websites or make custom web shortcuts on Linux.

This tutorial will provide instructions on how to edit and manage host files on Linux. We will also address a few FAQs on how to modify and manage hosts file on Linux.

Location of the Linux Hosts File

You can find the hosts file under /etc/hosts on Linux. You can open the hosts file using your preferred text editor because it is a plain text file.

To save changes to the hosts file, you'll need administrative permissions because it's a system file. You'll need superuser access to modify the file using a Linux terminal-based text editor like nano.

For example:

sudo nano /etc/hosts

An example of a graphical text editor is gedit:

gksu gedit /etc/hosts

Exit the editor after you've finished modifying the file. Ctrl + X and Y to confirm overwriting the modifications in nano. Before editing the file, it's a good idea to save a backup copy, so you can restore it if something goes wrong because it might muck up your network access.

Simply create a copy of the hosts file to create a backup of it. To help you remember that this is an old copy of the file, you may append a suffix like .old:

sudo cp /etc/hosts /etc/hosts.old

How to Add Sites to the Hosts File

Each entry in the hosts file has its own line. Simple syntax is used. Press the Tab key on your keyboard, type the IP address to which the hostname should be translated, and then type the hostname.

If you wanted to ban Wikipedia, for instance, you would type (remembering to press Tab rather than Space):

127.0.0.1        wikipedia.org

The loop back IP address, which always points back to your own system, is 127.0.0.1. Your browser will report that the site cannot be found because the web is not stored on your computer. It has now been successfully blocked.

Make Shortcuts in the Hosts File

The hosts file is also helpful for giving computers on a small office or home network names that are simple to remember.

You can enter the following in your hosts file if a computer on your home network (let's say with an IP address of 192.168.1.10) has a straightforward website or file server that performs a helpful task for you:

192.168.1.10        homeserver

If you then launch your browser and just type:

http://homeserver

Your computer will now be automatically redirected to 192.168.1.10. Compared to needing to hunt up an IP address, it is a lot simpler. Using the configuration menu on your Wi-Fi router, you may permanently assign an IP address to any computer on your network.

As an alternative, you can establish shortcuts to certain web pages using the hosts file. To add a website's IP address to your hosts file with the desired shortcut, use a tool like nslookup to determine the address of the website. This might not function on websites like Google or Netflix because the majority of large websites have numerous IP addresses.

Issues That Could Arise With the Hosts File

We now know how to modify the host file, however there may still be problems if you use Google Chrome. The hosts file is typically ignored by this web browser unless you do one of two possible actions:

  1. At the start of each address, type http://. For instance, Chrome will get around a block if you just write wikipedia.org into the address bar if Wikipedia is blocked in your hosts file. The hosts file will be used, nevertheless, if you type http://wikipedia.org into the address bar.
  2. You won't have to input http:// at the beginning of every URL if you disable Chrome's "Use a web service to help resolve navigation errors" option.

How to Edit the Hosts File?

The hosts file provides a simple method for creating names for any home servers that are simple to remember, as well as blocking access to particular websites on your computer.

As long as your children don't have superuser access, it's a simple but efficient technique to block websites you might not want them to see or restrict screen time if you have children. On Linux, you can restrict screen time and internet access using additional tools.

FAQs to Edit and Manage Hosts File on Linux

Where is the host file located on Linux? 

The host file is typically located at /etc/hosts.

Can I edit the host file without root privileges? 

No, editing the host file requires root privileges as it is a system-level file. Use sudo with a text editor to edit the file.

How do I add an entry to the host file? 

Open the host file, navigate to an empty line, and add the IP address followed by the domain name. For example: 127.0.0.1 example.com

Can I use comments in the host file?

Yes, you can add comments to the host file by prefixing a line with #. These lines will be ignored by the system.

How do I remove an entry from the host file? 

Open the host file, locate the entry you want to remove, and delete the corresponding line. Save the file once you've made the changes.

How do I apply changes in the host file without rebooting?

Changes to the host file take effect immediately, without the need to reboot. However, you may need to clear the DNS cache on your system or restart network-related services for the changes to take effect.

Can I automate host file management with scripts? 

Yes, you can automate host file management using shell scripts. These scripts can add, remove, or modify entries in the host file based on specific conditions or events.

Conclusion

You learned how to edit and manage the host file securely through this tutorial. If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.