如何将主机文件重置回默认值

应用对象
Windows 11 Windows 10

如果遇到网络或网站连接问题,则主机文件可能是问题的一部分。 本文介绍什么是 Hosts 文件、其工作原理,以及为何将其重置为默认状态可以解决常见的连接问题。

什么是主机文件

主机文件是操作系统用来将人性化的主机名 (如 contoso.com) 映射到数字 Internet 协议 (IP) 地址的纯文本系统文件。 此映射允许系统识别和定位 IP 网络上的设备,并且是操作系统 IP 实现的标准部分。

Hosts 文件中的每个条目都显示在自己的行上,包括一个 IP 地址,后跟一个或多个主机名,用空格 (制表符或空格) 分隔。 以哈希字符 (#) 开头的行被视为注释,空白行将被忽略。

如果从默认状态修改 Hosts 文件,则它可能会覆盖正常的网络名称解析并导致连接问题。 将文件重置为默认配置通常有助于解决访问网站或网络资源的问题

重置主机文件

若要将主机文件重置回默认值,请根据你的操作系统执行以下步骤:

Windows 11

  1. 按 Windows 徽标键,键入 Notepad,然后选择 记事本 应用图标。

  2. 复制以下文本,然后将文本粘贴到文件中:

    # 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
    
  3. 在 “文件 ”菜单上,选择“ 另存为 ”,然后在“ 文件名 ”框中键入“hosts” (并) 引号。

  4. 将 “另存为”类型 “更改为” 所有文件 (*.*) “。 主机文件不能有文件扩展名。 

  5. 将文件保存到桌面并关闭记事本。

  6. 搜索 %WinDir%\System32\Drivers\Etc,然后选择文件夹图标。
    注意:通常情况下, C:\Windows\System32\drivers\etc

  7. 右键单击现有的 Hosts 文件,选择“ 重命名”,然后将文件名更改为“Hosts.old”。

  8. 将创建的主机文件复制或移动到桌面的 %WinDir%\System32\Drivers\Etc 文件夹。 如果系统提示您输入管理员密码,请选择继续。

Windows 10

若要将主机文件重置回默认值,请执行以下步骤:

  1. 按 Windows 徽标键,键入 Notepad,然后选择 记事本 系统图标。

  2. 复制以下文本,然后将文本粘贴到文件中:

    # 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
    
  3. 在 “文件 ”菜单上,选择“ 另存为 ”,然后在“ 文件名 ”框中键入“hosts” (并) 引号。

  4. 将 “另存为”类型 “更改为” 所有文件 (*.*) “。 主机文件不能有文件扩展名。 

  5. 将文件保存到桌面并关闭记事本。

  6. 搜索 %WinDir%\System32\Drivers\Etc,然后选择文件夹图标。
    注意:通常情况下, C:\Windows\System32\drivers\etc

  7. 右键单击现有的 Hosts 文件,选择“ 重命名”,然后将文件名更改为“Hosts.old”。

  8. 将创建的主机文件复制或移动到桌面的 %WinDir%\System32\Drivers\Etc 文件夹。 如果系统提示您输入管理员密码,请选择继续。