loopback_address.md (5670B)
1 # Loopback Address 2 3 In Linux system administration and networking fundamentals, a loopback address 4 refers to a special, virtual network interface that allows a computer to 5 communicate with itself [5]. This interface is typically named `lo` or `lo0` on 6 Unix-like systems 7 8 ## IP Addressing and Standards 9 10 The most common IPv4 address used for this purpose is 127.0.0.1, which is the 11 standard address for IPv4 loopback traffic. This address is part of a reserved 12 block of more than 16 million addresses ranging from 127.0.0.0 to 13 127.255.255.255 (127.0.0.0/8) that are designated for loopback functionality. 14 For IPv6, the address ::1 is also reserved for this purpose. Internet 15 Engineering Task Force (IETF) standards reserve the IPv4 address block 16 127.0.0.0/8 and the IPv6 address ::1/128 for this specific use. 17 18 ## Functionality and Behavior 19 20 The primary purpose of the loopback interface is to return the packets sent to 21 it; whatever is sent to it is looped back internally. Traffic destined for a 22 loopback address allows a host to communicate with itself without sending 23 traffic onto any physical network. When an application sends data to 127.0.0.1, 24 the IP stack recognizes this as a loopback address and immediately redirects the 25 traffic internally through the loopback interface. Because of this design, a 26 system that is not connected to any network will still have this loopback device 27 and hence a 127.0.0.0 address. 28 29 ## System Administration and Configuration 30 31 In Linux, the loopback interface is managed by tools like NetworkManager, which 32 assigns the IP addresses 127.0.0.1 and ::1 that are persistent across reboots. 33 Administrators cannot override 127.0.0.1 and ::1, although they can assign 34 additional IP addresses to the interface. The hostname localhost is simply a 35 name that resolves to this IP address and is configured in /etc/hosts. 36 37 ## Usage and Availability 38 39 Common uses for the loopback device include diagnostics, local service testing, 40 and inter-process communication. System administrators and engineers rely on 41 loopback addresses when setting up servers to ensure configurations are correct. 42 While the majority of systems have loopback addresses by default, they typically 43 have zero impact on whether the system is part of a distributed system or not. 44 45 ## Compare between Unix Socket and Loopback Address 46 47 While both methods allow processes on the same machine to communicate, Unix 48 domain sockets are technically superior for performance and resource management 49 due to the lack of TCP/IP overhead and port limitations. However, loopback 50 addresses provide a robust, network-stack-independent method that is often 51 sufficient for testing and applications already designed around TCP/IP 52 protocols. 53 54 ### Resource Usage 55 56 Port Numbers: Using loopback addresses requires binding to local port numbers, 57 which are a limited resource. 58 59 File System: Unix sockets utilize the file system for their endpoints, avoiding 60 the consumption of network ports. 61 62 ### Performance and Overhead 63 64 Latency: Unix domain sockets generally offer lower latency than TCP loopback 65 connections. Benchmarks on modern Linux systems show Unix domain sockets 66 delivering approximately 2-3 microseconds of latency, compared to 3.6 67 microseconds for TCP loopback, representing a 36% improvement. 68 69 Overhead: Unix domain sockets are faster because they avoid the overhead of the 70 TCP/IP stack. Local interprocess communication via Unix domain sockets is faster 71 than loopback localhost connections because there is less TCP processing 72 involved. The loopback device is considered more complicated than Unix sockets, 73 resulting in higher relative overhead for loopback traffic. 74 75 ### Underlying Mechanism 76 77 Unix Sockets: These are inter-process communication (IPC) mechanisms that use 78 local files to send and receive data, rather than network interfaces and IP 79 packets. They function as communication endpoints for data exchange between 80 processes running on the same Unix or Unix-like operating system. 81 82 Loopback Address: This relies on the TCP/IP stack using the loopback interface 83 (typically lo with IP 127.0.0.1). While TCP/IP sockets using the loopback 84 address are a natural way to pass messages between processes on the same host, 85 they still traverse the network stack. 86 87 ### Reliability and Robustness 88 89 NIC Dependency: Communication via loopback addresses is highly robust because 90 the loopback interface (127.0.0.1) does not depend on the physical network 91 interface card (NIC). It remains active regardless of whether the NIC is up, 92 down, or reconfigured. 93 94 Failure Scenarios: If services are configured to communicate via a physical NIC 95 IP address, communication can fail if the NIC goes down or the IP changes. In 96 contrast, using the loopback address ensures communication remains functional 97 even if network hardware fails. 98 99 ### Typical Use Cases 100 101 Loopback: The loopback interface is primarily used for diagnostics and testing 102 local server applications. It is also commonly used for IPC when applications 103 are designed to use TCP/IP sockets (e.g., Java applications). 104 105 Unix Sockets: These are preferred for high-performance local IPC, such as in 106 database configurations (e.g., PostgreSQL performance advice suggests Unix 107 sockets over localhost for local connections). 108 109 ### References 110 111 [UNIX sockets vs. localhost: PostgreSQL performance advice](https://www.cybertec-postgresql.com/en/postgresql-performance-advice-unix-sockets-vs-localhost/) 112 113 ## Why does Docker use 172.x.x.x? 114 115 Docker uses IP ranges starting with 172.x.x.x (specifically 172.17.0.0/16 by 116 default) because that is part of the private IP address space defined in RFC 117 1918, which Docker reserves for internal container networking.