127.0.0.1 and localhost explained
127.0.0.1 is the address a computer uses to talk to itself, and localhost is its name. Here is what the loopback interface does, why developers live on it, and what to check when a page at localhost refuses to connect.
In this guide
127.0.0.1 is the IPv4 loopback address: any traffic sent to it is delivered back to the same computer by the operating system and never reaches the network. "localhost" is the hostname that resolves to 127.0.0.1 (and to ::1 in IPv6). A server listening on 127.0.0.1 can be reached only by programs on that machine, which is why it is the default address for development servers and databases.
What loopback means
Every operating system has a network interface that does not correspond to any hardware: the loopback interface, called lo on Linux and macOS and Loopback Pseudo-Interface on Windows. Packets sent to it are handed straight back up the stack to whichever program is listening, as if they had arrived from the network. The standard that defines how hosts behave on the internet reserves the whole 127.0.0.0/8 block for this, over sixteen million addresses from 127.0.0.1 to 127.255.255.254, and states that addresses of that form must never appear outside a host.
IPv6 keeps it to a single address, ::1. Both are configured on every machine automatically, and both work even when the computer has no network connection at all, which is exactly the point: software can test network code, and services can talk to each other, without a cable or a Wi-Fi signal.
localhost is the name for the same thing. It is not looked up on the internet: the rules for special-use domain names say that resolvers should answer localhost with the loopback address themselves and never send the question to a DNS server. Historically the mapping sat in the hosts file (/etc/hosts on macOS and Linux, C:\Windows\System32\drivers\etc\hosts on Windows); Windows now handles it internally, which is why its hosts file only contains a comment saying so.
Browsers give loopback one more privilege: an origin on 127.0.0.1, ::1 or localhost counts as a secure context, so features that normally require HTTPS, such as service workers, the camera and the geolocation API, work over plain http://localhost during development.
127.0.0.1, localhost, 0.0.0.0 and your LAN address
Four addresses turn up in server configuration and error messages, and they mean different things.
| Address | Meaning | Who can connect to a server bound to it |
|---|---|---|
127.0.0.1 | IPv4 loopback | Programs on this computer only, over IPv4 |
::1 | IPv6 loopback | Programs on this computer only, over IPv6 |
localhost | A name for both of the above; which one a program picks first varies | Programs on this computer, if the server listens on the family the client chose |
0.0.0.0 | "All interfaces" when used as a listening address | Anything that can reach this computer: the LAN, and the internet if a port is forwarded |
192.168.1.23 (example) | This computer's address on the local network | Other devices on the same LAN |
The mismatch between the first three rows is the source of a surprising number of "it works on my machine" failures. A browser that resolves localhost to ::1 cannot reach a server that listens only on 127.0.0.1, and the error looks identical to the server not running. Typing 127.0.0.1 instead of localhost removes the ambiguity, which is why experienced developers often do.
Your LAN address is the one other devices need; the local IP tool shows it, and Public vs private IP addresses explains how it relates to the address the internet sees.
Test it now
Cloudflare edgeRead at the Cloudflare edge from this request. The homepage adds your provider, network type and the VPN, DNS and WebRTC checks.
Show detailsCommon uses of 127.0.0.1
- Development servers.
http://localhost:3000,http://127.0.0.1:8080andhttp://localhost:5173are web apps running on the developer's own machine, visible to nobody else. The number after the colon is the port the program chose; the address is always the same. - Databases and caches. MySQL on
127.0.0.1:3306, PostgreSQL on127.0.0.1:5432, Redis on127.0.0.1:6379. Binding them to loopback is the single most effective way to keep them off the internet; a database bound to0.0.0.0with a weak password is how most "exposed database" breaches begin. - Local resolvers and filters. Pi-hole, Unbound and similar tools run a DNS server on the machine itself; the system then uses
127.0.0.1as its resolver. The What is my DNS server page and the DNS leak test show that arrangement as a resolver at your own address. - Blocking sites with the hosts file. A line such as
127.0.0.1 ads.example.comsends that name to your own machine, where nothing answers. It still works, with the caveat that browsers using DNS over HTTPS may bypass the hosts file. - Tunnels and containers. An SSH tunnel exposes a remote service on a local port (
ssh -L 5432:dbhost:5432makes a remote database appear at127.0.0.1:5432), and Docker's port mapping does the same for a container. - Testing network software without a network: a client and server on the same laptop, on an airplane, work fine over loopback.
Fixing "localhost refused to connect"
Chrome's wording is localhost refused to connect with the code ERR_CONNECTION_REFUSED; Firefox says Unable to connect. Both mean the same thing: your computer accepted the request, looked for a program listening on that port, and found none. The network is not the problem. Work down this list.
- Is the server actually running? Look at the terminal where you started it. A crash on startup, a syntax error or a missing dependency leaves the browser with nothing to talk to.
- Is the port right? The server prints the port it chose when it starts (
Listening on http://localhost:3000). If 3000 was busy it may have taken 3001. Match the address bar to that line exactly. - IPv4 or IPv6? Replace
localhostwith127.0.0.1in the address bar. If that works, the server listens on IPv4 only and your browser tried::1first. Either keep using127.0.0.1or make the server listen on both. - Is it bound somewhere else? A server started inside Docker, WSL or a virtual machine listens on that system's loopback, not yours, unless the port is published. Check the container's port mapping or WSL's networking mode.
- Is a firewall or security tool interfering? Rare for loopback, but some endpoint-protection products and proxy settings do intercept it. Temporarily disable the browser's proxy and try again.
- See what is listening. Windows:
netstat -ano | findstr LISTENING. macOS:lsof -iTCP -sTCP:LISTEN -n -P. Linux:ss -ltnp. The output names every port with a program behind it; if yours is missing, the server is not up.
The opposite problem, a service on your machine that other devices cannot reach, has the same cause in reverse: it listens on 127.0.0.1. Bind it to 0.0.0.0 or to your LAN address, allow the port through the operating system firewall, and connect from the other device using your LAN address rather than localhost, which on that device would mean itself. Use the port checker only if you have also forwarded the port on the router and want to know whether it is reachable from the internet.
Questions
Is 127.0.0.1 the same as localhost?
In practice, yes. localhost is a name; 127.0.0.1 is the IPv4 address it resolves to, and ::1 is the IPv6 one. The difference only shows when a program listens on one address family and a client tries the other, which produces a "refused to connect" error even though the server is running. Using 127.0.0.1 directly avoids the ambiguity.
Is 127.0.0.1 safe?
Yes. Traffic to 127.0.0.1 never leaves the computer, so a service bound to it cannot be reached from the local network or the internet, no matter what the router does. The only software that can connect to it is software already running on the same machine, which is why loopback is the recommended address for databases, admin panels and anything else that should stay private.
Is localhost safe to use?
Yes, for the same reason: connections to localhost stay inside the machine. What people need to watch is the opposite setting. A development server started with its host set to 0.0.0.0 listens on every interface and becomes visible to everyone on the same Wi-Fi, which is fine at home and risky in a cafe. Check which address your server reports when it starts.
What does localhost:8080 mean?
A program on your own computer listening on port 8080, reachable at http://localhost:8080 from that machine only. Port 8080 is a conventional alternative to the standard web port 80, used because binding port 80 needs administrator rights on most systems. Tomcat, Jenkins, many proxies and countless development servers default to it; the address part is always your own machine.
How do I find my localhost URL?
It is http://localhost followed by a colon and the port the program told you it is using, such as http://localhost:3000. The port appears in the program's startup output or its configuration; if none is mentioned, the default web port 80 is assumed and the URL is just http://localhost. If the page does not load, the port is wrong or the program is not running.