Why you can trust ZDNET
:ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission.Our process
'ZDNET Recommends': What exactly does it mean?
ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.
When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.
ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.
Linux is a very powerful operating system that is also one of the most flexible in the marketplace. That flexibility has always been a big selling point of the OS. Any user that doesn't like the way Linux works can always bend it to better fit their needs.
One very simple example of that is the Linux hosts file. Do understand that the hosts file isn't unique just to Linux. Windows and MacOS also make use of the hosts file. In fact, the MacOS hosts file is almost identical to that of Linux.
What is the hosts file?
The hosts file makes it easy to map IP addresses to domain names. Sound familiar? It should, as that's exactly what DNS does today. But back in the day of ARPANET, there was no DNS, so there needed to be a means to map those addresses. That's where the hosts file comes into play. Given that DNS now takes care of that for us, why would you even need the hosts file?
The best example I can give of that is for a LAN-based network setup, where you might have servers or desktops you want to be able to reach without having to type a full IP address. You might have a server on your LAN that you regularly access with:
You could map that IP address in the hosts file such that all you'd have to type is:
It may not be less typing, but it certainly means you don't have to remember the IP address of that server. Instead, you use a nickname. This can be very helpful when you have a large number of servers to work with on your LAN. You can even work with those nicknames in your web browser. For instance, if you've installed Invoiceplan (which is an invoicing solution) on a server within your LAN and the document root of that service is /var/www/html/invoiceplane, you could map the IP address of the server to the nickname invoice and type invoice/invoiceplane in your browser's address bar to open the service.
How do you set this up? It's very easy. Let me show you how.
The only things this will require are a running instance of Linux and some IP addresses to map. That's it, let's make some hosts magic.
1. Open the hosts file
Log in to your Linux machine and open a terminal window. To open the hosts file for editing, type the command sudo nano /ets/hosts.
2. Add a new entry to hosts
Each line in the hosts file is a mapping for a unique IP address and is laid out in the form:
IP Address Nickname Domain Name
For instance (sticking with our Invoiceplane example), you could map the IP address 192.168.1.11 to the nickname invoice with a domain of invoiceplane.lan. That entry would look like this (and be added to the bottom of the file):
192.168.1.11 invoice invoiceplane.lan
With that entry saved to the hosts file, you could reach that server via three ways:
IP address (such as ssh 192.168.1.11)
Nickname (such as ssh invoice)
Domain name (such as ssh invoiceplane.lan)
The one caveat to the domain name entry is that you won't be able to reach the server using that entry in a web browser (where you're limited to IP addresses and nicknames).
Once you've added the necessary entries to hosts, save and close it with the Ctrl+X key combination. With the file saved, you don't have to bother with restarting the OS, as the networking subsystem will pick up on the changes immediately.
For those new to Linux, you probably won't need to bother with the hosts file at first. Eventually, however, the deeper you dive into using Linux, you will most likely run into numerous situations where the hosts file is very much necessary.