127.0.0.1 — The Loopback Address
127.0.0.1 is the loopback IP address. It always refers to your own computer — no matter what machine you're on, 127.0.0.1 is "this device." Traffic sent to 127.0.0.1 never reaches a physical network interface, never leaves your machine, and loops back to the same process or server running on your system. This is by design: the entire 127.0.0.0/8 block (127.0.0.1 through 127.255.255.255) is reserved for loopback by the Internet Assigned Numbers Authority.
Why 127.0.0.1 Exists
Network software needs to talk to itself. A web server running on your machine needs a way to receive connections from the same machine without going out to the network — both for testing and for inter-process communication. The loopback interface provides this: a virtual network interface that the OS processes entirely in software, with no physical packet transmission.
When you start a development web server and open http://127.0.0.1:3000 in your browser, both the browser and the server are on the same machine. The request goes: browser → OS loopback interface → web server process → response back through loopback → browser. Zero physical network activity.
127.0.0.1 vs. localhost
| Address | Type | Resolves To | Notes |
|---|---|---|---|
127.0.0.1 | IPv4 loopback IP | Itself | Always works, bypasses DNS |
localhost | Hostname | 127.0.0.1 (or ::1) | Requires hosts file entry (present by default on all OSes) |
::1 | IPv6 loopback IP | Itself | IPv6 equivalent of 127.0.0.1 |
0.0.0.0 | Wildcard address | All local interfaces | Used to listen on all interfaces — not a loopback address |
The functional difference between 127.0.0.1 and localhost is subtle: localhost does a hostname lookup (usually very fast, from the hosts file), while 127.0.0.1 is a direct IP — no DNS at all. In a handful of environments (like some Docker setups or misconfigured hosts files), localhost may resolve to ::1 (IPv6) while 127.0.0.1 forces IPv4, which can cause connection refused errors when a server only listens on IPv4.
What 0.0.0.0 Means (vs. 127.0.0.1)
When a server binds to 0.0.0.0, it's saying "listen on all network interfaces" — the loopback, the ethernet adapter, the WiFi adapter, everything. This means both local connections and network connections can reach it. When a server binds to 127.0.0.1, it only accepts connections from the same machine. This distinction matters for development servers you don't want others on your network to access.
# Binds to 127.0.0.1 only — local machine access only
python -m http.server 8080 --bind 127.0.0.1
# Binds to 0.0.0.0 — accessible from any device on your network
python -m http.server 8080 --bind 0.0.0.0
# Or just:
python -m http.server 8080
The Full 127.x.x.x Block
The entire 127.0.0.0/8 range — that's 16,777,216 addresses from 127.0.0.1 to 127.255.255.254 — is reserved for loopback. All of them work as loopback addresses on most systems. In practice, 127.0.0.1 is the only one anyone uses, but you can legitimately bind different services to 127.0.0.2, 127.0.0.3, etc. to simulate multiple hosts on a single machine without using virtual IPs. Some testing frameworks and tools (like Basecamp's lvh.me) exploit this for subdomain testing.
Common Errors Involving 127.0.0.1
| Error | Cause | Fix |
|---|---|---|
| Connection refused at 127.0.0.1:PORT | Nothing is listening on that port — the server isn't running | Start your server first |
| Works on 127.0.0.1 but not your machine's IP | Server bound to 127.0.0.1 only (not 0.0.0.0) | Bind to 0.0.0.0 to allow network access |
| localhost works but 127.0.0.1 doesn't (or vice versa) | IPv4 vs IPv6 mismatch — server listens on IPv4, localhost resolves to ::1 | Force IPv4: use 127.0.0.1 explicitly, or configure server to bind both |
| Database "host: localhost" vs "host: 127.0.0.1" | MySQL/PostgreSQL: "localhost" uses a Unix socket, "127.0.0.1" uses TCP | Use 127.0.0.1 to force TCP, or ensure socket path is correct |
The MySQL localhost vs 127.0.0.1 Quirk
This trips up many developers. When connecting to MySQL or MariaDB, specifying host: localhost in your connection string does not use TCP/IP — it uses a Unix domain socket (on Linux/Mac). Specifying host: 127.0.0.1 forces a TCP connection. If your MySQL is listening on a socket but you're connecting via 127.0.0.1, or vice versa, you'll get "connection refused" or "access denied" errors that seem identical but have different root causes. PHP's PDO and most frameworks follow the same convention.
127.0.0.1 in the Hosts File
The hosts file on every OS maps hostnames to IPs before DNS is consulted. The line 127.0.0.1 localhost is present by default. You can add custom entries to test sites locally:
# /etc/hosts (Linux/Mac) or C:\Windows\System32\drivers\etc\hosts (Windows)
127.0.0.1 localhost
127.0.0.1 myapp.local
127.0.0.1 api.myapp.local
# Now http://myapp.local routes to your local server