96

I wondered what is the origin of the decision to make localhost's IP address 127.0.0.1. What is the "meaning" of 127? what is the "meaning" of 0.0.1?

7
  • 3
    I'm curious about this too. The IPv6 loopback is 0:0:0:0:0:0:0:1, which makes sense to me.
    – hyperslug
    Aug 29, 2009 at 6:11
  • 1
    Yes, IPv6's localhost does not raise too many questions :)
    – Roee Adler
    Aug 29, 2009 at 6:54
  • 22
    I haven't seen a "There's no place like 0:0:0:0:0:0:0:1" door mat yet! Sep 4, 2009 at 23:10
  • 10
    @Wil Compress it to "there's no place like ::1" and you'll get a bit more business.
    – new123456
    May 4, 2011 at 12:08
  • 8
    @WilliamHilsum expand it to "There's no place like 0000:0000:0000:0000:0000:0000:0000:0001" and charge more.
    – Nick T
    Apr 20, 2013 at 6:44

6 Answers 6

92

127 is the last network number in a class A network with a subnet mask of 255.0.0.0. 127.0.0.1 is the first assignable address in the subnet. 127.0.0.0 cannot be used because that would be the wire number. But using any other numbers for the host portion should work fine and revert to using 127.0.0.1. You can try it yourself by pinging 127.1.1.1 if you'd like. Why they waited until the last network number to implement this? I don't think it's documented.

3
  • 16
    The only reasons I would see them arbitrarily choosing 127 is that it's an easy number to remember (01111111), and perhaps they were allowing 16 million host addresses for being able to communicate with itself and itself only (like how some programs and Windows components use ports nowadays). The RFCs only really mentioned that it's standard practice for 127.0.0.1/32 to be used for loopback. It's quite nebulous as to what they intended for the rest of the block aside from it looping back to the host and never hitting the network, hence my above speculation. Aug 29, 2009 at 23:27
  • 3
    Having all the block addresses reverting to 127.0.0.1 is implementation dependent and might perhaps just be a Linux stack peculiarity. Solaris at least requires an interface to be bound to the destination address for your ping test to succeed.
    – jlliagre
    Jan 20, 2011 at 1:22
  • The two answers below by Joseph Bui and Dario Fumagalli are brilliant at describing why 127 was chosen. In fact, I don't think it's fair to say 127 was chosen as much as it was its destiny to be used due to its specific properties in the binary world. Feb 15 at 19:59
63

Earliest mention I can find regarding 127's assignment as loopback is November 1986 RFC 990 authored by Reynolds and Postel:

The address zero is to be interpreted as meaning "this", as in "this network".

For example, the address 0.0.0.37 could be interpreted as meaning host 37 on this network.

...

The class A network number 127 is assigned the "loopback" function, that is, a datagram sent by a higher level protocol to a network 127 address should loop back inside the host. No datagram "sent" to a network 127 address should ever appear on any network anywhere.

Even as early as September 1981 RFC 790, 0 and 127 were already reserved:

000.rrr.rrr.rrr                 Reserved                     [JBP]
...
127.rrr.rrr.rrr                 Reserved                     [JBP]

0 and 127 were the only reserved Class A networks by 1981. 0 was used for pointing to a specific host, so that left 127 for loopback.

I know this doesn't answer the question, but this is as far back as I could dig. It might have made more sense to choose 1.0.0.0 for loopback but that was already given to BBN Packet Radio Network.

29

The designers of the Internet really knew how the hardware worked, and they designed with low level implementation in mind.

The values 0, 127 and 255 are special in 8 bit assembly and machine language programming because there are "tricks" you can use to test for these values and branch to different code using smaller instructions that execute faster than for other integers. 127 is the highest signed 8 bit integer, so incrementing it by 1 will cause a signed overflow. Similarly, incrementing 255 will cause unsigned overflow. Merely loading the value 0 into a register will usually set a zero flag on the chip. Imagine the networking program looks like this in pseudocode:

if (value == 0) doLocal();
if (value == 127) doLoopback();
if (value == 255) doNetwork();

Although it depends on the chip, in those days most chips could encode these tests with 2 words, 3 words and 3 words respectively (total 8 words) and further those particular tests were all likely to execute in 1 clock cycle each. Using any other value would probably require 4 words each (total 12 words), a 50% increase in code size and likely a 50% increase in execution time as well.

