Home Learn Linux Your ultimate guide to the /etc/hosts file in Linux

Your ultimate guide to the /etc/hosts file in Linux

We'll dive deep into the /etc/hosts file in Linux. We'll explain its purpose, how it's structured, and how you can edit it to suit your needs. Ready to master this key part of Linux networking? Let's get started!

by Arun Kumar
a guide to the etc hosts file in linux

I am delighted to be back again to dive into another intriguing topic about Linux – the /etc/hosts file. This tiny yet significant file has seen me through many tough times, and truth be told, it’s become something of an old friend. And, like any old friend, it can occasionally be a tad stubborn, but it’s the understanding that matters!

What’s the /etc/hosts File?

To start, we must first understand what the /etc/hosts file is. In the simplest of terms, it’s a plain-text file in a Linux system that maps hostnames to IP addresses. In the olden days before the advent of DNS (Domain Name System), the /etc/hosts file was the primary method of host-to-IP mapping. It’s like a phonebook for your computer – remember those?

The hostname to IP mapping in the /etc/hosts file lets you access a system or a service with a friendly name rather than a numeric IP address. This function continues to prove useful, despite the dominance of DNS, in a range of scenarios such as web development, software testing, network configuration, and more.

Structure of /etc/hosts File

Here’s the part where things get a bit technical, but hang in there with me. The structure of the /etc/hosts file is straightforward. Each line of the file represents one IP entry, consisting of an IP address, followed by at least one hostname, separated by white space (either a space or a tab).

127.0.0.1 localhost
192.168.1.5 example.com

In the example above, localhost is a traditional hostname that points to your own system. The IP 127.0.0.1 is known as the loopback address. 192.168.1.5, on the other hand, is an example of an IP mapped to the domain example.com.

Practical Example

Let’s take a hypothetical (and slightly amusing) scenario. Suppose I dislike typing 192.168.1.5 every time I want to access my favorite local website on my Linux machine, and I’d rather type fave-site instead. In my /etc/hosts file, I’d simply add:

192.168.1.5 fave-site

And voila! Now, instead of typing the cumbersome IP address, I just enter fave-site in my web browser, and I’m good to go!

Editing the /etc/hosts File

To edit this magical file, we will need to use a text editor, and my personal favorite is nano. Here’s how you can do it:

sudo nano /etc/hosts
opening hosts file using nano text editor on pop! os

Opening hosts file using nano text editor on Pop!_OS

You’ll need root permissions, so make sure to prefix your command with sudo. Once you’re done with your changes, press Ctrl+X to exit, and say Y to save the changes.

5 Real-World /etc/hosts editing scenarios in an organization

Local Development & Testing: In software development, we often need to mimic production environment on our local machines. Here, /etc/hosts comes in handy as it can map domain names of the real services to localhost or a staging server. This allows us to test applications safely without affecting live servers.

Blocking Websites: It might sound archaic, but sometimes the simplest solutions are the most effective. If an organization needs to prevent access to specific websites, they can use the /etc/hosts file. By redirecting the website’s domain name to localhost or 0.0.0.0, the site becomes unreachable.

Creating Server Aliases: A single server can have multiple names (aliases) using the /etc/hosts file. This is useful in cases where the same server may need to be referred to by different names by different applications or services.

DNS Re-routing: In situations where the DNS server may not be working correctly, or during a DNS migration, entries can be added to the /etc/hosts file to ensure uninterrupted service. This is more of a quick fix and not a permanent solution, but it demonstrates the utility of the hosts file.

Network Traffic Optimization: In distributed systems within an organization, there might be a need to route traffic through specific paths for optimization or testing purposes. By specifying the IP addresses in the /etc/hosts file, one can control the path of network traffic.

While these are some practical examples, the usage of the /etc/hosts file is not limited to these scenarios alone. Its simplicity and direct approach make it a reliable tool in many other situations as well. However, be aware of the potential for complications if the file is not managed properly. Always ensure the changes are valid and necessary, and don’t forget to make a backup before making changes!

Troubleshooting Tips

Like any Linux feature, the /etc/hosts file can occasionally act up. Here are some common issues and how to tackle them:

Changes not reflecting: If your changes aren’t showing up, try clearing your DNS cache. The method depends on your Linux distribution and the DNS caching system installed. For systemd-resolved, you would use:

sudo systemd-resolve --flush-caches

Permission denied: This likely means you’re not editing the file as a root user. Remember to use sudo when trying to edit /etc/hosts.

Unable to resolve a hostname: Check for typos in your /etc/hosts file. If everything seems correct, try pinging the IP address directly. If the IP address works but the hostname doesn’t, your issue likely lies with the /etc/hosts file. For example, here is the ping command usage to verify 192.168.1.88. You can see the bytes received from the server, so we are good from the server’s point of view.

ping command usage

ping command usage

Pro Tips

Backup, backup, backup! Before making any changes to the /etc/hosts file, I highly recommend making a backup. It’s saved me countless times from unforeseen errors. A simple command like sudo cp /etc/hosts /etc/hosts.bak can save your day.

backing up hosts file

Backing up hosts file

Recovering your /etc/hosts file from a backup is a straightforward process in Linux. Let’s say you’ve followed my advice and created a backup of your hosts file named hosts.bak before making changes. Now, for some reason, you wish to revert to this backup. Here’s how you would do it:

Firstly, it’s always a good idea to check the contents of your backup file before replacing the existing hosts file. You can do this using the cat command:

cat /etc/hosts.bak
checking contents of hosts file using cat command

Checking contents of hosts file using cat command

If you’re satisfied that this is the version you want to restore, you can now replace the current /etc/hosts file with the backup. We’ll use the cp (copy) command for this, running as root with sudo because the /etc directory requires root permissions for changes:

sudo cp /etc/hosts.bak /etc/hosts
replacing hosts file from backup copy

Replacing hosts file from backup copy

That’s it! Your original /etc/hosts file has been replaced with the backup version. There will be no feedback in the terminal as shown in the above screenshot. Always ensure that you’re absolutely certain before replacing system files, as incorrect entries can potentially cause issues with system networking.

Keep in mind that if you’re running any applications that rely on the /etc/hosts file, you may need to restart them for the changes to take effect, as some applications may cache DNS lookups.

Commenting: You can add comments to your /etc/hosts file by prefixing the line with a #. This is super useful for noting down what each entry does, especially when you’re dealing with many entries.

Maintain format: Stick to the format – IP address followed by hostname(s). Don’t try to rearrange or add extraneous information. Keeping it clean and straightforward will save you a lot of headaches.

Conclusion

To conclude, the /etc/hosts file is a powerful tool that continues to hold relevance in the Linux world. It’s like that old tool in the shed – you might not use it every day, but when you need it, there’s nothing quite like it. So, here’s to the unsung hero of our Linux systems, the /etc/hosts file! And remember – with great power, comes great responsibility.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.