localhost and 127.0.0.1 explained, with fixes for common errors
2 min readNetworking · Developer Tools · Guides
localhost is the name your computer uses for itself. It resolves to 127.0.0.1 for IPv4 and ::1 for IPv6, and traffic sent there never leaves the machine. When a tutorial says to open http://localhost:3000, it means a server running on your own computer, listening on port 3000. Most beginner errors come from mixing up three things: the name, the address, and the interface a server actually listens on.
What it actually is
- The whole 127.0.0.0/8 block is loopback, not just 127.0.0.1. The address 127.0.0.2 also points at your own machine.
- Loopback traffic goes through a virtual interface inside the operating system, not through your network card or router.
- The name localhost is mapped by the hosts file: /etc/hosts on Linux and macOS, C:\Windows\System32\drivers\etc\hosts on Windows.
- Browsers treat http://localhost as a secure context, so features that normally need HTTPS generally work in development.
- localhost is private. Nobody else can reach your localhost, and you cannot reach theirs.
127.0.0.1 vs 0.0.0.0 vs your LAN IP
A server decides which address it listens on. Binding to 127.0.0.1 means only programs on the same machine can connect. Binding to 0.0.0.0 means every interface, so anyone on your network can connect too. Your LAN IP, something like 192.168.1.20, is how other devices on your network reach you. Dev servers often default to loopback for exactly this reason.
# only this machine can connect
python3 -m http.server 8000 --bind 127.0.0.1
# every device on the network can connect
python3 -m http.server 8000 --bind 0.0.0.0When 'localhost refused to connect' shows up
That message means nothing is listening on that address and port. The server is not running, it crashed, or it is on a different port than you think. Check what is listening before you touch the code.
# Linux
ss -ltnp | grep 3000
# macOS
lsof -iTCP:3000 -sTCP:LISTEN
# Windows PowerShell
netstat -ano | findstr :3000Where it bites
- IPv4 vs IPv6: localhost can resolve to ::1 first. If the server only listens on IPv4, connect to 127.0.0.1 explicitly, or the reverse. Newer Node.js versions are a common place to see this.
- Docker: inside a container, localhost is the container itself, not your laptop. Publish the port with -p and connect from the host, or use the service name on a Compose network.
- WSL2 and virtual machines: localhost may or may not forward to the machine you expect. It depends on your setup, so test it.
- Port already in use: two programs cannot listen on the same port. Find the owner with the commands above and stop it, or pick another port.
- Binding to 0.0.0.0 exposes your dev server to the whole network, including cafe Wi-Fi. Do not leave an unauthenticated admin tool open that way.
- Hosts file edits: a stray line that maps localhost somewhere else breaks everything that relies on the name.
Verdict
When something on localhost fails, ask three questions in order: is a process listening, on which address, and on which port. That sequence solves most of it before you open a search engine. Keep dev servers on 127.0.0.1 unless you have a reason to share them.

Written by
Shihab Hossain — Full-Stack Developer
I build thoughtful digital products from Bangladesh — fast web experiences with Next.js, Android apps with Flutter, and the infrastructure behind them.
Work with me