Localhost, a reserved IP address

Several blocks of IP addresses are reserved for routing within local area networks. They are used in conjunction with network address translation (NAT) to deliver packets behind firewalls.

Another reserved IP address is 127.0.0.1. This address refers to the local machine, so a packet sent to 127.0.0.1 is delivered directly to the IP program on the sending machine itself.

This is illustrated by the following tracert command to 127.0.0.1

C:\>tracert 127.0.0.1 Tracing route to LPNewPortable [127.0.0.1] over a maximum of 30 hops: 1 <1 ms <1 ms <1 ms localhost [127.0.0.1] Trace complete. C:\>

There were no hops through routers since the client and server software were both running on the same machine.

Similarly, we can ping 127.0.0.1:

C:\>ping 127.0.0.1 Pinging LPNewPortable [127.0.0.1] with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Ping statistics for 127.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms C:\>

Four 32-byte packets were delivered to the Ping server at 127.0.0.1. The time to deliver the packets and receive an acknowledgment from the Ping server was less than 1 millisecond because it was on the same machine. The time to live was still at the initial value of 128 when the packet arrived because it did not go through any intervening routers.

This loopback capability is important to developers. For example, a Web developer will typically keep an exact copy (a mirror) of his or her live Web site on a development machine. If the development machine is running a Web server as well as a Web client, the developer can test changes made on the local machine using http://127.0.0.1 as a URL. This will retrieve the home page from the local copy of the Web site.

Finally, if you have trouble remembering "127.0.0.1" you can use the key word localhost instead. So, for example, I could have replaced the ping and tracert commands shown above with:

C:\>ping localhost and C:\>tracert localhost

Or, I could retrieve the home page from the Web server running on my machine using the URL:

http://localhost/

Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.