On a FreeBSD system with a loopback network interface …
% ifconfig lo0 lo0 link up loopback drv_running running multicast nd6 performnud auto_linklocal no_radr link rxcsum txcsum hwcsum rxcsum_ipv6 txcsum_ipv6 link address metric 0 mtu 16384 type 24 linkstate 0 physical 0 baudrate 0 inet4 address 127.0.0.1 prefixlen 8 bdaddr 127.0.0.1 inet6 address ::1 scope 0 prefixlen 128 bdaddr ::1 scope 0 inet6 address fe80::1 scope 3 prefixlen 64 inet6 address ::2 scope 0 prefixlen 128 inet4 address 127.53.0.1 prefixlen 8 bdaddr 127.53.0.1 inet4 address 127.53.1.1 prefixlen 8 bdaddr 127.53.1.1 %
… pinging the IP address 127.0.0.2, which is not assigned to that (or to any other) network interface results in failure:
% ping -c 1 127.0.0.2 PING 127.0.0.2 (127.0.0.2): 56 data bytes ping: sendto: Network is unreachable --- 127.0.0.2 ping statistics --- 1 packets transmitted, 0 packets received, 100.0% packet loss %
The same is true for OpenBSD:
% ping -c 1 127.0.0.2 PING 127.0.0.2 (127.0.0.2): 56 data bytes ping: sendto: Network is unreachable ping: wrote 127.0.0.2 64 chars, ret=-1 --- 127.0.0.2 ping statistics --- 1 packets transmitted, 0 packets received, 100.0% packet loss %
But on Linux where there is similarly no 127.0.0.2 configured …
% ifconfig lo lo link up loopback running link address 00:00:00:00:00:00 bdaddr 00:00:00:00:00:00 inet4 address 127.0.0.1 prefixlen 8 bdaddr 127.0.0.1 inet4 address 127.53.0.1 prefixlen 8 bdaddr 127.255.255.255 inet6 address ::2 scope 0 prefixlen 128 inet6 address fe80:: scope 1 prefixlen 10 inet6 address ::1 scope 0 prefixlen 128 %
… the ping is surprisingly successful:
% ping -c 1 127.0.0.2 PING 127.0.0.2 (127.0.0.2) 56(84) bytes of data. 64 bytes from 127.0.0.2: icmp_seq=1 ttl=64 time=0.044 ms --- 127.0.0.2 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.044/0.044/0.044/0.000 ms %
How come?
Related questions
- "When does an IP address not need to be assigned to a network interface?" — the very roundabout question, asking for an explanation of "virtual interfaces", that this question asks straightforwardly
- "How is it possible to send ping to all 127.0.0.0/8 successfully?" — several questions in one, rather than one question per question, and not in fact answered by the purported duplicate
- "why such behavior in Linux Network over Loopback Interface" — a question about why on Linux the
lo
interface seems to also control the pingability of IP addresses that are assigned to other network interfaces (behaviour that is similarly not what one gets on the BSDs) - "What happens to local LAN traffic on linux to non-loopback addresses?" — a question about non-loopback behaviour
Other references
- Jonathan de Boyne Pollard (2019).
ifconfig
. nosh Guide. Softwares.
Best Answer
When an address is added to an interface, a local route is automatically added for the address. Additionally, when an address is added to a loopback interface (
IFF_LOOPBACK
), a local route is added for the network prefix of the address. In Linux 5.0, you can find this implemented in fib_add_ifaddr().In contrast, when an address is added to a non-loopback interface, a unicast route is added for the network prefix.
The local routes are added to a specific table called "local". The most obvious significance of this is that a quick
ip -4 route
will not show the local table. This is because it is equivalent toip -4 route show table main
.linux-ip.net has some broader information. It mentions two other uses of the local table.
The above will likely apply to any version of Linux that you encounter. I am not sure exactly how far back it goes. However the
ip route
command is already ancient; it goes back to at least the days of Linux 2.2.