Get to know the Linux hosts file and how to use it

If you’re new to Linux, there’s a handy file you’ll want to check out right away. Jack Wallen walks you through the hosts file and how to add entries to it.

Image: Christian-P. Worring/Adobe Stock

Regularly I refer to the hosts file in Linux and assume everyone knows what it is. But every once in a while I get a question that reminds me that some are new to Linux and need help with the basics.

TO SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

The hosts file is a great place to start because not only is it very useful, but it can also help new Linux users get to grips with a few important concepts. With that in mind, I’ll introduce you to the hosts file and walk you through each step of editing it.

Are you ready for this? Let’s do it.

What you will need

To work with the hosts file, you will need a running instance of Linux (regardless of distribution) and a user with sudo privileges.

What is the hosts file in Linux?

Simply put, the hosts file is a way to map hostnames to IP addresses. This is very important with some setups and to make Linux networking a bit easier. In a sense, the hosts file acts like a local DNS server.

Suppose you have deployed the InvoicePlane invoice management system on a Linux machine within your local network. The IP address of the machine hosting InvoicePlane is 192.168.1.11, and to access the service, users must type 192.168.1.11/invoiceplane. Now, how about if you could map that IP address to a more standard address (say www.invoiceplane.net), so your users don’t have to type in the IP address (which most people have trouble remembering)?

Or, let’s say you are deploying a Kubernetes cluster. Part of the process of deploying such a cluster is to map the controller and nodes in the hosts file.

Let’s dig into the hosts file to see how it works.

How to Open Hosts File for Editing in Linux

The first thing to do is to open the hosts file to modify it. We’ll do this from the command line, so log in to your Linux server or open a terminal window on your Linux desktop. We will use the nano editor (because it is the easiest tool for this purpose). Because the hosts file lives in the /etc/ directory it is protected, so we need to elevate our user’s privilege with the sudo ordered. To open the file for editing, run the command:

sudo nano /etc/hosts

You will be prompted to enter your user password. After successfully typing your user password, you will see the hosts file open and ready to edit (Figure A).

Figure A

This is an example of a hosts file on one of my Linux desktop machines.
This is an example of a hosts file on one of my Linux desktop machines.

The first entries in the hosts file are always dedicated to localhost, which is the local machine. In my example, localhost is represented in two different ways:

127.0.0.1 hive

::1 localhost

127.0.0.1 is called localhost or loopback address and is used on all computers (Linux, macOS and Windows). The first line (127.0.0.1 localhost) maps the loopback address to the localhost name. For this reason you can ping the local machine with either ping 127.0.0.1 or ping localhost. Since we mapped the address into the hosts, these two commands would ping the local machine. And since we mapped the IPv6::1 address to localhost, the same is true.

Now let’s create a new entry. We will use one of the examples mentioned above. What we are going to do is map the IP address 192.168.1.11 with the hostname billplane and the FQDN www.invoiceplane.net. It should be noted that I don’t own the invoiceplane.net domain, and it doesn’t solve anything. You can use invoiceplane.lan, but it has problems with web browsers.

At the bottom of the hosts file, add the following line (modifying an IP address/server you have on your local network):

192.168.1.11 invoiceplane www.invoiceplane.net

The finished file will look like this in Figure B.

Figure B

Our updated hosts file now includes the entry for the billing plan.
Our updated hosts file now includes the entry for the billing plan.

To save and close the file, press the key combination Ctrl+X and answer y to save.

Let’s test our changes. From the terminal, ping the server with the following (modifying an IP/server you have on your local network):

ping 192.168.1.11

ping invoiceplane

ping www.invoiceplane.net

The results should be the same and your local machine should be able to reach the remote machine on your local network no matter which address you use.

You can also open your web browser and instead of typing the IP address, type the mapped address www.invoiceplane.net (change depending on what you added to the hosts file). In my case, I could enter www.invoiceplane.net/invoiceplane (because my instance of InvoicePlane is served from the invoiceplane directory in the apache document root) in the address bar of my browser and reach my instance of InvoicePlane. One thing to note is that once you hit enter on your keyboard, you’ll likely see the address automatically change from www.invoiceplane.net/invoiceplane at 192.168.1.11/invoiceplane. This is expected behavior. The only point of the hosts file is to map host/domain names to IP addresses. Once the address is resolved, it will change in the address bar.

You can add as many address mappings as needed to the Linux hosts file. As long as you stick to the formula, you should be fine.

And that’s how you work with the Linux hosts file. Happy mapping!

Subscribe to TechRepublic How to make technology work on YouTube for all the latest tech tips for professionals from Jack Wallen.