I made an app in java where a client sends ip-packets to a server via the looback-address 127.0.0.1. A really simple app just to test how things work. At first I thought If I created several client-sessions new loppback-hosts would be created. But instead new sockets are created that is 127.0.0.1:portnumber. But then is my question, since the loopback seem to have a prefix of 8, under what circumstances are several loopback hosts used, that is 127.0.0.1, 127.0.0.2, .. 127.255.255.255?
1 Answer
You can use all loopback addresses from 127.0.0.1 upto 127.255.255.254 however you like. According to RFC 3330, they are all "looped back inside the host". Some operating systems respond to all loopback addresses out of the box, on others you have to explicitly define extra addresses on the loopback interface.
For most testing, 127.0.0.1/32 is sufficient, but sometimes multiple IPs can be useful or necessary. Examples include:
- Testing a web server with IP based virtual hosts, serving different web sites on different IPs.
- On some mail systems, mail passes multiple MTAs living on the same system. To clearly distinguish them (eg in the mail headers), they can all be running on different local IP address instead of different TCP ports.
So multiple addresses can be useful, but the decision to allocate over 16 million of them was clearly taken at a time when nobody worried about an address shortage...
Note that IPv6 only has one loopback address (::1/128).
-
2That IPv6 only has one loopback address is kind of/sort of false as IP 4 addresses can be mapped to IP 6 addresses. For example,
::ffff:127.0.0.2
. I don't fully understand the details of 4 to 6 mapping though. Sep 30, 2015 at 18:39 -
Another reason would be to avoid socket exhaustion on very high throughput non-keepalive protocols see for example : nginx.com/blog/overcoming-ephemeral-port-exhaustion-nginx-plus– mverooneJan 7, 2021 at 16:35