Why is 127.0.0.1 used for localhost?

发布时间 2023-07-07 10:32:43作者: 不及格的程序员-八神
 

Although it’s not documented anywhere (at least as far as I know), but there are some logical explanations.
But before going over the explanation let’s first give a brief description of what is localhost addresses and what is used for.

What is the localhost address “127.0.0.0/8”?

In computer networking, localhost is a hostname that means this computer. It is used to access the network services that are running on the host via the loopback network interface. Using the loopback interface bypasses any local network interface hardware.

The local loopback mechanism may be used to run a network service on a host without requiring a physical network interface, or without making the service accessible from the networks the computer may be connected to. For example, a locally installed website may be accessed from a web browser by the URL http://localhost to display its home page.

IPv4 network standards reserve the entire 127.0.0.0/8 address block for loopback purposes. That means any packet sent to any of those addresses (127.0.0.1 through 127.255.255.255) is looped back. The address 127.0.0.1 is the standard address normally used for IPv4 loopback traffic; the rest are rarely used in practice. The IPv6 standard assigns only a single address for loopback: ::1.

 

Localhost - Wikipedia

So basically the localhost address used in testing software/applications. It’s a service provided internally by the operating system. You can ping the localhost address even if you have no functional network interface card “NIC”. You can easily test it. Go now and disable your NIC then try to ping 127.0.0.1 “127.x.x.x”.


  • 0 = This
  • 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 address of all ones are to be interpreted as meaning "all", 
  • as in "all hosts". 
  •  
  • For example, the address 128.9.255.255 could be interpreted 
  • as meaning all hosts on the network 128.9. 
  •  
  • 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. 

RFC 990 - Assigned numbers


  • Pattern/Scheme “First and last IP”

We can easily see that the first and last number of any IP range is almost always special. For example the first Class A IP “0.x.x.x” is reserved, for any network, the first IP is reserved as it represents the whole network and the last IP is reserved for broadcast. So you can see there is some kind of scheme or pattern.
So they decided to use the last classful A network for the purpose of the localhost. Which is something seems so weird today, why would we waste an entire class A network while we just use one IP address. Back then they had no idea that there will be any need for all the possible IPv4 addresses, they just didn’t care about all the wasted IP addresses.

  • 127.rrr.rrr.rrr Reserved [JBP] 
  •  
  • Class B Networks 
  •  
  • Internet Address Name Network References 
  • ---------------- ---- ------- ---------- 
  • 128.000.rrr.rrr Reserved [JBP] 
  • 128.001.rrr.rrr-128.254.rrr.rrr Unassigned [JBP] 
  • 191.255.rrr.rrr Reserved [JBP] 
  •  
  • Class C Networks 
  •  
  • Internet Address Name Network References 
  • ---------------- ---- ------- ---------- 
  • 192.000.001.rrr Reserved [JBP] 
  • 192.000.001.rrr-223.255.254.rrr Unassigned [JBP] 
  • 223.255.255.rrr Reserved [JBP] 
  •  
  • Other Reserved Internet Addresses 
  •  
  • Internet Address Name Network References 
  • ---------------- ---- ------- ---------- 
  • 224.000.000.000-255.255.255.255 Reserved [JBP] 

RFC 790 - Assigned numbers - Dec 1980

As you can see, back in 1980 they just reserved entire classful networks for no good reason.


  • Low level implementation in mind.

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.

 

Why is localhost IP 127.0.0.1?


Some people suggest that it’s related to the old computers processor limitation. It was chosen to ensure the simplest calculations by the processor.

Other people think that the reserved range was randomly chosen, but I don’t like this theory.
Although back when the internet was created they didn’t imagine how the internet will be in the next years, Which is completely understanding but these people were geniuses, hard workers, …etc to chose things randomly.


For people out there studying CompTIA Network+ or CCNA, check AlphaPrep.net for high-quality study materials and practice tests.

  • Upvote if you find this useful.
  • Comment if you have a question or something to share.
  • Follow if you are interested in reading more about computers and networks.

Have a nice day : )