1.文档阅读 断网了,还能ping通 127.0.0.1 吗? 开发环境- 配置虚拟主机域名/hosts文件 2.整理输出 总结 1. localhost 是域名 ,127.0.0.1是IP地址. 2. localhost 是可有可无, 127.0.0.1是必有. localhost定义在hosts文件中, 127.0.0.1则是定义源码中, 应该是操作系统内核中. 通常默认为 127.0.0.1 localhost , 意思就是告诉IP地址解析 localhost就解析为 127.0.0.1 这也就意味着, localhost可以删掉, 修改, 以及对应IP未必是127.0.0.1. 3. localhost 是域名 ,127.0.0.1是IP地址. 通常情况下, 二者是等同的, 但是有细微差别. - 127.0.0.1 相较于 localhost 访问速度更快, 因为少了检索hosts解析的步骤 [公网IP访问也比域名要快, 一样的道理, 减少了路径] - 有时, 就未必是等同的, 比如在hosts文件中, 修改 localhost 14.119.104.254, 这时候重新访问localhost看看是怎样? 4. 本地地址其实应该是两个地址 - 本机公网IP, 本机私网IP. 但是通常, 大家的电脑并没有直接配置公网IP, 而是通过NET的方式联网, 也就是本机IP通常是指本机私网IP, 也就是局域网IP. 如下: 终端下查看 5. ping命令作为扩展, 简单记为, 可以测试网络连通性. 如果能ping通目标主机, 说明网络链路是没问题的, 如果这时候不能与目标主机通信 或者说 不能联网 就要找具体不能联网的服务/软件/工具, 比如浏览器, 微信等等, 问题很有可能出现这些软件本身, 通常一个可能就是代理的问题. 但是即便 不能ping通目标主机, 也不能说明网络链路是有问题的, 因为目标主机很有可能拒绝被ping, 可以多换几个, 以及使用curl等命令辅助确认. 使用方式: ping 域名/IP 注意, 不要携带http(s)前缀 至于, 详细解释整个过程, 可以了解, 也可以掌握, 从图上可以看到并不复杂, 记住就行了. 6. 不论是localhost 还是 127.0.0.1 以及 本机私网IP地址, 都不会从真网卡发送出去. 也就是不会数据包不会出本机. 也就意味着, 不能通过localhost 和 127.0.0.1 来压测服务性能, 因为网络几乎没有延时, 也就是不准. 下面是ping 本机公网IP, 127.0.0.1, 本机私网IP, 以及localhost的情况 [root@ip-172-31-24-254 ~]# ping 54.18.13.75 PING 54.18.13.75 (54.18.13.75) 56(84) bytes of data. 64 bytes from 54.18.13.75: icmp_seq=1 ttl=63 time=0.317 ms 64 bytes from 54.18.13.75: icmp_seq=2 ttl=63 time=0.322 ms 64 bytes from 54.18.13.75: icmp_seq=3 ttl=63 time=0.306 ms 64 bytes from 54.18.13.75: icmp_seq=4 ttl=63 time=0.280 ms 64 bytes from 54.18.13.75: icmp_seq=5 ttl=63 time=0.283 ms 64 bytes from 54.18.13.75: icmp_seq=6 ttl=63 time=0.477 ms 64 bytes from 54.18.13.75: icmp_seq=7 ttl=63 time=0.319 ms 64 bytes from 54.18.13.75: icmp_seq=8 ttl=63 time=0.291 ms ^C --- 54.18.13.75 ping statistics --- 8 packets transmitted, 8 received, 0% packet loss, time 7058ms rtt min/avg/max/mdev = 0.280/0.324/0.477/0.061 ms [root@ip-172-31-24-254 ~]# ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.013 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.015 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.026 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.019 ms 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.018 ms 64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.019 ms 64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.019 ms ^C --- 127.0.0.1 ping statistics --- 7 packets transmitted, 7 received, 0% packet loss, time 6019ms rtt min/avg/max/mdev = 0.013/0.018/0.026/0.005 ms [root@ip-172-31-24-254 ~]# ^C [root@ip-172-31-24-254 ~]# ^C [root@ip-172-31-24-254 ~]# ping 172.31.24.254 PING 172.31.24.254 (172.31.24.254) 56(84) bytes of data. 64 bytes from 172.31.24.254: icmp_seq=1 ttl=64 time=0.015 ms 64 bytes from 172.31.24.254: icmp_seq=2 ttl=64 time=0.021 ms 64 bytes from 172.31.24.254: icmp_seq=3 ttl=64 time=0.021 ms 64 bytes from 172.31.24.254: icmp_seq=4 ttl=64 time=0.020 ms 64 bytes from 172.31.24.254: icmp_seq=5 ttl=64 time=0.014 ms 64 bytes from 172.31.24.254: icmp_seq=6 ttl=64 time=0.017 ms 64 bytes from 172.31.24.254: icmp_seq=7 ttl=64 time=0.021 ms ^C --- 172.31.24.254 ping statistics --- 7 packets transmitted, 7 received, 0% packet loss, time 6002ms rtt min/avg/max/mdev = 0.014/0.018/0.021/0.004 ms [root@ip-172-31-24-254 ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 9001 inet 172.31.24.254 netmask 255.255.240.0 broadcast 172.31.31.255 inet6 fe80::ab:52ff:feda:e117 prefixlen 64 scopeid 0x20<link> ether 02:ab:52:da:e1:17 txqueuelen 1000 (Ethernet) RX packets 41267198133 bytes 2697939623991 (2.4 TiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 42763800360 bytes 97990995720923 (89.1 TiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 721790821 bytes 55052533474 (51.2 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 721790821 bytes 55052533474 (51.2 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@ip-172-31-24-254 ~]# ping localhost PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.015 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.017 ms 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.019 ms 64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.017 ms 64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.019 ms 64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.019 ms ^C --- localhost ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 5003ms rtt min/avg/max/mdev = 0.015/0.017/0.019/0.005 ms [root@ip-172-31-24-254 ~]# [root@ip-172-31-24-254 ~]# ping www.baidu.com PING www.wshifen.com (104.193.88.123) 56(84) bytes of data. 64 bytes from 104.193.88.123 (104.193.88.123): icmp_seq=1 ttl=47 time=1.38 ms 64 bytes from 104.193.88.123 (104.193.88.123): icmp_seq=2 ttl=47 time=1.37 ms 64 bytes from 104.193.88.123 (104.193.88.123): icmp_seq=3 ttl=47 time=1.57 ms ^C --- www.wshifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 1.373/1.445/1.577/0.093 ms ^C[root@ip-172-31-24-254 ~]# ping 104.193.88.123 PING 104.193.88.123 (104.193.88.123) 56(84) bytes of data. 64 bytes from 104.193.88.123: icmp_seq=1 ttl=47 time=1.40 ms 64 bytes from 104.193.88.123: icmp_seq=2 ttl=47 time=1.40 ms 64 bytes from 104.193.88.123: icmp_seq=3 ttl=47 time=1.39 ms 64 bytes from 104.193.88.123: icmp_seq=4 ttl=47 time=1.36 ms 64 bytes from 104.193.88.123: icmp_seq=5 ttl=47 time=1.36 ms ^C --- 104.193.88.123 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4019ms rtt min/avg/max/mdev = 1.365/1.387/1.407/0.050 ms [root@ip-172-31-24-254 ~]# 可以看到, 只有ping 本机公网IP出了网卡[ttl=63], 但是到了第一个节点, 应该是本地路由器, 就直接返回了. 其他情况, 都没有出网卡, ttl=64. 同时可以看往返时间: localhost 几乎等于 127.0.0.1 几乎等于 本机私网IP < 本机公网IP < 其他公网IP[百度] 后续补充 ... |