What Is localhost? 127.0.0.1 and Loopback Explained

By 

•

Updated on

•

9 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         localhost 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. Some systems also resolve localhost through an NSS module such as nss-myhostname. 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.

Localhost and Port Numbers

A hostname alone is not enough to reach a service. The address localhost identifies the machine, and the port number identifies which program on that machine receives the connection. That is why local development URLs look like http://localhost:8080 rather than just http://localhost.

When you open http://localhost with no port, the browser fills in the default port for the scheme, which is 80 for http and 443 for https. A development server listening on port 8080 is not reachable that way, and the browser reports that the connection was refused.

To see which programs are listening for TCP connections and which addresses and ports they use, run the ss command:

Terminal
sudo ss -tlnp
output
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port
LISTEN   0        4096         127.0.0.1:8080             0.0.0.0:*      users:(("node",pid=2841,fd=21))
LISTEN   0        4096         127.0.0.1:5432             0.0.0.0:*      users:(("postgres",pid=1044,fd=7))
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*      users:(("sshd",pid=445,fd=3))
LISTEN   0        511                    *:80                   *:*      users:(("nginx",pid=1190,fd=6))

The Local Address:Port column is the part to read. A value of 127.0.0.1:8080 means the service accepts connections only from the same machine, so http://localhost:8080 works while a request from another device does not. A value of 0.0.0.0:22 means the service listens on every IPv4 interface. Depending on the system, *:80 can represent a wildcard listener for one address family. These services may accept connections from other devices, but actual reachability also depends on the firewall, routing, and network configuration. The guide on checking listening ports covers the netstat and lsof equivalents.

On a default Linux system, a process needs the CAP_NET_BIND_SERVICE capability to bind to a port below 1024. Root normally has this capability, but it can also be granted to a non-root service, and the privileged-port threshold is configurable. This is one reason development servers default to higher numbers such as 3000, 5000, 8000, and 8080.

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 keeps requests for it on the local machine, but a local service listening on the requested port may still answer. Some blocklists use 0.0.0.0 as a shortcut, but it is the unspecified address rather than a loopback address, and clients do not handle it consistently. For dependable blocking, use DNS filtering or a browser-level blocker.

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.

Troubleshooting

localhost refused to connect
Nothing is listening on the address and port you requested. Confirm the service is running, then check what it actually bound to with sudo ss -tlnp. A service listening on a different port than the one in your URL produces this error even though the process is running normally.

The service answers on 127.0.0.1 but not on localhost
localhost can resolve to both ::1 and 127.0.0.1, and clients that follow the system resolver order try the IPv6 address first. When the service bound only to 127.0.0.1, that first attempt is refused, and clients that do not fall back to IPv4 stop there. Check the order your resolver returns:

Terminal
getent ahosts localhost
output
::1             STREAM localhost
::1             DGRAM
::1             RAW
127.0.0.1       STREAM localhost
127.0.0.1       DGRAM
127.0.0.1       RAW

Either configure the service to listen on ::1 as well, or use 127.0.0.1 explicitly in the URL instead of localhost.

bind: address already in use
Another process already holds that port on the same address. Note that 127.0.0.1:8080 and 0.0.0.0:8080 conflict, because 0.0.0.0 covers the loopback address too. See how to find which process is using a port to identify and stop it.

A new /etc/hosts entry has no effect
The file is read for new connections, but browsers and long-running applications cache resolved names. Restart the client, and on systems running systemd-resolved flush the cache with sudo resolvectl flush-caches. Check also for duplicate entries that map the same hostname to different addresses. A resolver may return multiple addresses, and the application can choose one you did not expect.

ping localhost reports an unknown host
Check the result of getent hosts localhost first. Linux can resolve localhost through /etc/hosts or an NSS module such as nss-myhostname, so this error can mean the hosts entry is missing or the resolver order in /etc/nsswitch.conf is misconfigured. If your system uses the files source and the mapping is absent, restore 127.0.0.1 localhost. You can also test with ping -c 4 127.0.0.1 to skip name resolution. If the address itself is unreachable, check that the loopback interface is up with ip a show lo and look for UP in the flags.

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?
Both point back to the local machine, but they are not identical. localhost is a hostname that the system resolver can map to the IPv4 address 127.0.0.1, the IPv6 address ::1, or both. Using 127.0.0.1 skips hostname resolution and selects IPv4 directly.

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. On a default Linux system, ports 80 (HTTP) and 443 (HTTPS) require the CAP_NET_BIND_SERVICE capability. Higher port numbers do not require that capability, so development servers can run under an unprivileged user.

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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page