什么是本地主机Localhost?

2022年 10月 12日 15.3k 0

本地主机

计算机机器由它们的主机名 [主机名] 和 IP 地址来标识。下图简要说明了这一点。

唯一名称

同样,我们也为所有过去自称的计算机系统提供了一个通用名称,即 localhost。在这里,术语“本地主机”与计算机网络的上下文相关联。它在我们作为开发人员或系统管理员的旅程中起着至关重要的作用。“本地主机”有许多用例,例如应用程序测试、文档、网络性能测试和站点阻止。

让我们更深入地了解它的细节。

什么是本地主机?

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

它可以帮助我们检查机器中的网络服务,即使在网络硬件出现故障时也是如此。当使用“localhost”时,网络服务是通过称为环回的逻辑网络接口访问的。Loopback 接口的 IP 地址为 127.0.0.1。

因此,localhost 解析为 127.0.0.1 作为名称解析的一部分。

什么是环回地址?

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

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

让我们看一下下面的代码片段。

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 卡。此外,存在一条规则,即路由器不应路由回环 IP 地址。

例如,当我们请求 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

使用 localhost 进行应用程序测试

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

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

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

ping -c 4 mryunwei.com
PING mryunwei.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
--- mryunwei.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 mryunwei.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
ping -c 4 mryunwei.com
PING mryunwei.com (127.0.1.100) 56(84) bytes of data.
64 bytes from mryunwei.com (127.0.1.100): icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from mryunwei.com (127.0.1.100): icmp_seq=2 ttl=64 time=0.094 ms
64 bytes from mryunwei.com (127.0.1.100): icmp_seq=3 ttl=64 time=0.042 ms
64 bytes from mryunwei.com (127.0.1.100): icmp_seq=4 ttl=64 time=0.055 ms
--- mryunwei.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 和环回网络接口有基本的了解。

相关文章

如何在 Linux 中使用 logname 命令?
为什么有 HTTPS?HTTPS 如何实现安全通信?
HTTPS的TSL握手流程是什么
华为无线网络射频调优及WLAN跨VLAN的三层漫游示例
502错误是什么、应该怎么排查?
HTTP3为什么抛弃了经典的TCP,而选择QUIC

发布评论