How to set up Linux hosts file

A pregnant woman uses a laptop in a living room.

Oscar Wong/Moment/Getty Images

Linux is a very powerful operating system that is also one of the most flexible on the market. This flexibility has always been a big selling point of the operating system. Any user who doesn’t like the way Linux works can still bend it to better suit their needs.

A very simple example of this is the Linux hosts file. Understand that the hosts file is not unique to Linux. Windows and MacOS also use the hosts file. In fact, MacOS’ hosts file is almost identical to Linux’s.

What is the hosts file?

The hosts file makes it easy to map IP addresses to domain names. Sound familiar? It should, because that’s what DNS is doing today. But back when ARPANET existed, DNS didn’t exist, so there had to be a way to map those addresses. This is where the hosts file comes into play. Given that DNS does that for us now, why do you need the hosts file at all?

The best example I can give of this is a LAN based network setup where you might have servers or desktops that you want to reach without having to enter a full IP address. You may have a server on your LAN that you access regularly:

ssh [email protected]

You could map this IP address in the hosts file by just typing:

ssh [email protected]

It might not be less typing, but it certainly means you don’t have to remember that server’s IP address. Instead, use a nickname. This can be very useful when you need to work with a large number of servers on your LAN. You can even work with these nicknames in your web browser. For example, if you have Invoiceplan (an invoicing solution) installed on a server on your LAN, and the document root of that service is /var/www/html/invoiceplane, you can map the server’s IP address to the nickname invoice and type bill/invoiceplane in your browser’s address bar to open the service.

Likewise: The 5 Best Linux Distros for Beginners: What You Can Do

How to set up the Linux hosts file

How do you set this? It’s very easy. Let me show you how.


The only things required for this are a running instance of Linux and some IP addresses to allocate. That’s it, let’s whip up some hosts.

Login to your Linux machine and open a terminal window. To open the hosts file for editing, enter the command sudo nano /ets/hosts.

Each line in the hosts file is a mapping for a unique IP address and has the following format:

IP address nickname domain name

For example (to stay with our Invoiceplane example) you could map the IP address to the nickname “Invoice” with a domain “Invoiceplane.lan”. This entry would look like this (and be added to the end of the file): invoice invoicelevel.lan

If this entry is stored in the hosts file, you can reach this server in three ways:

  • IP address (e.g. ssh
  • Nickname (e.g. ssh-invoice)
  • Domain name (e.g. ssh invoiceplane.lan)

The only limitation of entering the domain name is that you cannot reach the server with this entry in a web browser (where you are limited to IP addresses and nicknames).

Likewise: These are the absolute best Linux distributions for programming

After adding the required entries to hosts, save and close it using the Ctrl+X shortcut. When the file is saved, you don’t have to worry about restarting the operating system because the network subsystem applies the changes immediately.

Likewise: Cool things you can do with the Linux desktop that you can’t do with macOS or Windows

For those new to Linux, you probably don’t need to worry about the hosts file at first. However, the deeper you delve into using Linux, the more likely you will come across numerous situations where the hosts file is very necessary.

Read  How to create a bibliography in Word

Related Articles

Leave a Reply

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

Check Also
Back to top button