|This article needs additional citations for verification. (December 2013)|
In computer networking, localhost means this computer. It is a hostname that the computer's software and users may employ to access the computer's own network services via its loopback network interface. On most computer systems, localhost resolves to the address 127.0.0.1, which is the most-commonly used IPv4 loopback address, and to the IPv6 loopback address ::1. Using the loopback interface bypasses local network interface hardware.
The local loopback mechanism is useful for testing software during development independent of any networking configurations. If a computer has been configured to provide a website, directing its web browser to http://localhost may display its home page.
The name is also a reserved top-level domain name (cf. .localhost), set aside to avoid confusion with the narrower definition as a hostname. The registrars of many second-level domains also prevent registration.
IPv4 network standards reserve the entire 127.0.0.0/8 address block for loopback purposes. That means any packet sent to one of those 16,777,214 addresses (127.0.0.1 through 127.255.255.254) will be looped back.
The resolution of the name 'localhost' to the most-commonly used loopback addresses is set up by the following lines in each device's hosts file:
127.0.0.1 localhost ::1 localhost
Localhost can also be mapped to other IPv4 loopback addresses, and additional synonyms can be assigned to any loopback address.
The processing of any packets sent to a loopback address is implemented in the link-layer of the TCP/IP stack. Such packets are never delivered to any network interface controller (NIC) or device driver, which absolves the operating system from choosing one in the event the device is equipped with multiple NICs.
Like any other packets traversing the TCP/IP stack, looped-back packets convey the IP address and port number they were addressed to. Thus, the services that ultimately receive them can respond differently based on the specified loopback destination. For example, an HTTP service could route packets addressed to 127.0.0.99:80 and 127.0.0.100:80 to different HTML servers, or to a single server that would return different web pages. To simplify such testing, the hosts file can be modified to provide appropriate aliases to each such address.
The name localhost is reserved for loopback purposes by RFC 6761 (Special-Use Domain Names), which achieved the Proposed Standard maturity level in February, 2013. That RFC sets forth a number of special considerations governing the use of the name in the Domain Name System namespace:
- An IPv4 or IPv6 address query for the name localhost must always resolve to the respective loopback address, which is specified in a separate standard.
- Programs can resolve that hostname to such a loopback address themselves, or pass it to the host's address resolution APIs.
- When a device's hostname resolver receives a query for localhost, it should return the appropriate loopback addresses for A and AAAA record requests, and negative responses for any other requested records.
- To avoid burdening the Internet's Domain Name System with pointless traffic, no DNS server should ever forward any localhost record request. If any such server does receive such a request, it should immediately return the appropriate loopback address (A or AAAA record request), or a negative response to any other request.
- DNS registrars are precluded from registering domain names that include localhost (e.g., localhost.com), in order to avoid the confusion that would result if a DNS lookup appended a specified search domain localhost, when it was meant to loop back.
The IPv4 loopback addresses are separately reserved within the IPv4 namespace by the current IETF Special Use IPv4 Addresses standard (RFC 5735). That reservation can be traced back to the November, 1986 Assigned Numbers standard (RFC 990), which was issued at a time when the first byte of the IP address was a signed network number that was the only portion of the address assigned by the IETF. So reserving its highest possible network number (127) for loopback testing eventually morphed into an excessive reservation of 16,777,214 loopback addresses.
In contrast, the current IETF IP Version 6 Addressing Architecture standard (RFC 4291) reserves a single IPv6 loopback address within the IPv6 namespace. That address is ::1. That standard precludes the assignment of that address to any physical interface, as well as its use as the source or destination address in any packet sent to remote hosts. Any such packet that is erroneously transmitted is not supposed to be routed, and should be dropped by all hosts that receive it.
One notable exception to the use of the 127.0.0.0/8 network addresses is their use in Multiprotocol Label Switching (MPLS) traceroute error detection, in which their property of not being routable provides a convenient means to avoid delivery of faulty packets to end users.
The public releases of the MySQL database differentiate between the use of the hostname localhost and the use of the addresses 127.0.0.1 and ::1. When using localhost from a client program (e.g., a PHP application), the MySQL application programming interface connects to the database using a Unix domain socket, while a TCP connection via the loopback address requires the direct use of the address.
Any IP datagram with a good source or destination address set to a loopback address must not appear outside of a computing system, or be routed by any routing device. Packets received on an interface with a loopback destination address must be dropped. Such packets are sometimes referred to as Martian packets As with other bogus packets, they may be malicious and any problems they might cause can be avoided by applying bogon filtering.
- "RFC4291: IP Version 6 Addressing Architecture". Section 2.5.3: IETF.
- "RFC2606: Reserved Top Level DNS Names". Section 2: IETF.
- "RFC6761: Special-Use Domain Names". Section 6.3: IETF.
- "RFC5735: Special Use IPv4 Addresses". Section 4: IETF.
- Reynolds and Postel. "RFC990: Assigned Numbers". page 5: IETF.
- Raymond, Eric S. "The Jargon File".