5
  • 2
    Note that I used "words" because old machines back then had unusual word sizes, but they often still used 8 bit arithmetic. For an 8088, I would have said "bytes."
    – Joseph Bui
    Apr 15, 2011 at 15:39
  • This is a great example of premature optimization. Compared to all the other latencies needed for network communication, the ability to avoid a single compare operator on a given hardware was pretty poor cause for messing the address space to allocate 16 million addresses to localhost use only, from the middle of possible 32 bit address space. Localhost could have been only 1.1.1.1 just as well. However, that would have prevented Cloudflare from running global DNS service in that IP address later... Jan 26, 2023 at 12:21
  • A great example of optimization, yes. Premature? No. Read this description of how IPv4 was defined by direct competition in RFC 1025 datatracker.ietf.org/doc/rfc1025
    – Joseph Bui
    Jan 31, 2023 at 0:31
  • Selecting specific magical binary values for a new protocol because it happened to have the best performance on a single CPU architecture? Yes, it does check the premature optimization box in my books. Jan 31, 2023 at 13:25
  • Mikko, it seems like we have endless amounts of resources now, but it's not like assembly language disappeared. Think about the importance of efficiency here. How many data packets do you suppose pass through the internet daily? I don't know what comes after decillion (trillion, quadrillion,... decillion) but I bet the number is a lot higher than that. Doesn't it seem like this little tweak of efficiency is part of the reason the internet is as fast and durable as it is? Take that out and the cost of running the internet to give us the performance we're used to would be unaffordable. Feb 15 at 20:14
12

Because when those standards were created, computers were slow and usually limited to 8 bit registers. Comparing numbers to numbers was very slow, expecially if those numbers had to be fetched from the back then REALLY slow memory. Registers, that is "CPU on board" storage were much faster.

Moreover those old computers had special, faster instructions to detect "equal to zero", "different than zero", "negative / positive integer" (where the sign was... guess what, the leftmost bit, now see a connection with 127, that is the number with all binary "1" except the leftmost = sign?).

Therefore those were special numbers, because they allowed programming trickery to save a lot of CPU cycles on frequently done operations.

You'd never see a "IF CallerIP = "0" but a "IF NotZero(CallerIP)" instruction.

You could go check ancient assembly instructions like "BEQ, BNE" (6502 CPUs) for longer explanations. Also check this page out.

In the end:

0, 255 and 127 could all be checked for with an one, fastest, instruction. Even more high level languages like C have "shortuct" comparison functions that the compiler may internally optimize into a one instruction.

Years 70 and 80 programmers really produced magnificent architectures with super-scarce resources, behind standards like IP numbering there's a lot of thought and genius.

3
  • 3
    Great answer. This is really the clearest and most sensible explanation.
    – not2qubit
    Dec 29, 2017 at 14:43
  • Actually, back when IP was designed and some of the early groundwork for it was laid down, working with data in integer multiples of 8 bits wasn't a given. Many architectures at that time had registers and word sizes in multiples of 12 or 18 bits, for example. This is one reason why octal was so popular at the time; 18 bits can be represented as exactly 6 octal digits with no loss and no waste; 12 bits is 4 octal digits. Microcomputers typically worked in 8-bit quantities, but it was only much later that microcomputers started being regularly connected (especially directly) to the Internet.
    – user
    Dec 30, 2017 at 18:27
  • 1
    Very nice answer, @Dario Fumagalli. I also like your comments on SE Politics! Feb 7, 2022 at 6:14
6

If you think about what a localhost or loopback IP address means, you realize that you never want to see that address, or the network that that address belongs to, outside of a host. (Inside of a host, it's too dark to see it. Apologies to Mark Twain.)

So, someone had to pick an IP network to represent this localhost address. I don't recall who first chose it, but it's specified in the IETF Request for Comments that is periodically issued as "Host Requirements".

It was done so long ago, that the idea of "wasting" an entire class A address didn't enter anyone's mind at the time.

The utility of localhost is that you can talk to yourself using a hard-coded IP address. It was used long before there was the Domain Name System. You could actually use any of the 127.x.x.x valid addresses, but no one ever does. You can't sneak and use 127 as a real network because the "Router Requirements" RFC disallows ever routing that network on any internet.

4

First, the whole 127.x.x.x range points to your localhost.
127 in binary is "01111111". "11111111" = 255 and 0 are reserved, so the choice is obvious :)

4
  • 28
    How come it's obvious again?
    – Roee Adler
    Aug 30, 2009 at 4:42
  • I believe they just chose something simple. Again, all "1"s is reserved, so one zero was added :)
    – kolypto
    Aug 30, 2009 at 10:15
  • so why not 10111111 ? :) or 11011111 or 11101111 or 11110111 or 11111011 or 11111101 or something like it ?
    – Mushegh A.
    Oct 8, 2009 at 7:40
  • 2
    Nope. Try again.
    – Ardee Aram
    Jun 11, 2013 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .