
0.0.0.0 vs 127.0.0.1: Difference and When to Use Each
0.0.0.0 vs 127.0.0.1 explained: 127.0.0.1 is loopback-only, while 0.0.0.0 binds to all interfaces. When to use each for local servers, Docker and security.
0.0.0.0 and 127.0.0.1 are not interchangeable: 127.0.0.1 is for connecting, 0.0.0.0 is for listening. 127.0.0.1 is the loopback address reachable only from your own machine. 0.0.0.0 is a special "all interfaces" address used when a server binds a socket, it tells the server to accept connections on every network interface. You connect to a service at 127.0.0.1; you bind a service to 0.0.0.0 (or 127.0.0.1) to choose who can reach it.
0.0.0.0 vs 127.0.0.1 at a glance
| Aspect | 127.0.0.1 | 0.0.0.0 |
|---|---|---|
| Meaning | Loopback (this machine only) | All local interfaces |
| Typical use | Connecting; private binding | Server binding for external access |
| Reachable from other devices | No | Yes |
| Security | Safe by default | Exposes the service |
| Browser address | Yes | Not reliably |
When to use 127.0.0.1
Bind to 127.0.0.1 when a service should be local only, a dev database, a local API you do not want exposed, or anything sensitive. Other devices on the network cannot reach it, which is the safe default. To connect, use 127.0.0.1 or localhost (note the IPv6 nuance in the section below).
When to use 0.0.0.0
Bind to 0.0.0.0 when the server must be reachable from outside the machine: testing a site on your phone over the LAN, or a Docker container whose port must reach the host. Many frameworks default to 127.0.0.1; switch to 0.0.0.0 (e.g. --host 0.0.0.0) to expose it.
localhost vs 127.0.0.1 vs 0.0.0.0
The three are often mixed up, and they are different kinds of things:
| What it is | Use it to | |
|---|---|---|
localhost | A name that resolves to 127.0.0.1 and usually ::1 | Connect to your own machine |
127.0.0.1 | The IPv4 loopback address | Connect, or bind a local-only server |
0.0.0.0 | A wildcard meaning "every interface" | Bind a server that others may reach |
The whole 127.0.0.0/8 block is loopback. On Linux and Windows, an address such as 127.0.0.2 also reaches your machine, which is handy for running two servers on the same port number. macOS only configures 127.0.0.1 by default, so other addresses need an alias on the loopback interface first.
The IPv6 equivalent (::1)
On IPv6, the loopback address is ::1, the equivalent of 127.0.0.1. A service may listen on the IPv4 loopback but not on ::1 (or the reverse), which is a common cause of ERR_CONNECTION_REFUSED: the browser resolves localhost to the IPv6 loopback first and gets a connection refused because nothing is bound there. For listening on every interface, the IPv6 wildcard :: plays the same role as 0.0.0.0.
The Docker gotcha
A container that binds its app to 127.0.0.1 listens only inside the container, so docker run -p 8080:8080 appears broken. Bind the app to 0.0.0.0 inside the container so the published port reaches the host. See managing the hosts file with Docker.
Framework examples
Node.js / Express
// Local only (safe default)
app.listen(3000, '127.0.0.1');
// Reachable from phone on same Wi-Fi
app.listen(3000, '0.0.0.0');Vite
// vite.config.js - LAN testing
export default { server: { host: '0.0.0.0', port: 5173 } };Docker Compose
services:
web:
ports:
- "8080:8080"
environment:
- HOST=0.0.0.0Inside the container, the app must bind 0.0.0.0, not 127.0.0.1.
0.0.0.0 in the hosts file
In the hosts file, the address is not a bind address but an answer. When you map a domain to 0.0.0.0, lookups return an address that leads nowhere, so the connection fails at once. Mapping it to 127.0.0.1 sends the request to your own machine instead, where a local web server on port 80 or 443 may answer or make the browser wait.
That is why the maintained block lists, such as the one used for blocking ads with the hosts file, point blocked domains to 0.0.0.0. For the full method and its limits, see how to block a website with the hosts file.
What happens when you connect to 0.0.0.0
Using 0.0.0.0 as a destination is not reliable. On Linux and macOS a connection to it usually reaches the local machine. On Windows it fails. Do not rely on it: connect to 127.0.0.1 or localhost, and keep 0.0.0.0 for binding and for blocking entries.
Check what your server is bound to
The listening address tells you who can reach the service:
# macOS and Linux
lsof -iTCP -sTCP:LISTEN -n -P
# Linux
ss -ltn# Windows (Command Prompt)
netstat -ano | findstr LISTENING| Address in the output | Meaning |
|---|---|
127.0.0.1:3000 | Local only, IPv4 |
[::1]:3000 | Local only, IPv6 |
0.0.0.0:3000 or *:3000 | Every IPv4 interface |
[::]:3000 | Every IPv6 interface, often IPv4 too on a dual-stack system |
Quick decision tree
- Only this Mac needs access? → bind
127.0.0.1 - Phone/tablet on same network? → bind
0.0.0.0, check firewall - Docker port mapping broken? → app inside container →
0.0.0.0 - Browser connection refused on localhost? → check IPv6
::1vs127.0.0.1, see localhost refused to connect
Security reminder
Binding to 0.0.0.0 exposes the service to your whole network. On untrusted Wi-Fi, this can leak a dev server. Prefer 127.0.0.1 unless you specifically need external access, and use friendly local domains for clarity, see the hosts file syntax guide. ToggleHosts makes those local domain mappings easy to manage and toggle.
_Last reviewed: June 2026._
Sources and further reading
Frequently Asked Questions
127.0.0.1 is the loopback address, reachable only from the same machine. 0.0.0.0 is a wildcard that tells a server to listen on all network interfaces, so it is reachable from other devices.
On a trusted local network it is fine, but it exposes the service to anything that can reach your machine. For local-only development, bind to 127.0.0.1.
Inside a container, binding to 127.0.0.1 only listens within the container. Bind to 0.0.0.0 so the published port is reachable from the host.
On Linux it often maps to localhost, but behavior varies by OS. Use 127.0.0.1 or localhost to connect; use 0.0.0.0 only for binding.
Related Articles
Localhost Refused to Connect: How to Fix ItTroubleshooting
Localhost Refused to Connect: How to Fix It
7 min read
Using Hosts Files for Docker Development on MacLocal development
Using Hosts Files for Docker Development on Mac
9 min read
How to Sync WSL2 with Windows Hosts File for Local Domains (2026)Local development
How to Sync WSL2 with Windows Hosts File for Local Domains (2026)
5 min read