Register now for better personalized quote!

How to set up Linux hosts file

Oct, 05, 2022 Hi-network.com
Oscar Wong/Moment/Getty Images

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.

See also

  • How to install Ubuntu Linux (It's easy!)
  • How to run a Windows app on Linux with Wine
  • How to get started with Git on Linux
  • How to kill a process in Linux

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 [email protected]

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.

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).

Also:These are the absolute best Linux distros for programming

3. Save and close

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.

Featured

We're not ready for the impact of generative AI on electionsThis is the$300 Android phone to beat in 2023 - and it even has a stylus5 things I learned while building my smart homeThe best laptops under$1,000: MacBook, Surface Pro, HP models compared
  • We're not ready for the impact of generative AI on elections
  • This is the$300 Android phone to beat in 2023 - and it even has a stylus
  • 5 things I learned while building my smart home
  • The best laptops under$1,000: MacBook, Surface Pro, HP models compared

tag-icon Hot Tags : Services & Software Operating Systems LINUX

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.