Internet Protocol or IP uses a 32-bit IPv4 or 128-bit IPv6 address for identifying a device on a network. This is analogous to our physical home address which is used for delivering our mails and online shopping items, for reaching to us, etc. Our physical home address is a mix of numbers and words, but in the case of computers, they only understand numeric addresses in a specific format. Even if we type an address like “www.domain.com”, computers will resolve it into a numerical format using the DNS mechanism.
An IP address is a private one if it is used only inside a local network. On the other hand, a public IP is used for accessing networks and devices outside a private LAN and over the internet. Three different ranges of IP addresses are assigned in a private block by the Internet Assigned Numbers Authority (IANA). RFC 1918 specifies these three ranges as follows:
172.16.0.0 - 172.31.255.255
192.168.0.0 – 192.168.255.255
The address block 127.0.0.0/8 is reserved for loopback or localhost addresses. It is basically an IP address range that is private at the host level. Any IP address inside this range cannot be used for any network. Let us explore in-depth- the concept of localhost IP or 127.0.0.1.
What is Localhost or 127.0.0.1?
Localhost refers to the name of a computer itself. Localhost redirects to the IP address 127.0.0.1, which is also called the loopback address. Actually, any IPv4 address in block 127.0.0.0/8 is designated as a loopback address. Applications usually prefer to use 127.0.0.1 for their networking service. The IP address 127.0.0.1 is normally assigned to the loopback interface. A loopback interface is technically a closed circuit. This means any TCP or UDP packet arriving at the loopback interface (localhost or 127.0.0.1) will be routed to the computer itself without going to the outside network or on the internet.
Localhost is basically used by many software programs like web servers, database servers, etc. to provide their services. In most cases where multiple applications are using the localhost address, they can be configured to use different port numbers. This can be usually done by modifying their configuration files.
Need of Localhost
The main purpose of using Localhost is to develop and test applications before deploying them to production environments. One of the popular examples is WordPress development. One can use the local environment to test new plugins and updates before deploying them to the real world. Some applications like Stringer RSS reader can be used only through Localhost. If you want to check if the Localhost is properly set on your computer, just install the Apache web server and navigate to the address http://localhost or http://127.0.0.1. This will bring the Apache homepage. If it is IPv6 localhost, you can access it by going to URL HTTP ://[::1]/ To access this web page on other devices on your LAN, you will need its private IP assigned by the DHCP server. Similarly, if you want to access this web page from a remote location, you will require a public IP address.
Besides 127.0.0.1, the localhost can also be mapped to other loopback IP addresses. For instance, you can still access the Apache start page with any address from 127.0.0.1 to 127.0.0.255 in the network 127.0.0.0/28.
The new IPv4 successor, IPv6 is a next-generation Internet Protocol (IP). It has a 128-bit IP address. Like a 32-bit IPv4 loopback address, IPv6 also specifies a 128-bit loopback address. The notation for an IPv6 localhost address is::1/128. Normally applications are configured to use both the IPv4 and IPv6 addresses. If you have previously installed Apache web server on your Linux server, you will notice that you can visit the Apache start page with the IPv6 localhost address. The choice of using IPv6 or IPv4 loopback IP may depend on your requirement. Maybe you want to develop your application to use only the IPv6 addresses, in that case, you can configure IPv6 localhost networking for it. Each has its own benefits over the other.
Name Resolution for Localhost
In almost every operating system, there exists a host file that resolves hostname to an IP address. This file also contains the name resolution for both the IPv4 and IPv6 localhost. Let us quickly look at the content of this file on an Ubuntu 20.04 OS. Open a terminal (ctrl+alt+t) and type:
The above command will display the contents of the host’s file on the terminal, as shown here:
From the above picture, we can see that label 1 corresponds to IPv4 localhost, and label 2 corresponds to IPv6 localhost.
In this guide, we have explored various features and uses of localhost or loopback IP. It is really a great boon for application development as it provides great flexibility and many possibilities.