Localhost, the Name Your Own Machine Is Told to Answer
Technical
Type localhost into a browser and nothing you asked for leaves the machine. The name need never be looked up, and the address it resolves to is one no router will carry, but those two facts were settled in separate documents, written at different layers for different software. Neither is really a statement about where traffic goes; both are about who is expected to stop the question.
Two reservations, and two kinds of machinery
The number came first. Requirements for Internet Hosts, published by the IETF in October 1989 as RFC 1122, is the document that tells a host’s own IP layer what it may and may not do, and it deals with the loopback range in a single entry. It lists the forms a special IPv4 address can take, gives the range beginning 127 the name “Internal host loopback address”, and attaches the requirement directly: “Addresses of this form MUST NOT appear outside a host.”
Read that as an instruction to your own machine rather than to the network. The same section requires that a host sending a datagram use “one of its own IP addresses” as the source, so the software choosing that field is the thing that has to apply the restriction. Nothing further out is asked to help. It is enforced before anything reaches a cable, by whatever built the packet.
There is a stranger place the same range turns up. The document also says when a host must keep quiet rather than return an error, and it rules out replying to “a datagram whose source address does not define a single host”, listing the loopback address among its examples beside broadcast and multicast addresses. A number that identifies the machine you are sitting at, counted among the addresses that pick out nobody in particular.
Which IPv4 blocks a router will carry and which it is required to drop is a separate question, worked through one block at a time in what the registry records against each one. This post is about something narrower: where the stopping happens, and who does it.
The name is answered without being asked
The word localhost is governed by a separate document, and by nothing in the IP layer at all. Special-Use Domain Names, RFC 6761, was published in February 2013 to create a register of names that software is expected to treat differently, and what makes it useful here is the form it makes every applicant fill in. Any specification reserving such a name must supply a subsection “giving answers in the seven categories listed below”, and those seven are the parties that would otherwise pass a question along: users, applications, name resolution libraries, caching servers, authoritative servers, server operators and the registries that sell names.
The questions are blunt about what they are for. Writers of applications are asked whether, “if a human user enters such a name, should the application software reject it with an error message?” Operators get a similar one: “will compliant name server software reject it as invalid?” Read the form rather than the answers and the hazard it was built against is visible: a name carried outward by software with no reason to know better.
Then come the answers for this one. Users “may assume that IPv4 and IPv6 address queries for localhost names will always resolve to the respective IP loopback address”. The short circuit itself sits one step further in: name resolution libraries should “always return the IP loopback address for address queries and negative responses for all other query types”, and should not “send queries for localhost names to their configured caching DNS server(s)”.
Every layer behind that one is told the same thing again. A caching server should not “attempt to look up NS records for them, or otherwise query authoritative DNS servers in an attempt to resolve localhost names”, and should instead “generate an immediate positive response giving the IP loopback address”. The stated reason is housekeeping rather than privacy: “This is to avoid unnecessary load on the root name servers and other name servers.”
Two answers close the door from the far side. Operators are told that “the effective RDATA for localhost names is defined by protocol specification and cannot be modified by local configuration”, so the value is not theirs to change, and registrars must not “grant requests to register localhost names in the normal way to any person or entity”. The path a typed name takes when it is not reserved, from a library out to a recursive server and up to the root, is followed end to end in the chain a question normally climbs. A localhost name is specified never to enter it.
An interface that was never there
IPv6 restates the reservation in its own vocabulary, and the wording is worth reading closely because it gives up on the idea of a place. IP Version 6 Addressing Architecture, RFC 4291, carries a February 2006 date and the Standards Track label. “The unicast address 0:0:0:0:0:0:0:1 is called the loopback address,” it says, and “It may be used by a node to send an IPv6 packet to itself.”
Then it takes away the hardware. The address “must not be assigned to any physical interface”, and the specification offers instead a picture with nothing physical in it: the address may be thought of as belonging to a virtual interface attached to “an imaginary link that goes nowhere”. An address is normally pictured as a thing on a wire somewhere. This one is defined by the absence of both.
That has a consequence worth drawing out. Because the address belongs to no interface, a packet bearing it that turns up on a real one is not a delivery, and the rule is flat: “A packet received on an interface with a destination address of loopback must be dropped.” The same document lists the addresses a host has to treat as identifying itself, and the loopback address is among them, so a host is required both to answer to it and to destroy anything bearing it that arrives from outside.
Reaching a service is a question about direction
The reservation holds on every machine, so the number tells you nothing about which machine. 127.0.0.1 on the laptop in front of you and 127.0.0.1 on a server in a rack are both correct and they mean different hardware, so a service sitting at that address is not somewhere anyone else can be directed to. There is nothing for an outsider to aim at.
That turns a question about reach into a question about where a request starts. Before anything leaves, a stack decides whether it can reach the destination directly or has to pass the packet to a router, and the second answer leads to the one place everything else is sent instead. A loopback destination never gets as far as that decision. It resolves to this node, and the rules above require that a packet arriving from elsewhere with that destination be destroyed rather than delivered.
The practical form of that is a habit worth having: something answering on a machine is not the same as something being reachable from the network, and the way to tell them apart is to start the request on the other side. The tool on this site that does exactly that says in its own copy that it never accepts a typed-in address, so a connection attempted from outside, against the address you are already using measures the direction that matters. Where nothing answers, that page is careful to separate a real negative from a run it could not complete at all.
Addresses a page can discover on its own
One thing does not follow from any of this. An address belonging to a machine is not automatically an address nobody else can learn, and loopback is the exception rather than the pattern. A private address on the local network is held by the same machine, means something only inside that network, and can still be surfaced by code running in a page, which is a different kind of exposure from anything in the documents above.
That gap is what which of a machine’s own addresses a page can discover for itself exists to show, by setting whatever it finds against the address websites are already being given. A private network address turning up there is not an identity, as the page says plainly, though it is detail a site did not have to be handed.
Three documents, three audiences, one arrangement. An IPv4 requirement from 1989 tells a host’s own stack not to put the address on a wire, an addressing specification from 2006 denies it any hardware to sit on, and a naming register from 2013 tells every piece of software from the application outward to the root not to ask about the word. None of them builds a tunnel, encrypts anything or hides you. They agree only on where a particular question should stop, and each of them stops it somewhere different.