Fundamentals 13 min read

Why Can You Ping 127.0.0.1 Even When Offline? Understanding Loopback Addresses

This article explains the nature of 127.0.0.1, localhost and 0.0.0.0, why pinging the loopback address works without a network connection, and how the operating system routes such traffic through a virtual network interface.

macrozheng
macrozheng
macrozheng
Why Can You Ping 127.0.0.1 Even When Offline? Understanding Loopback Addresses

What is 127.0.0.1?

127.0.0.1 is an IPv4 address. IPv4 addresses are 32 bits long, divided into four octets. Addresses that start with 127 belong to the loopback range, a special set of addresses defined by convention.

The address 127.0.0.1 is one of many loopback addresses; it is chosen simply because the source code defines it that way.

<code>#define INADDR_LOOPBACK 0x7f000001 /* 127.0.0.1 */</code>

Why can you ping 127.0.0.1 when the network cable is unplugged?

When you run

ping 127.0.0.1

, the packet is processed entirely within the kernel. The routing table recognizes the destination as a loopback address and selects the "local" (virtual) network interface instead of a physical NIC.

The virtual interface (often called

lo0

) does not use a real ring buffer; it places the packet into a shared input queue (

input_pkt_queue

). A kernel thread named

ksoftirqd

handles the soft‑interrupt, retrieves the packet from the queue, and delivers it back to the application without ever leaving the host.

Ping vs. TCP data transmission

Both ping and TCP create a socket, but ping uses a raw socket (

socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)

) that works at the network layer, while TCP uses a stream socket (

socket(AF_INET, SOCK_STREAM, 0)

) that works at the transport layer. The processing path is similar; the main difference is the protocol headers that are added.

Loopback address vs. local IP address

On macOS,

ifconfig

shows the loopback interface

lo0

with address 127.0.0.1 and a regular Ethernet interface (e.g.,

en0

) with an address such as 192.168.31.6. Pinging the local IP address actually goes through the same loopback interface, so there is no practical difference in the packet flow.

127.0.0.1, localhost and 0.0.0.0

localhost

is a hostname that resolves to 127.0.0.1 via the

/etc/hosts

file, so using either name yields the same result.

0.0.0.0

is not a usable destination; it represents an invalid address in IPv4. However, when a server binds to 0.0.0.0 (INADDR_ANY), it listens on all local IPv4 addresses, allowing connections via 127.0.0.1, the machine’s external IP, or any other assigned address.

<code>#define INADDR_ANY ((unsigned long int) 0x00000000) /* 0.0.0.0 */</code>

Summary

127.0.0.1

is a loopback address;

localhost

is a hostname that resolves to it.

Pinging the loopback address or the machine’s own IP uses the virtual

lo0

interface, never leaving the host, which is why it works even when the network is down.

Binding a server to

0.0.0.0

makes it listen on all local IPv4 addresses, but clients must connect to a specific address, not 0.0.0.0.

IPv6pingnetworkingIPv4loopbackIP address
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.