计算机机器是由其主机名[主机名称]和IP地址来识别的。
同样,我们也有一个所有计算机系统的共同名称,用来称呼自己,被称为localhost。在这里,”localhost “这个词与计算机网络的背景有关。在我们作为开发者或系统管理员的过程中,它起着至关重要的作用。Localhost “有很多用例,例如应用测试、文档、网络性能测试和网站封锁。
让我们更多地挖掘它的细枝末节。
什么是Localhost?
Localhost是一个主机名,指的是调用程序所运行的计算机系统,这意味着当我们调用localhost时,机器将与自己对话。
它可以帮助我们检查机器中的网络服务,即使在网络硬件故障时也是如此。当使用 “localhost “时,网络服务是通过名为loopback的逻辑网络接口访问的。环回接口的IP地址是127.0.0.1。因此,作为名称解析的一部分,localhost会解析到127.0.0.1。
什么是环回地址?
环回是所有操作系统中存在的一个逻辑网络接口。通过这个接口传输的数据包会返回(循环)到同一台机器的同一个接口。因此,这个接口被称为环回。
根据IETF的IPv4寻址标准,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
环回数据包处理内部
一般来说,环回数据包是通过其地址与其他IP数据包区分开的。对带有回环地址的回环数据包的处理发生在TCP/IP堆栈的链接层。这种流量将在计算机系统内部传递。它不会像其他IP数据包那样撞击硬件网卡。此外,还存在一个规则,即路由器不应该路由回环IP地址。
例如,当我们请求使用127.0.0.1地址时。由于第一个八位数(127),该请求将不会转发到互联网。在这里,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的应用测试
应用服务的可访问性首先通过网络接口发生。如果应用服务被映射为一个物理接口,它可以从外部世界访问。同样,如果应用程序被映射到一个逻辑环回接口,它只能从该特定的计算机系统中访问,而不能从外部世界访问。
从本地服务器开发和测试代码总是比从另一个远程主机开发和测试要容易。为了实现这一点,我们在/etc/hosts文件中用一个新的回环地址(127.0.1.100)映射我们的生产域名。在/etc/hosts中的条目优先于DNS。
下面的片段展示了从本地服务器到远程Linuxize网络服务器的有机ping响应。域名映射在第二个片段中显示。当我们仔细检查主机文件中127段IP映射到linuxize.com后的输出,流量被路由到一个回环网络接口。
ping -c 4 linuxize.com
PING linuxize.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
--- linuxize.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 linuxize.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
ping -c 4 linuxize.com
PING linuxize.com (127.0.1.100) 56(84) bytes of data.
64 bytes from linuxize.com (127.0.1.100): icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from linuxize.com (127.0.1.100): icmp_seq=2 ttl=64 time=0.094 ms
64 bytes from linuxize.com (127.0.1.100): icmp_seq=3 ttl=64 time=0.042 ms
64 bytes from linuxize.com (127.0.1.100): icmp_seq=4 ttl=64 time=0.055 ms
--- linuxize.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是我们程序运行的系统的默认名称,帮助我们测试应用程序和排除网络问题。它是通过回环网络接口使用本地循环机制实现的。它帮助我们在没有网络硬件配置依赖的情况下测试软件。作为一个计算机用户,对localhost和loopback网络接口这两个术语有一个基本的了解是非常必要的。