Skip to content

Why is 127.0.0.1 the 'Loopback' Address?

If you've ever built a website or run a database, you've used 127.0.0.1. It’s the universal address for "this computer right here."

But have you ever stopped to wonder why it's such a weird number? Why not 1.1.1.1 or 0.0.0.0? And why is the whole 127.x.x.x block reserved just for us?

The Big Picture: Talking to Yourself

In networking, sometimes you want to send a message to a program running on your own machine. Instead of sending that message out to the router and back, the OS has a "shortcut."

[ Your App ] --( Request )--> [ Loopback Interface ] --( Success! )--> [ Your Database ]
                                     |
                                (Never leaves the computer)

The History: Class A Networks

Back in the early days of the internet (the 1980s), IP addresses were divided into "Classes." - Class A was for massive networks and used the first number (0 to 127). - 127 was the very last block available in Class A.

The engineers decided to reserve this entire last block for "loopback" testing.

Why 127?

There isn't a deep technical reason like "127 is a magic prime number." It was mostly a matter of convenience. They needed a block, and 127 was at the end of the list.

The most famous address in that block is 127.0.0.1, but technically, 127.0.0.2 or 127.99.88.77 would also work as your "local" machine!


Wait, but why reserve the WHOLE block?

We reserved 16.7 million addresses (127.0.0.0 to 127.255.255.255) just so one computer could talk to itself.

In hindsight, this was a bit wasteful. We are running out of IPv4 addresses, and here we have 16 million of them sitting idle on every single device! But back then, they thought 4 billion total addresses was "plenty."


Common gotchas

  • I always forget that localhost is just a name. Your computer has a hosts file that maps the word localhost to the address 127.0.0.1.
  • Watch out for 0.0.0.0: This is different! 127.0.0.1 means "talk to myself." 0.0.0.0 usually means "listen on every available network interface" (like your Wi-Fi AND your Ethernet).

Try it yourself

You can prove that the whole block works! Try pinging a random address starting with 127:

ping 127.42.42.42
It should work exactly like ping 127.0.0.1.


Further reading

β€” Nadeem 🏠

Comments