How to configure the hosts file in Linux


Linux is a very powerful operating system which is also one of the most flexible on the market. This flexibility has always been one of the main selling points of the operating system. Any user who doesn’t like how Linux works can always modify it to better suit their needs.

A very simple example to illustrate this is the Linux hosts file.

What is the hosts file?

The hosts file makes it easy to match IP addresses to domain names. Does this remind you of something ? This is exactly what DNS does today. But back in the days of ARPANET, there was no DNS, so you had to find a way to map those addresses. This is where the hosts file came in. Since DNS now takes care of this for us, why does the hosts file still exist you ask me?

The best example I can give you is of a LAN-based setup where you want to connect to machines without having to type in their full IP addresses. You may have a server on your local network that you access regularly using the command:

You can map this IP address in the hosts file so you just have to type:

This avoids having to remember the server’s IP address each time. This is very useful when you have to work with a large number of servers on your local network. You can even work with these nicknames in your web browser. For example, if you have installed Invoiceplan (which is an invoicing solution) on a server in your local network and the root of the service is /var/www/html/invoiceplane, you can associate the IP address of the server with pseudonym invoice and simply type invoice/invoiceplane in the address bar of your browser to open the service.

How to configure the Linux hosts file

How to configure this file? It is very simple.

Prerequisites

The only things you will need are a working Linux distribution and some IP addresses to map.

1. Open the hosts file

Log in to your Linux machine and open a terminal. To open the hosts file for editing, type the following command:

2. Add a new entry to hosts

Each line in the hosts file is a mapping for a single IP address and looks like this:

  • IP address Pseudonym Domain name

For example (staying with our Invoiceplane example), you can map the IP address 192.168.1.11 to the nickname invoice with the domain invoiceplane.lan. This entry would look like this (and would be added to the bottom of the file):

  • 192.168.1.11 invoice invoiceplane.lan

Once this entry is saved in the hosts file, you can reach this server in three different ways:

  • via IP address (ssh 192.168.1.11)
  • via his pseudonym (ssh invoice)
  • via its domain name (ssh invoiceplane.lan)

The only caveat to domain name registration is that you will not be able to reach this server using this entry in a web browser (only IP addresses and pseudonyms are accepted).

3. Save and Close

Once you have added the necessary entries, save and close the file with the key combination CTRL + X. Once the file is saved, you do not need to reboot the operating system, as the networking subsystem will immediately pick up the changes.

For those new to Linux, you probably won’t need to worry about the hosts file at first. However, the more you delve into using Linux, the more situations you will encounter where the hosts file will be useful to you.

Source: ZDNet.com





Source link -97