2

什么是Localhost

 2 years ago
source link: https://www.myfreax.com/what-is-localhost/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

计算机机器名由它们的主机名 [主机名] 和 IP 地址标识。

在下文中接口代表我们常说的网卡

下图简要说明了它。

同样,我们也有一个通用名称,用于所有用于调用自身的计算机系统称为 localhost。在这里,术语“localhost”与计算机网络的上下文相关联。它在我们作为开发人员或系统管理员的历程中发挥着至关重要的作用。“localhost”有很多用例,例如应用程序测试、文档、网络性能测试和站点阻止。

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

什么是localhost本地主机?

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

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

什么是回环地址?

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

根据IPv4寻址的IETF标准,127.0.0.0/8 的整个块被分配用于回环网络接口。作为默认行为,每次安装服务器后都会配置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环回网络接口这两个术语。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK