Localhost is the name a computer uses to refer to itself: it resolves to the loopback address 127.0.0.1 in IPv4 and ::1 in IPv6, and traffic sent there never leaves the machine. When a developer tool says "running at http://localhost:3000", it means a server on your own computer, on port 3000, that only your computer can reach.
ping localhost
Example output on Windows 11 (host name anonymised):
Pinging alex-pc [::1] with 32 bytes of data:
Reply from ::1: time<1ms
Reply from ::1: time<1ms
The reply comes from ::1, the IPv6 loopback, in under a millisecond, because the packet only travels through the operating system's network stack, not through any network card.
What localhost, 127.0.0.1 and ::1 are
| Name or address | What it is | Defined in |
|---|---|---|
localhost | A reserved host name that must resolve to loopback | RFC 6761, section 6.3 |
127.0.0.1 | The usual IPv4 loopback address; the whole 127.0.0.0/8 block is loopback | RFC 1122, section 3.2.1.3 |
::1 | The single IPv6 loopback address (0:0:0:0:0:0:0:1) | RFC 4291, section 2.5.3 |
RFC 1122 describes { 127, <any> } as the "internal host loopback address" and says such addresses "MUST NOT appear outside a host". So it is not only 127.0.0.1: any address from 127.0.0.0 to 127.255.255.255 loops back. You can check this yourself; on the same Windows machine ping -n 1 127.5.5.5 got Reply from 127.5.5.5. IPv6 has just one loopback address, ::1, and RFC 4291 says packets sent to it must never leave the node or be forwarded by a router.
RFC 6761 reserves the name localhost and every name ending in .localhost. Name-resolution libraries are expected to answer address lookups for them with the loopback address without asking a DNS server, and registrars may not register them. So app.localhost also means "this machine" in software that follows the RFC.
How loopback works
The operating system has a virtual network interface just for loopback, which RFC 4291 describes as a "loopback interface" attached to a link that goes nowhere. On Windows 11, Get-NetIPInterface lists it as Loopback Pseudo-Interface 1, with both IPv4 and IPv6. It has no hardware behind it. When a program connects to 127.0.0.1:8000, the operating system hands the data straight to whatever program is listening on port 8000 on the same machine. Because nothing goes on the wire, loopback works with Wi-Fi switched off and is not affected by your router, ISP or internet speed.
The port number decides which local program gets the traffic, exactly as it does for remote connections. What a port number is covers that half of the address.
What "running on localhost:3000" means
A development server prints a localhost address to tell you two things: the server is on your own computer, and it is listening on that port. Try it with Python, which ships a small web server:
python -m http.server 8765 --bind 127.0.0.1
Then open http://localhost:8765/ in a browser, or from another terminal:
curl -v http://localhost:8765/ -o /dev/null
Example output (Windows 11, Python 3.12.2, curl from Git Bash; trimmed):
* Trying [::1]:8765...
* Trying 127.0.0.1:8765...
* Connected to localhost (127.0.0.1) port 8765
< HTTP/1.0 200 OK
< Server: SimpleHTTP/0.6 Python/3.12.2
curl resolved localhost to both loopback addresses, tried IPv6 ::1 first, found nothing listening there because the server was bound to 127.0.0.1 only, and fell back to IPv4. Not every client falls back. If a tool reports "connection refused" on localhost while 127.0.0.1 works, an IPv4-only server and an IPv6-first client is the usual reason: connect to 127.0.0.1 explicitly, or make the server listen on ::1 too.
Localhost vs 0.0.0.0 vs your local IP
A server chooses which addresses to listen on. This choice decides who can reach it:
| Server binds to | Who can connect |
|---|---|
127.0.0.1 or localhost | Only programs on the same computer |
0.0.0.0 (IPv4) or :: (IPv6) | Every interface: the same computer and, if the firewall allows it, other devices on the network |
A local IP such as 192.168.1.20 | Devices that can reach that address, typically your LAN |
Defaults vary by tool, so read the startup message. Python's http.server, for example, binds to all interfaces unless you pass --bind, according to its documentation. That is why the command above includes --bind 127.0.0.1: without it, other devices on your Wi-Fi could browse the folder you started it in.
0.0.0.0 is a "listen on everything" instruction for servers, not a destination. To test a server from your phone, open your computer's local IP (for example http://192.168.1.20:8765), not localhost, which on the phone means the phone. Public vs private IP addresses explains how to find that local IP.
Why browsers treat localhost as secure
Many browser features, such as service workers, the async Clipboard API and the Geolocation API, only work in a "secure context", which normally means HTTPS. The W3C Secure Contexts specification makes an exception: origins whose host is in 127.0.0.0/8 or ::1/128, and, in browsers that follow RFC 6761's rules for the name, localhost and names ending in .localhost, count as "potentially trustworthy". That is why you can develop those features on http://localhost without a certificate, but not on http://192.168.1.20.
Localhost inside Docker and virtual machines
A container or VM has its own network stack, so localhost inside it means the container or VM, not your computer. A database running on your laptop at localhost:5432 is not reachable from a container at localhost:5432. Docker Desktop provides a special name for this: host.docker.internal, which its documentation says resolves to the internal IP address of your host. Going the other way, from your computer into a container, needs a published port; see Docker port mapping.
Where localhost is defined on your machine
Your operating system's resolver knows localhost before any DNS server is asked. On Linux and macOS you can see the mapping in /etc/hosts:
127.0.0.1 localhost
::1 localhost
On Windows the default C:\Windows\System32\drivers\etc\hosts has the same lines commented out, with the note # localhost name resolution is handled within DNS itself.: Windows answers for localhost internally. You can add your own names to the hosts file, for example to point myapp.test at 127.0.0.1 during development; how to edit the hosts file covers that on each OS.
If localhost doesn't work
- "Connection refused" or "This site can't be reached": nothing is listening on that port. Check the server's startup output for the actual port, and whether it crashed.
- Works with
127.0.0.1, fails withlocalhost: the IPv4/IPv6 mismatch described above, or a hosts-file entry that remapslocalhost. Open the hosts file and check. - "Address already in use" when starting the server: another program holds the port; see how to fix "port already in use".
- Browser forces HTTPS: type the full
http://localhost:PORT, and check the server really speaks plain HTTP.
Frequently asked questions
- Can other people access my localhost?
- No. Traffic to 127.0.0.1 or ::1 never leaves your computer, and RFC 1122 says loopback addresses must not appear outside a host. Someone on your network reaches your machine through its local IP address instead, and only if a program is listening on that address and the firewall allows it.
- Is localhost the same as 127.0.0.1?
- Almost. localhost is a name; 127.0.0.1 is one IPv4 address it resolves to. On most systems localhost also resolves to the IPv6 loopback ::1, and many clients try ::1 first. A server listening only on 127.0.0.1 still works through localhost if the client falls back to IPv4, as curl does.
- Does localhost need an internet connection?
- No. The loopback interface is virtual and exists even with networking switched off, so a server on localhost works on a plane or with the Wi-Fi disabled.
- Why does localhost show 'This site can't be reached' or 'connection refused'?
- Nothing is listening on that port. Check that your server actually started, which port it printed, and that you typed http rather than https if the server does not use TLS. netstat -ano on Windows or lsof -i :PORT on macOS and Linux shows what is listening.
Sources
- RFC 6761: Special-Use Domain Names (section 6.3, localhost)
- RFC 1122: Requirements for Internet Hosts (127/8 loopback)
- RFC 4291: IPv6 Addressing Architecture (section 2.5.3, loopback address)
- W3C: Secure Contexts (potentially trustworthy origins)
- MDN: Features restricted to secure contexts
- Python docs: http.server (--bind)
- Docker docs: Networking how-tos (connect a container to a service on the host)