What is localhost?

Localhost is the name a computer uses for itself. Traffic sent to it never leaves the machine, which makes it the safest place for services that only local programs should reach.

Which addresses does localhost use?

127.0.0.1 for IPv4 and ::1 for IPv6. In fact the whole 127.0.0.0/8 block, over sixteen million addresses, is reserved for loopback, so 127.0.0.53 or 127.1.2.3 also point at the local machine. Linux attaches them to the lo interface, a virtual network device with no cable behind it. Packets sent there are handed straight back to the receiving side of the kernel network stack, which makes loopback extremely fast.

ip -brief address show dev lo
getent ahosts localhost
ping -c2 127.20.30.40

How is the name localhost resolved?

Usually from /etc/hosts and increasingly by rule. The hosts file maps localhost to 127.0.0.1 and ::1, and current standards say resolvers should always answer localhost names with loopback addresses without asking DNS at all. On systems with systemd-resolved, names ending in .localhost also resolve to loopback, which is handy for testing several local sites. Some distributions also map the machine's own host name to 127.0.1.1, which can surprise software that expects the public address.

Why bind services to localhost on a server?

So nobody outside can reach them, whatever the firewall does. A database, cache or admin panel listening on 127.0.0.1 is invisible to the internet by construction. A reverse proxy on the same machine forwards public requests to the application on loopback, and only the proxy listens publicly. This is the first rule of exposing services safely, and it removes a whole class of breaches caused by a firewall rule forgotten during maintenance.

ServiceSetting that binds to loopback
PostgreSQLlisten_addresses = 'localhost'
MySQL and MariaDBbind-address = 127.0.0.1
Redis and Valkeybind 127.0.0.1 -::1
Memcached-l 127.0.0.1
Docker published port-p 127.0.0.1:8080:8080

How do I check what listens publicly?

List listening sockets and look at the local address column. Anything on 0.0.0.0, * or [::] listens on every interface, including the public one; anything on 127.0.0.1 or ::1 is local only:

ss -ltnup | awk '{print $1, $5, $7}' | column -t

Our Secure Memcached guide shows the consequences of getting this wrong for one common service.

How can I reach a loopback service from my laptop?

Through an SSH tunnel. Local port forwarding maps a port on your laptop to a loopback port on the server, carried inside the encrypted SSH session, so you can use a database client or admin panel without opening any public port:

ssh -N -L 15432:127.0.0.1:5432 ops@db1.example.net
# then connect locally
psql "host=127.0.0.1 port=15432 dbname=billing user=reporting"

For permanent access by several people, WireGuard gives a private interface on the server that services can listen on instead. Our SSH basics guide covers tunnels further.

What are common localhost pitfalls?

Does localhost traffic count against bandwidth?

No. Loopback traffic never touches the network card or the data centre network, so it is unlimited apart from CPU and memory bandwidth. Talking to a database over a Unix socket is slightly faster still. That is one reason a single powerful server, such as ESSENTIAL-M with 12, can run an application, its database and its cache side by side very efficiently. Our IP address article covers the public addresses that do face the network.

Can I give local services friendlier names?

Yes, and it helps when several applications share one machine. Add entries such as 127.0.0.10 billing.internal to /etc/hosts, bind each service to its own loopback address, and the configuration files read clearly while every port stays private. Because the whole 127.0.0.0/8 block is local, two services can even use the same port number on different loopback addresses without conflict, which is handy when testing two versions of an application side by side.

Related pages

IP address

How IPv4 and IPv6 addresses identify a machine on the internet, how prefixes and reverse DNS work, and what a rented server receives.

Read more

SSH basics

What SSH is, how to connect from any computer, run commands, keep sessions alive, forward ports and copy files, for people new to servers.

Read more

Secure Memcached

Keep a Memcached cache private: bind to localhost, turn off UDP, use SASL or a tunnel for remote clients and size memory for the workload.

Read more

What a firewall does

Firewalls explained from packets up: stateful filtering, default-deny policies, host and network firewalls, nftables examples and mistakes that lock you out.

Read more

Questions and answers

What IP address is localhost?

127.0.0.1 for IPv4 and ::1 for IPv6. The whole 127.0.0.0/8 range is reserved for loopback.

Can other computers connect to localhost on my server?

No. Services listening only on loopback addresses can be reached only by programs running on the same machine.

How do I reach a database bound to localhost from my computer?

Use an SSH tunnel with local port forwarding, or a WireGuard interface on the server, instead of opening the database port publicly.

What does localhost mean inside a Docker container?

The container itself. To reach a service on the host, use the bridge gateway address or run the container with host networking.

Why does my app fail to reach a database on localhost?

Often because the client resolves localhost to ::1 while the database listens only on 127.0.0.1. Use the explicit address or make the database listen on both.