Difference between localhost and 127.0.0.1

微信扫一扫,分享到朋友圈

Difference between localhost and 127.0.0.1

Lots of people would think what the address 127.0.0.1
is when first seeing this address. In fact, 127.0.0.1 is a loopback address which refers to the local machine. It is generally used for local testing. When typing ping 127.0.0.1
on local command console, it will send network packets to local IP/TCP layer to test whether IP/TCP works properly or not.

To those who are used to use localhost
, it is actually mapped to 127.0.0.1 by default. There are hosts files in the system which store this mapping. Hence normally these two can be interchanged.

# localhost name resolution is handled within DNS itself. # 127.0.0.1       localhost # ::1             localhost
127.0.0.1       localhost

Is there any difference between localhost and 127.0.0.1? What is it if there is?

Indeed, there is. Many people would think that it needs one less DNS resolution when using 127.0.0.1 since localhost no need to be resolved. But there are differences. localhost is also called local, usually referred as the local machine
, while 127.0.0.1 is usually considered as local address
. It’s very important to know that it will not go through network card when using localhost while it will when using 127.0.0.1, this means localhost will not be affected by network card configurations and firewall settings, all ports are open as well
. Generally speaking, localhost is a better option when configuring local testing environment. Sometimes some connection is working with localhost but is not working with 127.0.0.1 is because of above reasons. Furthermore, when using localhost to access resource, it is accessed with the local user privileges, while it will use the network user privileges when using 127.0.0.1 to access.

Below table summarizes the differences

localhost 127.0.0.1 local IP(192.56.xxx.xxx)
Network Not connected Not connected Connected
Transmission No network card, not firewall restriction Network card, firewall restrictions Network card, firewall restrictions
Access Local access Local access(Network user privileges) Local or external access

Reference: https://my.oschina.net/fhd/blog/500778

Spring学习笔记(一):IOC-1

上一篇

这个国庆节,华为云的域名上线了。

下一篇

你也可能喜欢

Difference between localhost and 127.0.0.1

长按储存图像,分享给朋友