Difference between localhost and 127.0.0.1

  sonic0002        2018-09-29 22:11:12       31,081        2         

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

LINUX  NETWORK  LOCALHOST  127.0.0.1  LOCALHOST VS 127.0.0.1 

       

  RELATED


  2 COMMENTS


Germán Hernández [Reply]@ 2019-05-15 18:04:22

Thanks a lot for the information.

Anonymous [Reply]@ 2021-04-29 03:41:13

Thank you so much for your explanation. very helpful. 



  RANDOM FUN

Programmer's halloween