Loopback
Loopback Address
A virtual network interface reserved for routing traffic back to the local machine — 127.0.0.1 in IPv4 and ::1 in IPv6 — used for local service testing without physical network involvement.
The loopback interface is a software-only network interface that exists on every computer. Packets sent to 127.0.0.1 (or any address in 127.0.0.0/8) never leave the machine — the OS intercepts them and delivers them back to the destination port on the same host. No physical network card, cable, or router is involved. This makes loopback ideal for testing network services locally and for inter-process communication on the same machine.
The loopback network interface
On Linux and macOS, the loopback interface is named lo and is visible in ip link show or ifconfig output alongside physical interfaces. On Windows, the loopback adapter is a virtual network adapter present in Device Manager. Unlike a physical NIC, the loopback interface is handled entirely within the kernel's networking stack — packets are short-circuited before they reach any driver or physical medium. This means loopback traffic does not generate any actual network frames, does not consume radio spectrum on Wi-Fi, and is not affected by physical network outages.
Loopback addresses
| Protocol | Loopback address | Range |
|---|---|---|
| IPv4 | 127.0.0.1 | 127.0.0.0/8 — entire /8 block reserved |
| IPv6 | ::1 | ::1/128 — single address only |
| Hostname | localhost | Resolves to 127.0.0.1 or ::1 |
The full 127.0.0.0/8 block
IPv4 reserves the entire 127.0.0.0/8 block — 16,777,216 addresses — for loopback. Any address from 127.0.0.1 to 127.255.255.254 routes back to the local machine. In practice, only 127.0.0.1 is conventionally used as the loopback address. The rest of the block is wasted address space from the era when classful addressing reserved a full Class A for this purpose. IPv6 took a more efficient approach, reserving only the single address ::1/128 for loopback.
localhost resolution
The hostname localhost resolves to the loopback address via two mechanisms. First, the operating system's hosts file (/etc/hosts on Linux/macOS, C:\Windows\System32\drivers\etc\hosts on Windows) contains a hardcoded entry mapping localhost to 127.0.0.1 and ::1. This lookup happens before DNS. Second, if the hosts file entry is removed, most OS resolvers fall back to DNS, where localhost should resolve to the loopback address per RFC 6761 — though not all DNS servers honour this. The practical caution: if an application connects to localhost and your system resolves it to ::1 (IPv6) but the service only listens on 127.0.0.1 (IPv4), the connection will fail. Using the explicit IP address avoids this ambiguity.
Use cases for loopback
- Local development servers — a developer runs a web app on
http://localhost:3000; the browser connects via loopback without network involvement - Database connections — applications connect to
127.0.0.1:5432(PostgreSQL) or127.0.0.1:3306(MySQL) for a local database instance - Inter-process communication via TCP — two processes on the same machine can communicate through a loopback TCP socket without using a Unix domain socket, which is useful for language-agnostic IPC
- Network stack testing —
ping 127.0.0.1verifies the OS TCP/IP stack is functioning without needing a live network connection - Hosts file ad blocking — pointing ad domains to 127.0.0.1 in
/etc/hostsblocks them by routing requests to the local machine, which has nothing listening on port 80
Binding to 0.0.0.0 vs 127.0.0.1
When a server application starts, it binds to an IP address and port. Binding to 127.0.0.1 means the service is accessible only from the local machine — no other device on the network can reach it. Binding to 0.0.0.0 means the service listens on all available network interfaces, including the physical NIC, making it reachable from other devices on the network (and potentially the internet). This distinction is critical for security: a database or development server should bind to 127.0.0.1 unless there is a specific reason to expose it to the network. Many security incidents stem from services accidentally bound to 0.0.0.0 in production environments.
Loopback interfaces on routers
In routing and network engineering, router loopback interfaces serve a different purpose from host loopback. A router loopback (e.g., Loopback0 in Cisco IOS) is a virtual interface assigned a stable IP address that is always up as long as the router is operational, regardless of which physical interfaces are functioning. This makes it ideal as the router ID for BGP peering — BGP sessions are established to the loopback address rather than a physical interface IP, so the session survives a physical link failure as long as an alternate path to the loopback exists. Loopback addresses are also used as the source address for management traffic and SNMP, ensuring that monitoring tools always reach the router via a stable address.
Frequently Asked Questions
What is 127.0.0.1 used for?
Connecting to services running on your own machine — local web servers, databases, APIs — without using a physical network interface. The entire 127.0.0.0/8 range is reserved for loopback; 127.0.0.1 is the conventional address.
What is the difference between 127.0.0.1 and localhost?
localhost is a hostname that resolves to the loopback address — typically 127.0.0.1 or ::1 depending on the system's DNS resolution order. They are functionally equivalent in most cases, but specifying the IP directly avoids IPv4/IPv6 resolution ambiguity.
Why would pinging 127.0.0.1 fail?
It should never fail — pinging loopback tests the OS TCP/IP stack without any physical hardware. If it fails, the issue is with the network stack itself, a firewall blocking loopback ICMP, or a corrupted network configuration.