What is Localhost

Published on

5 min read

By 

Sriram Ramanujam
Localhost

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

The below diagram briefly illustrates it.

unique names

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.

Let’s dig more into the nitty-gritty details of it.

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.

packet flow walkthrough for localhost
ping -c 4 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.041 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.040 ms

--- localhost ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3075ms
rtt min/avg/max/mdev = 0.035/0.039/0.043/0.008 ms
ping -c 4 google.com
PING google.com (142.250.71.46) 56(84) bytes of data.
64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=1 ttl=120 time=2.14 ms
64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=2 ttl=120 time=2.18 ms
64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=3 ttl=120 time=2.19 ms
64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=4 ttl=120 time=2.20 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 2.147/2.180/2.203/0.051 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.

The below snippet showcases the organic ping response from the local server to the remote Linuxize web server. Domain mapping is shown in the second snippet. When we closely check the output after mapping the 127 segment IP to linuxize.com in the host’s file, traffic is routed to a loopback network interface.

ping -c 4 linuxize.com
PING linuxize.com (172.67.74.167) 56(84) bytes of data.
64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=1 ttl=59 time=34.5 ms
64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=2 ttl=59 time=34.5 ms
64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=3 ttl=59 time=34.5 ms
64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=4 ttl=59 time=34.5 ms

--- linuxize.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 34.521/34.529/34.541/0.227 ms
sudo cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 sandbox1
127.0.1.100 linuxize.com

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
ping -c 4 linuxize.com
PING linuxize.com (127.0.1.100) 56(84) bytes of data.
64 bytes from linuxize.com (127.0.1.100): icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from linuxize.com (127.0.1.100): icmp_seq=2 ttl=64 time=0.094 ms
64 bytes from linuxize.com (127.0.1.100): icmp_seq=3 ttl=64 time=0.042 ms
64 bytes from linuxize.com (127.0.1.100): icmp_seq=4 ttl=64 time=0.055 ms

--- linuxize.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3062ms
rtt min/avg/max/mdev = 0.042/0.066/0.094/0.020 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.

About the authors

Sriram Ramanujam

A seasoned Global Network Automation Architect with 10+ years of rich industry experience in developing scalable solutions for network automation, engineering design, operation and telemetry.