Send, receive, and shield emails with PostScale. One API, EU-hosted. PostScale

    Fundamentals

    127.0.0.1 and Localhost Explained

    What 127.0.0.1 and localhost mean, how loopback networking works, why localhost is not public DNS, and how to debug local resolver issues.

    Updated

    TL;DR

    127.0.0.1 is the IPv4 loopback address: it points back to your own machine. localhost is the conventional name for that loopback address, usually mapped by the operating system through the hosts file or resolver rules. Public DNS should not resolve localhost to a real server. If a browser says it cannot reach localhost, the problem is usually the local service, port, firewall, container binding, or hosts file.

    What you'll learn

    • Explain what 127.0.0.1 means
    • Explain how localhost differs from a public DNS name
    • Show where hosts files and resolvers fit in
    • Debug common localhost and container binding problems

    127.0.0.1 means "this machine."

    When you connect to it, packets do not go to your router, your ISP, or the internet. The operating system routes them straight back to the same computer.

    That is why developers use addresses like:

    http://127.0.0.1:3000
    http://localhost:3000

    They mean "connect to the service running on my own machine, on port 3000."

    What 127.0.0.1 Is

    127.0.0.1 is part of the IPv4 loopback range:

    127.0.0.0/8

    The common address is:

    127.0.0.1

    If an application opens a connection to 127.0.0.1, the network stack loops the connection back locally.

    browser -> 127.0.0.1:3000 -> local app on the same machine

    No external DNS provider is involved.

    What Localhost Is

    localhost is the human-readable name for loopback.

    Most systems map it locally:

    127.0.0.1    localhost
    ::1          localhost

    You can usually find that mapping in:

    SystemHosts file
    macOS / Linux/etc/hosts
    WindowsC:\Windows\System32\drivers\etc\hosts

    The hosts file is checked before normal DNS on most systems. That is why localhost works even when you are offline.

    IPv4 vs IPv6 Localhost

    There are two common loopback addresses:

    AddressProtocolMeaning
    127.0.0.1IPv4loop back to this machine
    ::1IPv6loop back to this machine

    Some tools prefer IPv6 first. If an app listens only on IPv4, this can matter:

    curl http://localhost:3000
    curl http://127.0.0.1:3000
    curl http://[::1]:3000

    If 127.0.0.1 works but localhost fails, check whether the app is listening on IPv4, IPv6, or both.

    Localhost Is Not Public DNS

    This is the key point:

    your localhost != someone else's localhost

    If you send a colleague a URL like:

    http://localhost:3000

    their browser tries to connect to port 3000 on their own machine.

    To share a local app, use a proper hostname, a tunnel, a VPN, or deploy it somewhere reachable.

    Why Local Dev Servers Use It

    Localhost is useful because it is safe by default:

    • it does not expose the service to the network
    • it avoids public DNS changes
    • it works offline
    • it lets many apps use different ports on one machine

    Examples:

    npm run dev
    # Vite starts on http://localhost:5173
     
    go run ./cmd/api
    # API starts on http://127.0.0.1:8080

    The port number chooses the local service. The hostname chooses the local interface.

    127.0.0.1 vs 0.0.0.0

    These are often confused:

    AddressMeaning for a server
    127.0.0.1Listen only for connections from this same machine
    0.0.0.0Listen on all IPv4 interfaces
    ::Listen on all IPv6 interfaces

    If a dev server is bound to 127.0.0.1, another device on your Wi-Fi cannot reach it.

    If it is bound to 0.0.0.0, another device may be able to reach it through your LAN IP:

    http://192.168.1.25:3000

    Only do this intentionally. Firewalls and development servers are not a substitute for production access control.

    Localhost in Containers

    Containers add one more layer.

    Inside a Docker container:

    localhost = that container

    Not your laptop. Not another container.

    Common fixes:

    • publish the container port with -p 8080:8080
    • bind the app inside the container to 0.0.0.0
    • use the Docker service name to reach another container on the same network

    If an app works inside the container but not from your browser, check port publishing first.

    Debugging Checklist

    If localhost is not working:

    1. Check the service is running.
    2. Check the port is correct.
    3. Try 127.0.0.1 instead of localhost.
    4. Try ::1 if IPv6 may be involved.
    5. Check the app bind address.
    6. Check firewall or security software.
    7. Check /etc/hosts or the Windows hosts file.
    8. If using Docker, check port publishing.

    Useful commands:

    curl -v http://127.0.0.1:3000
    lsof -iTCP:3000 -sTCP:LISTEN
    dig localhost

    Frequently asked questions

    What is 127.0.0.1?
    127.0.0.1 is the standard IPv4 loopback address. Traffic sent there never leaves your machine; it is routed back to the local host.
    Is localhost the same as 127.0.0.1?
    Usually for IPv4, yes. localhost often resolves to 127.0.0.1 and may also resolve to ::1 for IPv6.
    Can other people access my localhost?
    No. Their localhost points to their own machine, not yours. To expose a local service, you need to bind it to a reachable interface and route traffic to it safely.
    Why does localhost work without public DNS?
    Operating systems treat localhost as a special local name. It is commonly defined in the hosts file and does not need an authoritative DNS lookup.
    What is ::1?
    ::1 is the IPv6 loopback address. It is the IPv6 equivalent of 127.0.0.1.
    Why does my app work on 127.0.0.1 but not from another device?
    The app is probably bound only to the loopback interface. Bind it to 0.0.0.0 or the machine's LAN address if you intentionally want other devices to connect.

    Ready to manage your DNS with confidence?

    DNScale provides anycast DNS hosting with a global network, real-time analytics, and an easy-to-use API.

    Start free