Jump to content

How to set up Linux hosts file


aum

Recommended Posts

Jack Wallen introduces you to the Linux hosts file and why it's important to know and customize.

 

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

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

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.

 

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

Edited by aum
Link to comment
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...