Localhost is (sometimes) a network
Everyone knows localhost
, 127.0.0.1, the IP(v4) address of every
machine's loopback network. If you're talking to 127.0.0.1, you're
talking to yourself (machine-wise). Many of us (although not all)
know that localhost is not just a single IP address; instead, all
of 127.*.*.* (aka 127/8) is reserved as the loopback network.
ifconfig
will tell you about this on most machines:
lo0: flags=[...] inet 127.0.0.1 netmask ff000000
You don't see that many 0's in a netmask all that often.
One of the tricks that you can play here is to give your loopback network more 127.* IP aliases. Why would you want that? Well, suppose that you have two things that both want to run localhost web servers. With 127.* IP aliases, one can be http://127.0.0.1/ and the other can be http://127.0.0.2/, and both can be happy. Localhost IP aliases can also be a convenient way to invent additional source addresses for testing. Need to connect to something on the same machine from ten different source IPs? Just add 127.0.100.1 through 127.0.100.10, then have your software rotate among them as the source addresses. It's all still loopback traffic, so all of the usual guarantees apply; it just has some different IPs involved.
Sometimes, though, we mean that loopback is a network in a more literal way. Specifically, on Linux:
bash $ ping 127.0.$(($RANDOM % 256)).$(($RANDOM % 256)) PING 127.0.251.170 (127.0.251.170) 56(84) bytes of data. 64 bytes from 127.0.251.170: icmp_seq=1 ttl=64 time=0.055 ms [...]
On at least FreeBSD, OpenBSD, and OmniOS, this will fail in various ways. On Linux, it works. Through black magic, you don't have to add additional 127.* IP aliases in order to use those addresses; although not explicitly declared anywhere as existing, they are just there, all 16,777,215 or so of them. The various localhost IPs are all distinct from each other as usual, so a service listening on some port on 127.0.0.1 can't be reached at that port on 127.0.0.2. You just don't have to explicitly create 127.0.0.2 before you can start using it, either to have a service listen on it or to have a connection use that as its source address.
(And anything that listens generically can be reached on any 127.* address. You can ssh to these random localhost IPs, for example.)
PS: Before I tested it as part of writing this entry, I thought that the Linux behavior was how all Unixes worked. I was a bit surprised to find otherwise, partly because it makes the Linux behavior (much) more odd. It is kind of convenient, though.
Comments on this page:
|
|