How to Use and Edit the Hosts File in Linux

Edit Hosts File Linux Featured

If you’ve ever managed multiple servers from a Linux machine or set up some kind of home lab, then you know how useful the “hosts” file can be in Linux. However, some of you might not even know it exists, much less how to leverage it to make your life easier. That’s why we are guiding you through how to use and edit the hosts file in Linux.

What Is the Hosts file?

The “/etc/hosts” file is where your system keeps an internal map of IP Addresses -> hostnames. This doesn’t require a DNS server because it’s stored locally on your machine. This is a great way to manage things like internal servers or other devices on your network that commonly require you to enter an IP Address. I use this all the time for Raspberry Pi projects and managing commonly-used KVM Virtual Machines.

How Do I Use the Hosts File?

First, take a look at what’s already in the hosts file. Type the following command into the terminal and press enter:

In the output, you’ll find two IP addresses that are meant for your local machine to identify itself. If you run the ping localhost command, you’ll see one of these IP addresses come up. That’s because Linux automatically generates these two entries into the hosts file. If you want to add more entries, all you have to do is follow the formatting of the file, and you’ll be all set to go.

Editing the Hosts File

To edit the hosts file, just open it in your preferred text editor. You can use Vim, Nano, Gedit, Kate, or whichever you like best. You should see the content in the following format:

So if I have a Pi Hole server with an IP address of 192.168.122.7, my format would be something like this:

It’s, of course, dependent on what you want to name it. That means that any time you want to address that Pi Hole server, whether it’s via ping, ssh, or some other tool, you don’t have to enter the IP address. You can just type pihole.local into the terminal. It’s quite the timesaver, especially if you get to shorter server hostnames.

It’s also important to note that you don’t have to use “.com,” “.net,” or “.org” addresses. You can even just call it pihole, and your system will recognize it. I used a .local address to show you the “proper” way to do it, but you can call them server if it’s your only one.

It’s also useful for virtual machines on a virtualization server or workstation. Remembering VM IP addresses is a huge pain, and using the hosts file is perfect to manage that. With my VM IP Address, 192.168.122.202, I’ll take that and edit my hosts file with this:

Then I can ping that VM with the hostname, which makes it much easier to remember what I’m looking for. You can see that rather than an IP address, I’m pinging the hostname, and that shows up in the output of the command. The system has an internal map of that IP and hostname, so it’s easy to slot into that spot.

Wrapping Up

As you can see, learning how to edit the Hosts file in Linux can be a useful skill, as it allows you to save you a lot of time later down the line. If you are on Windows, you can also edit the Hosts file and use it exactly the same way as stated above. If you are looking for a simple way to block websites, then these Chrome extensions may do a better job.

Related:

John Perkins John Perkins

John is a young technical professional with a passion for educating users on the best ways to use their technology. He holds technical certifications covering topics ranging from computer hardware to cybersecurity to Linux system administration.

2 comments

  1. You did not mention a very important use of the HOSTS file and that is blocking ads, trackers and other sketchy sites. There are, in fact, pre-formatted HOSTS files available for download so that one does not have to manually enter thousands of sketchy IP addresses.

    I’ve been using HOSTS in such a way for at least 20 years on both Windows and Linux systems. It is my first line of defense.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.