1. Overview

Network interfaces are endpoints that connect the Linux machine to external networks. The operating system sends and receives network traffic through these interfaces.

In this tutorial, we’ll learn about one special type of interface that exists on almost every Linux machine – the loopback (lo) interface.

2. What Is a Loopback Interface?

In contrast to regular network interfaces, which represent physical hardware devices, a loopback interface is a virtual interface. Therefore, it doesn’t represent any hardware devices and has no physical cables or connectivity to other devices.

It’s only part of the internal TCP/IP stack of the operating system. The machine can use this interface to communicate with itself, and it’s very useful in troubleshooting and diagnostics.

The loopback interface uses an IP address like any other network interface. There’s a reserved IPv4 address range 127.0.0.0/8 for loopback interfaces. The loopback interface usually uses the first address 127.0.0.1 in this range.

So let’s check the details of this interface using the ifconfig command:

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 192.33.136.6  netmask 255.255.255.0  broadcast 192.33.136.255
        ether 02:42:c0:21:88:06  txqueuelen 0  (Ethernet)
        RX packets 362  bytes 54226 (54.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 335  bytes 157764 (157.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 14  bytes 1230 (1.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1230 (1.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

There are two network interfaces here. The eth0 is a physical ethernet interface, and the lo is our loopback interface. We can see that the lo interface has an IP address of 127.0.0.1, with a netmask 255.0.0.0, which is equivalent to /8.

There’s also a hostname localhost assigned to this IP address by default. We can verify this by checking the hosts file:

$ cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.33.136.6    ubuntu-host

Here we can see a mapping between the localhost hostname and the loopback IP address 127.0.0.1.

3. The Purpose of the Loopback Interface

When a physical network interface is disconnected for any reason, like a hardware failure or being physically detached from the network, the machine can’t use it for any communication, even with itself.

However, because the loopback interface is only an internal virtual interface with no physical hardware, the machine can use it anytime to communicate with itself.

This can help in troubleshooting scenarios where we want to isolate and identify if a network issue is caused by an internal problem inside the machine network stack itself.

For example, we can try to ping the localhost and verify if there’s a reply or not. If there’s a reply, it means that the TCP/IP stack is working fine and able to process packets:

$ ping 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.019 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.021 ms

--- localhost ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4089ms
rtt min/avg/max/mdev = 0.019/0.024/0.037/0.006 ms

Here we verified that our network stack has no issues because we got a reply, and the traffic actually never left the machine.

The loopback interface can also be used by services and applications to communicate with each other on the same machine. For example, we can test a web server running on our machine by connecting from a browser to the loopback IP address and the web server port. This gives us the same experience that a normal user gets when connecting from their browser.

Let’s now do an easier example, which is connecting through SSH to our local machine:

$ ssh root@localhost
root@localhost's password: 
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.4.0-1106-gcp x86_64)
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Here we connected from our machine through SSH to the same machine. Therefore, we used the localhost as our destination. This didn’t require any external network or other machines, only local connectivity on the same machine through the loopback interface.

4. Conclusion

In this article, we discussed the main concepts and uses of the loopback interface. We saw that a loopback interface is a virtual network interface that doesn’t represent any physical or hardware device. It’s part of the OS network stack and uses an IP address similar to other network interfaces.

We can use the loopback interface in troubleshooting to verify that the TCP/IP stack of the machine is working fine. Additionally, applications can also use the loopback interface to communicate with each other on the same machine.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.