How to Use and Edit the Hosts File in Linux

Spread the love

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:

cat /etc/hosts

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:

ip address    hostname other-hostname other-hostname

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

192.168.122.7   pihole.local

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:

192.168.122.202   linux-vm.local

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


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.

Comments (2)