localhost: 127.0.0.1 and Loopback Explained

By 

Updated on

6 min read

What is localhost and how the loopback interface works

Localhost is the hostname a computer uses to refer to itself. When a program connects to localhost, it connects back to the same machine, so the network traffic stays local instead of going out to the network.

This guide explains what localhost is, how it maps to loopback addresses such as 127.0.0.1 and ::1, and how it is used in development, testing, and network diagnostics. The examples use Linux commands, but the core idea applies to Linux, macOS, Windows, BSD, and other networked systems.

What localhost Means

Every modern operating system has a special virtual network interface called the loopback interface. Unlike a physical network card, the loopback interface exists entirely in software. Any packet sent to it is immediately routed back to the same machine without touching any hardware or external network.

The loopback interface is assigned the IP address 127.0.0.1 in IPv4. The entire 127.0.0.0/8 block is reserved for loopback by the IETF, meaning any address from 127.0.0.1 to 127.255.255.254 routes back to the local machine. In practice, 127.0.0.1 is the standard address used.

In IPv6, the loopback address is ::1.

On Linux, localhost usually resolves through the /etc/hosts file before DNS is queried:

Terminal
cat /etc/hosts
output
127.0.0.1   localhost
127.0.1.1   hostname

# IPv6 capable hosts
::1         ip6-localhost ip6-loopback

On most Linux systems, entries in /etc/hosts are checked before DNS resolution based on the resolver order in /etc/nsswitch.conf. Other operating systems have the same localhost concept, but the hosts file path and resolver configuration differ.

localhost vs 127.0.0.1 vs 0.0.0.0

localhost, 127.0.0.1, and 0.0.0.0 are often mentioned together, but they do different jobs.

  • localhost is a hostname. It usually resolves to 127.0.0.1 for IPv4 and may also resolve to ::1 for IPv6.
  • 127.0.0.1 is the standard IPv4 loopback address. Connecting to it always means connecting to the same machine.
  • ::1 is the IPv6 loopback address. It serves the same purpose as 127.0.0.1, but for IPv6 traffic.
  • 0.0.0.0 is not a loopback address. When a server binds to 0.0.0.0, it listens on all available IPv4 interfaces, including loopback and network-facing interfaces.

For local-only development, bind a service to localhost or 127.0.0.1. To make a service reachable from other devices on the network, bind it to the machine’s LAN IP address or to 0.0.0.0, then make sure the firewall allows the port.

How Loopback Traffic Works

When you send a request to 127.0.0.1 or localhost, the TCP/IP stack recognizes the loopback address and routes the packet back to the same machine internally. The traffic does not reach the physical network interface (NIC) and does not travel to any router or external network.

This is different from ordinary network traffic, which is sent through a physical or virtual network interface, forwarded by routers, and travels across the network to reach the destination.

On Linux, you can verify the loopback interface is active with the ip command:

Terminal
ip a show lo
output
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host

Test the Loopback with ping

On Linux, you can use the ping command to verify the loopback interface is working:

Terminal
ping -c 4 localhost
output
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.041 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.040 ms

--- localhost ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3075ms

Near-zero response times confirm the traffic is not leaving the machine.

Common Use Cases

Application development and testing
Web servers and development tools bind to localhost or 127.0.0.1 by default when running locally. This makes the application reachable only from the same machine, not from the network. For example, a local web server running on port 8080 is accessed at http://localhost:8080.

Override a domain for testing
You can map a domain name to a loopback address in /etc/hosts to redirect its traffic to a local server. For example, adding the following line routes all requests for example.com to your local machine:

output
127.0.0.1   example.com

This is useful for testing a site locally using its production domain name before deploying.

Block a domain
Mapping a domain to 127.0.0.1 in /etc/hosts effectively blocks it. Any request to that domain is sent to the local loopback, where nothing is listening, so the connection fails silently.

Network diagnostics
Pinging 127.0.0.1 confirms the TCP/IP stack on the local machine is functioning, independent of network hardware or connectivity. If ping 127.0.0.1 fails, there is a problem with the local networking stack itself.

Quick Reference

TaskCommand / Value
Localhost hostnamelocalhost
IPv4 loopback address127.0.0.1
IPv6 loopback address::1
Show hosts mappingcat /etc/hosts
Check loopback interfaceip a show lo
Test loopback reachabilityping -c 4 localhost
Listen on localhost onlyBind service to 127.0.0.1
Listen on all interfacesBind service to 0.0.0.0

FAQ

What is the difference between localhost and 127.0.0.1?
They refer to the same thing in practice. localhost is a hostname that resolves to 127.0.0.1 via the /etc/hosts file. Using 127.0.0.1 skips the hostname resolution step, but the result is identical.

What is 0.0.0.0?
0.0.0.0 is not the same as localhost. When a server binds to 0.0.0.0, it listens on all available network interfaces, including the loopback, LAN, and any other interfaces. When a server binds to 127.0.0.1, it only accepts connections from the local machine.

Can other devices connect to my localhost?
No. Traffic sent to 127.0.0.1 or localhost never leaves the machine. Other devices on the network cannot reach services bound only to the loopback interface. To allow external connections, the service must bind to the machine’s LAN IP or 0.0.0.0.

What is the IPv6 equivalent of localhost?
The IPv6 loopback address is ::1. It behaves the same as 127.0.0.1 for IPv6 traffic.

Why does my application use port 3000, 8080, or 8000 on localhost?
These are common default development ports used by various web frameworks and tools. Port 80 (HTTP) and 443 (HTTPS) require root privileges to bind on most Linux systems, so development servers default to higher port numbers that any user can use.

Conclusion

Localhost points back to the current machine through the loopback interface, usually at 127.0.0.1 for IPv4 and ::1 for IPv6. Use it for local development, testing, domain overrides, and network diagnostics when traffic should stay on the same computer.

To customize how hostnames resolve on your system, see the guide on editing the /etc/hosts file .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page