What is localhost, 127.0.0.1 and loopback-address ? Hint: Different ways to reference your computer

tldr: the dns name Localhost or ip address 127.0.0.1 is a reserved network address that always mean YOUR OWN COMPUTER/HOST/NODE, these addreses are assigned to the loopback interface. Localhost or 127.0.0.1 are by default inaccessible by external hosts and only accessible internally or through port-forwarding. This networking feature is well defined in international standards.

Imagine if your parents never gave you a name, what would you be called? Probably “person”. You would be able to reference yourself as a person but you would be nameless.

  • Localhost - Dns
  • 127.0.0.1 - ip address
  • Loopback - interface for both addresses, the ip variant and dns variant

The localhost or 127.0.0.1 address is somewhat similar to this analogy in that its something all networked nodes have and can be identified by but its not the name of the node. The localhost works as default DNS name that translates to the network address 127.0.0.1 which is bound on the loopback interface. The DNS name Localhost was defined in RFC2606 by IETF,

network interface = a physical port on a network interface card

logical network interface = a network interface that does not exists physically but exists logically or “virtually” in software as a software construct.

IP protocol = a protocol used for logical addressing to identify hosts in a hierarchical way.

All computers have a host file which maps names to ip addresses, and works like a very basic dns lookup table. The first line in the host file look the same on all linux based systems and maps the ip address 127.0.0.1 to the reserved DNS name called “localhost”. Localhost is a reserved name because it is defined in international standards documents.

Even if your computer has the ip address 192.168.0.9 or any other ip address on your network/domain your internal ip address will always be 127.0.0.1, no other computer is able to connect to your localhost or 127.0.0.1 address unless you do port forwarding. All computers have the localhost or 127.0.0.1 address assigned to a loopback interface which enables your computer to talk to itself, something that is useful when running services on your host.

Each ip address is able to host over 65000 ports or services. The localhost address and its ip counterpart is no exception and its possible to host one service on port 1337 and another on 13337 for example. There are a bunch of reserved port ranges for well known protocols like SSH port 22.

The loopback interface is the virtual interface that is used to identify your own host in the ip stack and has the mac address 00:00:00:00:00:00 in linux.

If a system administrator does not add a hostname to their host that system is considered nameless and the localhost name is used to identify the computer when logged in on the terminal/shell, nameless computers without hostnames are generally not considered ‘network ready’ because they cant be uniquely identified on the network as mandated by a fully qualified domain name in the DNS spec.

The hostname program in Linux enables setting a unique name for your server/host. This name can then be used to identify your host in a domain name setting. hostname.domain.tld, the host + domain + tld = FQDN (fully qualified domain name)

Example, tunnel traffic between two computers (localhosts) to use services on the remote host.

SSH -L 127.0.0.1:5900:127:0.0.01:5901 root@192.168.0.1

# command breakdown
SSH = specifies the ssh binary 
-L = Local tunneling
127.0.0.1:5900 = localhost port 5900 on local host, YOUR COMPUTER, meaning YOU REFERENCE YOUR OWN COMPUTERS PORT 5900
: = delimiter for port forwarding 
127:0.0.01:5901 = localhost port 5900 on remote host, THE SERVER, meaning this is the "servers" own localhost 
root@192.168.0.1  = the server that acts as the intermediary and is the localhost 5901 

FAQ

  • Do layer 2 switches have loopback interfaces?

sources:

note to self: we talk about three distinct things here, try to seperate them more.

another way to expalin is that YOU = localhost