Skip to content

Instantly share code, notes, and snippets.

@zxhfighter
Last active March 30, 2023 08:12
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zxhfighter/b9f4b4ef328cd8b433b0e9dc2f4af26d to your computer and use it in GitHub Desktop.
Save zxhfighter/b9f4b4ef328cd8b433b0e9dc2f4af26d to your computer and use it in GitHub Desktop.
127.0.0.1 和 0.0.0.0 的区别

127.0.0.1、localhost、0.0.0.0、本机IP地址

localhost

首先,localhost 就是一个域名,域名指向 127.0.0.1,两者是一样的,如果是 mac,可以在 /etc/hosts 文件中看到这一行。

127.0.0.1     localhost

127.0.0.1

127.0.0.1 这个地址通常分配给 loopback 接口,用来测试本机的 TCP/IP 协议栈。loopback 是一个特殊的网络接口(可理解成虚拟网卡),用于本机中各个应用之间的网络交互。只要操作系统的网络组件是正常的,loopback 就能工作。

0.0.0.0

这个 IP 相当于 java 中的 this,代表当前设备的 IP,可以代表本机的所有 IP 地址。

比如你的一个 tomcat 配置文件中,如果监听的 IP 地址设置了 0.0.0.0,就表示你的这个 tomcat 服务器监听在你本机的所有 IP 地址上,通过任何一个 IP地址 都可以访问到。

如果你本地的 IP 地址有 192.168.1.10,172.16.2.10,那么访问你这个 tomcat 就可以。 http://192.168.1.10:8080/,http://172.16.2.10:8080/ 都可以访问。

如果你的监听地址设置了 192.168.1.10 ,那么将无法通过 http://172.16.2.10:8080/ 进行访问。

本机 IP

至于本机 IP,你可以理解为本机有三块网卡,一块网卡叫做 loopback(这是一块虚拟网卡),另外一块网卡叫做 ethernet (这是你的有线网卡),另外一块网卡叫做 wlan(这是你的无线网卡)。

你的本机 IP 是你真实网卡的 IP,具体来说有线无线各有一个,而 127.0.0.1 是那块叫做 loopback 的虚拟网卡的 IP。

192.168.1.1

192.168.1.1 是一个 C 类 IP 地址,可以是局域网内任何一台主机,看你的路由器把这个地址分配给了谁。

@RitterHou
Copy link

大佬牛逼!!!给大佬递茶

@120534
Copy link

120534 commented Aug 21, 2019

👍

@fengjk12138
Copy link

赞一个

@shion57
Copy link

shion57 commented Oct 7, 2020

👍

@shion57
Copy link

shion57 commented Oct 7, 2020

👍

@gotgith
Copy link

gotgith commented Oct 9, 2020

six six six

@Brian24NX
Copy link

Great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment