What is localhost?

Computer machines are identified by their hostname [name of the host machine] and IP address.

The below diagram briefly illustrates it.

Likewise, we also have a common name for all the computer systems that used to call itself is known as localhost. Here, the term “localhost” is associated with the context of computer networking. It plays a vital role during our journey as a developer or system administrator. There are many use cases for the “localhost,” such as application testing, documentation, network performance testing, and site blocking.

What is Localhost?

Localhost is a hostname that refers to the computer system on which the calling program is running, which means the machine will talk to itself when we call localhost.

It helps us to check the network services in the machine, even during network hardware failures. When using “localhost” the network services are accessed through the logical network interface called loopback. The IP address of the loopback interface is 127.0.0.1. Thus, the localhost resolves to 127.0.0.1 as part of the name resolution.

What is a Loopback Address?

Loopback is a logical network interface present in all operating systems. The packets transmitted through this interface are returned (looped) back to the same interface in the same machine. Hence, the interface is called a loopback.
According to the IETF Standards for IPv4 addressing, the entire block of 127.0.0.0/8 is allocated for network loopback purposes. As the default behavior, a loopback interface gets configured after every server installation.

Let’s have a look at the below snippet.

ip a show lo
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 76238871 bytes 6955286874 (6.9 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 76238871 bytes 6955286874 (6.9 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
sudo cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 sandbox1

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback

Loopback Packet Processing Internals

Generally, Loopback packets are distinguished from the other IP packets by their addresses. The processing of loopback packets with the loopback address happens at the link layer of the TCP/IP Stack. This traffic will be passing inside the computer systems itself. It will not hit the Hardware NIC Cards like other IP packets. Additionally, there exists a rule that the routers should not route the loopback IP address.

For instance, when we request for 127.0.0.1 address. The request will not forward to the internet because of the first octet (127). Here, the TCP/IP stack recognizes the request and routes it back to the same machine.

A quick illustration of the packet flow walkthrough for localhost and other IP packets is below.

ping -c 4 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from liviu.test (127.0.0.1): icmp_seq=1 ttl=64 time=0.101 ms
64 bytes from liviu.test (127.0.0.1): icmp_seq=2 ttl=64 time=0.091 ms
64 bytes from liviu.test (127.0.0.1): icmp_seq=3 ttl=64 time=0.076 ms
64 bytes from liviu.test (127.0.0.1): icmp_seq=4 ttl=64 time=0.069 ms

--- localhost ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.069/0.084/0.101/0.014 ms
ping -c 4 nexonhost.com
PING nexonhost.com (89.45.12.91) 56(84) bytes of data.
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=1 ttl=63 time=0.565 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=2 ttl=63 time=0.589 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=3 ttl=63 time=0.609 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=4 ttl=63 time=0.633 ms

--- nexonhost.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.565/0.599/0.633/0.025 ms

Application Testing Using Localhost

The accessibility of the application service first happens through the network interface. If the application service is mapped with a physical interface, it can be accessible from the outside world. Likewise, if the application is mapped with a logical loopback interface, it can be accessed only from that specific computer system and cannot be accessible from the outside world.

It is always easy to develop and test the code from the local server than from another remote host. To achieve that, we map our production domain name with a new loopback address (127.0.1.100) in the /etc/hosts file. Entries in the /etc/hosts have precedence over DNS.

ping -c 4 nexonhost.com
PING nexonhost.com (89.45.12.91) 56(84) bytes of data.
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=1 ttl=63 time=0.640 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=2 ttl=63 time=0.757 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=3 ttl=63 time=0.634 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=4 ttl=63 time=0.668 ms

--- nexonhost.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.634/0.674/0.757/0.058 ms
sudo cat /etc/hosts
127.0.0.1 liviu.test
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
ping -c 4 nexonhost.com
PING nexonhost.com (89.45.12.91) 56(84) bytes of data.
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=1 ttl=63 time=0.482 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=2 ttl=63 time=0.595 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=3 ttl=63 time=0.837 ms
64 bytes from 89.45.12.91 (89.45.12.91): icmp_seq=4 ttl=63 time=0.607 ms

--- nexonhost.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.482/0.630/0.837/0.130 ms

Conclusion

Localhost is the default name of the system where our program runs that helps us test applications and troubleshoot network issues. It is achieved using a local loop mechanism through the loopback network interface. It helps us testing software without network hardware configuration dependencies. As a computer user, it is essential to have a basic understanding of the terms localhost and loopback network interfaces.