hosts文件说明
在Windows系统中,有一个叫hosts的系统文件,可以用它进行本地IP地址映射,文件的原始内容如下:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
设置本地IP地址映射后,可以简化一些操作,比如ping命令,ssh命令。这个特点对于做编程或网路的场景比较实用。比如,开发人员可能在自己的电脑上部署几台虚拟机作为测试环境,经常需要用ssh命令连接虚拟机或者使用ping命令测试网络测试虚拟机的网络情况。我的频道会及时的整理出windows操作的技巧和实用方法,喜欢的朋友可以关注,点赞,收藏,分享给朋友。
文件位置:
在Win11系统中,映射文件的位置在C:\Windows\System32\drivers\etc目录下,其它Windows系统的位置可能会略有不同。
设置方法:
1. 在文件的最后加入,每个映射关系占一行。
2. IP地址在前面,后面是机器名,IP地址和机器名之间最少要有一个空格。
3. `#` 号后面的内容是注释,可以独占一行,也可以跟在设置的后面。
4. hosts属于系统文件,修改时,需要使用管理员权限才能保存。
5. 保存完成后即刻生效。
设置格式:
IP地址 映射字符串
例如,我给自己的电脑上加入一条设置`192.168.100.71 combo71`,就是将192.168.100.71这个地址与combo71这个字符串建立了对应关系,当然,为了实用考虑,这个字符串一般是局域网内某台机器的机器名或是域名。修改后的hosts文件如下:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
# 虚拟机上的机器
192.168.100.71 combo71
测试
设置成功后 ping 192.168.100.71的效果和ping combo71对 效果是一样的。如下:
C:\Users\80771>ping 192.168.100.71
正在 Ping 192.168.100.71 具有 32 字节的数据:
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
192.168.100.71 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
C:\Users\80771>ping combo71
正在 Ping combo71 [192.168.100.71] 具有 32 字节的数据:
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.71 的回复: 字节=32 时间<1ms TTL=64
192.168.100.71 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
感谢阅读到最后,关注我,持续给您带来简单,实用的Windows操作技巧。