Contrary to other interfaces, when the Linux kernel adds an address to the loopback interface, its LAN route as also added as type local
and scope host
(these are what matter) to the local routing table (this is what makes it harder to find), instead of the main routing table.
To check the difference, check all routing tables entries (table all
) that were added by the kernel (proto kernel
) and are of type local
.
From a system with only lo
up and nothing else done:
# ip route show proto kernel type local table all local 127.0.0.0/8 dev lo table local scope host src 127.0.0.1 local 127.0.0.1 dev lo table local scope host src 127.0.0.1 #
Here all addresses in the range 127.0.0.0/8 belong to the host (scope host
).
Create dummy0
and an address on it:
# ip link add name dummy0 up type dummy # ip addr add 192.0.2.2/24 dev dummy0 # ip route show proto kernel type local table all local 127.0.0.0/8 dev lo table local scope host src 127.0.0.1 local 127.0.0.1 dev lo table local scope host src 127.0.0.1 local 192.0.2.2 dev dummy0 table local scope host src 192.0.2.2 # ip route 192.0.2.0/24 dev dummy0 proto kernel scope link src 192.0.2.2 #
Instead add it on lo
# ip addr flush dev dummy0 # ip addr add 192.0.2.2/24 dev lo # ip route show proto kernel type local table all local 127.0.0.0/8 dev lo table local scope host src 127.0.0.1 local 127.0.0.1 dev lo table local scope host src 127.0.0.1 local 192.0.2.0/24 dev lo table local scope host src 192.0.2.2 local 192.0.2.2 dev lo table local scope host src 192.0.2.2 # ip route #
This time 192.0.2.0/24 is of scope host
, making the whole range belong to the host just as 127.0.0.0/8, instead of only 192.0.2.2 (/32) in the previous results. Linux following the Weak Host Model, will answer for these addresses for requests made on any interface, including ARP requests when relevant.
There are multiple ways to obtain the same results. So what can or should be used depends on the goal to achieve. pro and con below aren't always pro and con, but that's to compare them.
dummy0
lo
pro
add a whole range of host addresses in one swoop
This should be reserved to projects related to intercepting traffic on a large scale. Note that what matters is the route added by the kernel, not the address itself. Such route is used for example by Linux' tproxy for transparent proxying:
# ip rule add fwmark 1 lookup 100 # ip route add local 0.0.0.0/0 dev lo table 100
but with specific policy-routing rules in place so it doesn't always apply.
Outside of this, just add /32 addresses when using lo
if it's ever done.
permanent interface, neutral role
This interface is guaranteed to exist, so can be used as an anchor to add addresses in advance without knowing what interface will exist or should be used. The routes related to this address won't change when an interface goes down, since it's on lo
(which has no reason to go down). Some routing daemons might handle in a special way addresses added on the loopback interface.
con