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:
ssh jack@192.168.1.100
You could map that IP address in the hosts file such that all you’d have to type is:
ssh jack@invoiceplane
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.
Also: The 5 best Linux distros for beginners: You can do this
How to set up Linux hosts file
How do you set this up? It’s very easy. Let me show you how.
Requirements
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.
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.
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).
Also: These are the absolute best Linux distros for programming
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.
Also: 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 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.
Source: Networking - zdnet.com