Loopback and 127.0.0.1: Talking to Yourself on Purpose
Learn what the loopback address does, how localhost relates to it, how to use it for testing and why binding to it affects security.
Key takeaways
- The whole 127.0.0.0/8 block is reserved for loopback in IPv4, while IPv6 uses the single address ::1.
- Loopback traffic stays inside the operating system and works even with no network connected.
- A server bound only to a loopback address is invisible to other devices.
-
localhostcan resolve to both ::1 and 127.0.0.1, which sometimes causes puzzling connection failures. - Loopback limits who can connect, but local programs and web pages can still try to talk to local services.
127.0.0.1 is an address that always means "this computer." A program that connects to it is talking to another program, or to itself, on the same machine, without any packet reaching a cable or radio. The name localhost normally points there, and in IPv6 the equivalent address is ::1.
Developers use it to test web servers before publishing them, and many ordinary applications use it quietly to let their parts communicate. Understanding it also explains a common puzzle: why a service works on your own machine but nobody else can reach it.
What loopback is
Every operating system with networking has a virtual interface for loopback, often named lo on Linux and lo0 on macOS and BSD. It behaves like a network interface, with an address and statistics, but no hardware.
When a program sends data to a loopback address, the networking software hands it straight back up to the receiving side on the same host. The usual protocols still apply: connections use ports, TCP still performs its handshake and an application can't easily tell the difference from a remote connection, apart from the speed.
Why so many addresses?
RFC 1122 reserves the entire 127.0.0.0/8 block, more than 16 million addresses, for loopback, and says packets with these addresses must never appear on a network. That is lavish by today's standards, but it was decided when space seemed plentiful.
The extra addresses have uses. On many Linux systems and Windows, you can run separate services on 127.0.0.2, 127.0.0.3 and so on, each using the same port number. Some systems point the local DNS stub resolver at an address such as 127.0.0.53. macOS, by default, configures only 127.0.0.1 on the loopback interface, so other values need to be added before they work.
IPv6 took the opposite approach and defines exactly one loopback address, ::1.
localhost and the hosts file
localhost is a name, and names have to be translated. Operating systems answer it locally, traditionally from the hosts file:
127.0.0.1 localhost
::1 localhost The standards treat localhost as a special name that should always resolve to loopback, so you shouldn't repoint it. You can add your own test names in the hosts file, such as shop.test, and aim them at 127.0.0.1. The .test ending is reserved for this kind of use.
The two lines above cause a classic problem. A program may try ::1 first while your server listens only on 127.0.0.1, or the reverse. The result is "connection refused" for localhost while the numeric address works. Fix it by using the explicit address in the client, or by having the server listen on both.
Testing with loopback
Say you are building a small website. Python's built-in server makes a quick demonstration:
python3 -m http.server 8000 --bind 127.0.0.1 On Windows the command may be python instead of python3. Open http://127.0.0.1:8000/ in a browser, or:
curl -I http://127.0.0.1:8000/ (In Windows PowerShell, use curl.exe so that you get the real curl rather than an alias.) The server serves the folder you started it from, so don't run it somewhere containing private files.
To see what is listening on your machine:
netstat -ano | findstr LISTENING # Linux
ss -ltnp
# macOS
lsof -iTCP -sTCP:LISTEN -n -P Showing process details on Linux and macOS may need sudo. Testing a port from the command line covers more.
Binding: who is allowed to knock
When a server starts, it chooses which address to listen on. This choice decides who can reach it.
| Listening address | Who can connect |
|---|---|
127.0.0.1 or ::1 | Only programs on the same machine |
A LAN address such as 192.168.1.20 | Devices that can reach that interface |
0.0.0.0 | Anyone who can reach any IPv4 interface |
:: | Any IPv6 interface, and on some systems IPv4 too |
That second question trips up many people testing from a phone. Typing localhost on the phone asks the phone to connect to itself.
Containers and virtual machines
A container usually has its own network namespace with its own loopback. Inside a container, 127.0.0.1 means that container, not the host computer. So:
- A server inside a container that binds only to loopback cannot be reached through a published port. It needs to listen on the container's external interface.
- A container trying to reach a database on the host needs the host's address or a special name provided by the container tool, which differs between products and platforms.
- Publishing a port on the host without specifying an address often exposes it on all interfaces. Specify
127.0.0.1in the publish setting if only the host should use it.
Virtual machines are similar: each has a private loopback.
Security: helpful, with limits
Binding to loopback is one of the simplest protective measures available. A database or admin panel that only the same machine needs should listen there. For remote administration, an SSH tunnel lets you reach a loopback-only service securely:
ssh -L 8080:127.0.0.1:5432 [email protected] Connections to port 8080 on your computer travel encrypted to the server and arrive at port 5432 on its loopback.
Loopback is not a guarantee, though. Any program or user on the machine can connect. A web page open in your browser can attempt requests to local ports, and browsers have been adding restrictions on this. A technique called DNS rebinding tries to get a browser to treat a hostile name as pointing at your loopback address. Good local services therefore require authentication, check the Host header and reject cross-origin requests they didn't expect. A reverse proxy on the same machine also makes every visitor look as if they come from loopback, so never grant trust based on that source address alone when a proxy sits in front.
Everyday software that uses it
You are probably running loopback services right now. Common examples include:
- Development servers. Web frameworks usually print a local address when they start, and listen only there by default.
- Databases for desktop use. A database installed for one application on one computer needs no network access.
- Local DNS stubs. Some systems and DNS tools run a small resolver on a loopback address and point applications at it.
- Local proxies. Ad filters, debugging proxies and some security products ask the browser to send traffic through a port on
127.0.0.1. - Sign-in flows for desktop apps. A program may open your browser to log in and then wait on a temporary loopback port to receive the result. OAuth guidance for native apps (RFC 8252) describes this pattern.
If one of these stops working, a common cause is that something else grabbed the port first. The server then reports "address already in use." Use the listening-port commands above to find the other program, then stop it or choose a different port.
A leftover proxy setting is another classic. If a debugging proxy or filter is removed but the system still points at 127.0.0.1, every page fails to load while other connectivity looks fine. Check the proxy settings in the operating system and browser, and turn off any entry you no longer use.
Local addresses and web development
Browsers treat loopback specially in one helpful way: pages served from http://localhost or http://127.0.0.1 are generally considered secure contexts, so features that normally require HTTPS can be developed without a certificate. That allowance does not extend to your LAN address. When you open the same development server from a phone using http://192.168.1.20:8000/, some features may refuse to work.
Origins matter too. http://localhost:8000 and http://127.0.0.1:8000 are different origins to a browser, even though they reach the same server. Cookies and stored data set under one are not visible under the other, and a cross-origin rule that allows one may reject the other. Pick one spelling and use it consistently. CORS explained covers the rules.
What loopback tests can and cannot show
Pinging 127.0.0.1 confirms that the local IP software is working:
ping 127.0.0.1 It says nothing about your network adapter, cable, router or provider, because none of them is involved. A successful loopback ping with a failed router ping points to the local network. A failed loopback ping is rare and suggests a damaged network stack or very restrictive security software.
Similarly, an app that works over loopback may still fail in the real world. Loopback has almost no delay, no loss and a very large packet size, so timing bugs and fragmentation problems stay hidden. Test across a real network before relying on it.
Common questions
Is something spying on me if I see 127.0.0.1 connections?
Local connections are normal. Many applications talk to their own helper processes this way. If you're unsure, find which program owns the port using the commands above.
Why do some blocklists point domains at 127.0.0.1?
A hosts file entry like that makes the name resolve to your own machine, so the request goes nowhere useful. 0.0.0.0 is often used for the same purpose and tends to fail faster.
Can another computer send packets to my loopback address?
It shouldn't. Correctly working systems drop packets arriving from a network with a loopback address. Keeping the operating system updated is part of what keeps that true.