How to Add Static Entries to the Hosts File on Linux

Learn how to add static entries to the Linux hosts file to achieve precise control over domain mapping. Quick and simple guide!

Are you looking to take control of your domain name resolution? You’ve come to the right place! In this guide, we’ll walk you through the simple steps of adding static entries to the hosts file on Linux.

Mastering the manipulation of the hosts file is a valuable skill. This seemingly small file holds the power to redirect network traffic, block unwanted sites, or even create shortcuts to your favorite servers. But before we go any further, we need to introduce some theory.

What Is the Linux Hosts File?

The hosts file is a plain text file that almost every operating system, including all flavors of Linux, uses to map hostnames (like example.com) to IP addresses. This file serves as a local override for DNS lookups.

The "/etc/hosts" file on Linux.
The “/etc/hosts” file on Linux.

On Linux systems, the hosts file is typically located at “/etc/hosts“. This path makes it easy for system administrators and users to find and edit it when necessary. Accessing it requires root privileges, ensuring only authorized users can make changes.

How Does the Hosts File Work?

When you enter a URL into your browser, try connecting to an SSH remote server using a hostname, etc., your system uses the Domain Name System (DNS) to translate the web address or hostname into an IP address that computers can understand.

However, if an entry exists in the hosts file for that address, the system uses the specified IP address from the hosts file instead of performing a DNS lookup. This process speeds up address resolution and allows for custom domain mappings.

Linux hosts file

In simpler terms, when you try to visit a website, your computer will check the hosts file first to see if there’s a corresponding IP address there before asking the wider internet.

Top Use Cases

Adding static entries to your host’s file on Linux can be super handy for a few reasons: all about controlling how your computer finds and connects to websites or networked devices:

  • Testing and Development: Developers working on websites that are not yet live can map the domain name of their project to the IP address of a local or staging server. This allows you to see how the website will look on the real internet without it actually being there.
  • Override DNS information: In cases where DNS information is incorrect or has not propagated yet, adding static entries to your host’s file can temporarily override DNS settings to allow you to access the correct server.
  • Block Unwanted Sites: If you want to keep certain websites off-limits, redirect their domain names to a “dead end” like 0.0.0.0. This way, when you try to visit them, your browser just can’t find the way.
  • Speed Up Access: Your computer can find the website faster because it doesn’t have to ask for the address; it’s already written down in your local “/etc/hosts” file.

Add Static Entries to the Hosts File on Linux

To add entries to the hosts file, you need to edit it. This process requires administrative (sudo) privileges on Linux, as the hosts file is a system file protected from regular user changes.

Open the “/etc/hosts” file using a terminal text editor you’re comfortable with. Once the file is open, you’ll see existing entries. These typically include references to localhost.

sudo nano /etc/hosts

To add a new entry, navigate to the end of the file and enter the IP address, followed by at least one space or tab, and then the hostname you wish to map to that IP. For example, to map the “example.com” domain to the IP address “10.0.1.69,” the final version should look like this:

Mapping a domain name to the IP address in the "/etc/hosts" file on Linux.
Mapping a domain name to the IP address in the “/etc/hosts” file on Linux.

After adding your entries, save the changes and exit the editor. Now, it’s time to test your changes. You can ping the hostname you added to ensure your new mapping works. For example:

ping -c 3 example.comCode language: CSS (css)
Testing a local domain mapping.
Testing a local domain mapping.

If everything is set up correctly, the ping command should resolve the hostname to the IP address you specified in the “/etc/hosts” file, proving that your system is now using the local hosts file entry for this hostname.

Conclusion

Adding entries to the hosts file in Linux is straightforward and invaluable for testing purposes. Following the steps outlined above, you can add static entries to the hosts file, allowing for precise control over hostname resolution on your system.

Thanks for your time! For more in-depth information, consult hosts file manual page.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%

Leave a Reply

Your email address will not be published. Required fields are marked *