2025年2月24日 星期一 甲辰(龙)年 腊月廿四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 网络通信

什么是Localhost

时间:06-09来源:作者:点击数:32

计算机名称由它们的主机名和IP地址标识。同样,我们也有一个通用名称,用于调用自身的计算机系统。它称为localhost。在这里,术语localhost与计算机网络的上下文相关联。它在我们作为开发或系统管理的过程中发挥着至关重要的作用。

localhost有很多用例,例如应用程序测试、网络性能测试。下图简要说明了它。让我们更深入地了解它的细节。

localhost本地主机

localhost是一个主机名,指的是调用自身计算机系统,这意味着当我们调用localhost时,机器将与自己对话。

它帮助我们检查机器中的网络服务,即使在网络硬件故障期间。在使用localhost时,可以通过称为回环Loopback逻辑网络接口访问本地网络服务。Loopback接口的IP地址为127.0.0.1。因此,作为名称解析的一部分,localhost本地主机解析为127.0.0.1。

回环地址

回环接口是所有操作系统中都存在的逻辑网络接口。通过该接口传输的数据包被返回/回环到同一台机器上的同一接口。因此,该接口称为回环接口。

根据IPv4寻址的IETF标准,127.0.0.0/8的整个网段被分配给回环网络接口。作为默认行为,每次安装服务器后都会配置Loopback接口。

让我们看看下面ip命令的输出,ip a show lo将会打印回环接口Loopback的信息。

  • ip a show lo
  • 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 76238871 bytes 6955286874 (6.9 GB)
  • RX errors 0 dropped 0 overruns 0 frame 0
  • TX packets 76238871 bytes 6955286874 (6.9 GB)
  • TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  • sudo cat /etc/hosts
  • 127.0.0.1 localhost
  • 127.0.1.1 sandbox1
  • # The following lines are desirable for IPv6 capable hosts
  • ::1 ip6-localhost ip6-loopback

回环数据包

通常,Loopback报文通过地址与其他IP报文区分开来。带有环回地址的环回数据包的处理发生在 TCP/IP协议栈的链路层。此流量将在计算机系统本身内部传递。它不会像其他IP数据包那样通过硬件NIC卡。

例如,当我们请求 127.0.0.1 地址时。由于第一个八位字节 (127),请求不会转发到 Internet。在这里,TCP/IP 堆栈识别请求并将其路由回同一台机器。

本地主机localhost和其他IP数据包的数据包流的快速说明如下。

  • ping -c 4 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.035 ms
  • 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.043 ms
  • 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.041 ms
  • 64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.040 ms
  • --- localhost ping statistics ---
  • 4 packets transmitted, 4 received, 0% packet loss, time 3075ms
  • rtt min/avg/max/mdev = 0.035/0.039/0.043/0.008 ms
  • ping -c 4 google.com
  • PING google.com (142.250.71.46) 56(84) bytes of data.
  • 64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=1 ttl=120 time=2.14 ms
  • 64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=2 ttl=120 time=2.18 ms
  • 64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=3 ttl=120 time=2.19 ms
  • 64 bytes from maa03s35-in-f14.1e100.net (142.250.71.46): icmp_seq=4 ttl=120 time=2.20 ms
  • --- google.com ping statistics ---
  • 4 packets transmitted, 4 received, 0% packet loss, time 3004ms
  • rtt min/avg/max/mdev = 2.147/2.180/2.203/0.051 ms

使用本地主机测试应用程序

应用服务的可访问性首先通过网络接口。如果应用服务被映射到一个物理接口,它就可以从外部世界访问。同样,如果应用程序映射到逻辑Loopback回环接口,则只能从该特定计算机系统访问它,而不能从外部世界访问它。

从本地服务器开发和测试代码总是比从另一个远程主机容易。为此,我们将生产域名映射到/etc/hosts文件中的新环回地址 (127.0.1.100) 。/etc/hosts中的条目优先于 DNS。

下面的代码片段展示了从本地服务器到远程 myfreax Web 服务器的自然ping响应。域映射显示在第二个片段中。当我们将 127 段 IP 映射到主机文件中的 myfreax.com 后仔细检查输出时,流量被路由到环回网络接口。

  • ping -c 4 myfreax.com
  • PING myfreax.com (172.67.74.167) 56(84) bytes of data.
  • 64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=1 ttl=59 time=34.5 ms
  • 64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=2 ttl=59 time=34.5 ms
  • 64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=3 ttl=59 time=34.5 ms
  • 64 bytes from 172.67.74.167 (172.67.74.167): icmp_seq=4 ttl=59 time=34.5 ms
  • --- myfreax.com ping statistics ---
  • 4 packets transmitted, 4 received, 0% packet loss, time 3005ms
  • rtt min/avg/max/mdev = 34.521/34.529/34.541/0.227 ms
  • sudo cat /etc/hosts
  • 127.0.0.1 localhost
  • 127.0.1.1 sandbox1
  • 127.0.1.100 myfreax.com
  • # The following lines are desirable for IPv6 capable hosts
  • ::1 ip6-localhost ip6-loopback
  • ping -c 4 myfreax.com
  • PING myfreax.com (127.0.1.100) 56(84) bytes of data.
  • 64 bytes from myfreax.com (127.0.1.100): icmp_seq=1 ttl=64 time=0.074 ms
  • 64 bytes from myfreax.com (127.0.1.100): icmp_seq=2 ttl=64 time=0.094 ms
  • 64 bytes from myfreax.com (127.0.1.100): icmp_seq=3 ttl=64 time=0.042 ms
  • 64 bytes from myfreax.com (127.0.1.100): icmp_seq=4 ttl=64 time=0.055 ms
  • --- myfreax.com ping statistics ---
  • 4 packets transmitted, 4 received, 0% packet loss, time 3062ms
  • rtt min/avg/max/mdev = 0.042/0.066/0.094/0.020 ms

结论

Localhost是我们程序运行的系统的默认名称,它帮助我们测试应用程序和解决网络问题。它是通过环回网络接口使用本地环回机制实现的。它帮助我们在没有网络硬件配置依赖的情况下测试软件。作为计算机用户,必须基本了解本地主机和Loopback环回网络接口这两个术语。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门