Networking · foundation

Localhost and loopback

Loopback is a virtual network path that sends IP traffic back through the same host without placing it on an external link. localhost is a special-use name that normally resolves to loopback addresses such as 127.0.0.1 and ::1.

Why it matters

Binding a development server to loopback keeps it local to the host, while binding to broader addresses can expose it to containers, a LAN, or the internet. The distinction is both operational and security-relevant.

Mental model

How to reason about localhost and loopback

Packets addressed to loopback enter the local network stack and return internally. The destination still uses normal transport concepts such as ports, but no physical network interface needs to carry the packet.

Analogy

Loopback is an office's internal mail slot: a message still goes through the office's delivery process, but it never leaves the building.

Examples

See the boundary, not just the happy path

Worked example · Call a local-only service

curl http://127.0.0.1:3000/health

The request targets IPv4 loopback on TCP port 3000. It can reach a service listening there even when the machine has no working external network.

Worked example · Inspect localhost resolution

getent ahosts localhost

On systems with getent, this consults the configured name-service stack and can show IPv4 and IPv6 loopback results. Applications may choose one family before the other.

Useful contrast · Wildcard is not a destination address

A server bound to 0.0.0.0:3000 accepts on all local IPv4 interfaces

0.0.0.0 is commonly used as a wildcard bind address, whereas 127.0.0.1 identifies loopback. Clients should connect to a concrete destination, not treat 0.0.0.0 as equivalent to localhost.

Common mistakes

Misconceptions to remove early

Assuming localhost means the developer's laptop everywhere

localhost means the network namespace of the calling process. Inside a container or virtual machine, it normally refers to that isolated environment, not its host.

Testing only one address family

localhost can resolve to both ::1 and 127.0.0.1. A server listening only on one family may appear unavailable when a client tries the other first.

Quick check

Can you predict the result?

1. Where does traffic to 127.0.0.1 normally travel?
  • Through the local host's network stack and back to the same host.
  • To the default internet gateway and back.
  • To whichever machine owns the localhost DNS zone.
Answer: Through the local host's network stack and back to the same host.
2. Why does localhost inside a container usually not reach a service bound only to the host's loopback?
Answer: The container normally has its own network namespace, so its loopback path refers to the container itself.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster