
dns中的主机记录
DNS makes us name IP addresses into meaningful names. We can name a host with a human-readable name and use this name to access it. DNS management can be done in different ways where DNS servers are the most practical and straightforward way. But the first step before a DNS server to query a DNS is using the local Hosts File
. In this tutorial, we will learn how to manage Windows 10 Hosts File like change, secure, restore, block, etc. These settings can be also applied to all modern Windows operating systems like Windows 7, Windows 8, Windows 10, Windows Server 2008, Windows Server 2012, Windows Server 2016, Windows Server 2019.
DNS使我们将IP地址命名为有意义的名称。 我们可以使用易于理解的名称来命名主机,然后使用该名称进行访问。 DNS管理可以以不同的方式完成,而DNS服务器是最实用,最直接的方式。 但是,DNS服务器查询DNS之前的第一步是使用本地Hosts File
。 在本教程中,我们将学习如何管理Windows 10主机文件,例如更改,安全,还原,阻止等。这些设置也可以应用于所有现代Windows操作系统,例如Windows 7,Windows 8,Windows 10,Windows Server 2008 ,Windows Server 2012,Windows Server 2016,Windows Server 2019。
什么是主机文件? (What Is Hosts File?)
hosts
file is a basic text file that stores IP address and domain name matches. Every IP address has a domain name that should be compliant with the domain name standard. hosts
file usage is very popular and standard where it is used different operating systems like Microsoft Windows, Linux Distributions and MacOSX operating systems.
hosts
文件是一个基本的文本文件,用于存储IP地址和域名匹配项。 每个IP地址都有一个域名应符合域名标准。 hosts
文件的使用非常流行并且是标准的,用于不同的操作系统,例如Microsoft Windows,Linux Distributions和MacOSX操作系统。
主机文件位置 (Hosts File Location)
The hosts file is stored in different paths in different operating systems. The hosts file is generally stored in a system configuration path or location which requires special privileges like Administrator to access and change it. In Windows operating systems hosts file is store C:\Windows\System32\Drivers\etc\
file as it can be also expressed like %WinDir%\System32\Drivers\Etc\
where %WinDir%
is the path variable which stores windows installation path.
主机文件存储在不同操作系统中的不同路径中。 主机文件通常存储在系统配置路径或位置中,该路径或位置需要特殊权限(例如管理员)才能访问和更改。 在Windows操作系统中,主机文件存储在C:\Windows\System32\Drivers\etc\
文件中,因为它也可以表示为%WinDir%\System32\Drivers\Etc\
,其中%WinDir%
是存储Windows安装路径的路径变量。

Also, we can see that its size is 1KB where it stores very little data in text format.
此外,我们可以看到它的大小为1KB,其中以文本格式存储的数据很少。
主机文件语法 (Hosts File Syntax)
As stated previously hosts file has very basic syntax. Each line is an entry to match the IP address with the domain name.
如前所述,hosts文件具有非常基本的语法。 每行是一个将IP地址与域名匹配的条目。
IP_ADDRESS DOMAN_NAME
- `IP_ADDRESS` is the IP address we want to route for the given DOMAIN_NAME IP_ADDRESS是我们要为给定DOMAIN_NAME路由的IP地址
- `DOMAIN_NAME` is the domain name we want to match with the IP_ADDRESSDOMAIN_NAME是我们要与IP_ADDRESS匹配的域名
# 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
主机文件注释 (Hosts File Comment)
In hosts file #
sign is used to create comments. Comments are useful to create some notes or explanations about the given DNS record. We can also use comments in order to make an active record passive. In the following example, the poftut.com
domain name will be redirected to the IP address 1.2.3.4
.
在主机文件中,使用#
号创建注释。 注释对于创建有关给定DNS记录的一些注释或解释很有用。 我们还可以使用注释来使活动记录成为被动记录。 在以下示例中, poftut.com
域名将被重定向到IP地址1.2.3.4
。
# This is poftut.com special DNS record
1.2.3.4 poftut.com
If we want to make this record passive without deleting it we can put a #
before the poftut.com record to make it passive.
如果要使该记录成为被动记录而不删除它,我们可以在poftut.com记录前加上#
以使其成为被动记录。
# This is poftut.com special DNS record
#1.2.3.4 poftut.com
备份主机文件 (Backup Hosts File)
Before starting the adding, removing or changing the contents of the hosts file backing up is good behavior. Especially if there are a lot of records it will be very useful. As hosts file is a simple text file we can take a backup just copy and paste it into the same directory with a different name or different directory will work. Below we copied the hosts file and added the .backup
extension. We can see that the type of hosts.backup
is also shown as BACKUP
.
在开始添加,删除或更改主机文件备份的内容之前,这是一个好的行为。 特别是如果有很多记录,它将非常有用。 由于hosts文件是一个简单的文本文件,因此我们可以进行备份,只需将其复制并粘贴到具有不同名称的同一目录中,否则其他目录将起作用。 在下面,我们复制了hosts文件并添加了.backup
扩展名。 我们可以看到hosts.backup
的类型也显示为BACKUP
。

使用记事本修改主机文件(Modify Hosts File with NotePad)
Modifying the hosts file requires Administrative privileges. so in order to edit and save with the NotePad applications, the NotePad should be opened with the Administrative user rights. First From the Start Menu
type notepad
and right-click to the listed notepad app.
修改主机文件需要管理权限。 因此,为了使用NotePad应用程序进行编辑和保存,应使用管理用户权限打开NotePad。 首先从“ Start Menu
键入notepad
然后右键单击列出的记事本应用程序。

We will be asked the following question where we will accept it by clicking Yes
.
单击Yes
将询问以下问题,我们将在哪里接受该问题。

The notepad will be opened and shown like below. We can open the hosts file in different ways where we will click to the File
->Open
.
记事本将打开,如下所示。 我们可以通过不同的方式打开hosts文件,在其中单击File
> Open
。

We will we asked for the hosts file location which is C:\Windows\System32\drivers\etc\hosts
or %WinDir%\System32\drivers\etc\hosts
.
我们将要求提供主机文件位置,即C:\Windows\System32\drivers\etc\hosts
或%WinDir%\System32\drivers\etc\hosts
。

The opened file will be like below where existing records will be shown.
打开的文件将如下所示,其中将显示现有记录。

We will add the following records where there are some comments. We will also add a subdomain name to the poftut.com named srv.poftut.com
我们将在有评论的地方添加以下记录。 我们还将在poftut.com中添加一个名为srv.poftut.com
的子域名。
#Poftut.com domain
1.2.3.4 poftut.com
#Poftut.com Server1
5.6.7.8 srv.poftut.com
100.101.102.103 mysrv2.kaleinfo.com

Then we can click to the File
->Save
in order to save and activated newly added records.
然后我们可以单击File
> Save
,以保存和激活新添加的记录。
使用NotePad ++修改主机文件 (Modify Hosts File with NotePad++)
As a simple text file, we can open the hosts
file with the Windows NotePad. We will navigate to the etc
directory by using %WinDir%\System32\Drivers\Etc\
or C:\Windows\System32\Drivers\etc\
in our Windows Explorer.
作为一个简单的文本文件,我们可以使用Windows NotePad打开hosts
文件。 我们将使用Windows资源管理器中的%WinDir%\System32\Drivers\Etc\
或C:\Windows\System32\Drivers\etc\
导航到etc
目录。

We will see the following hosts
file which is opened by the Notepad++.
我们将看到以下由记事本++打开的hosts
文件。

We will add the following lines to the existing hosts
file and use CTRL+S
or File
->Save
in order to save changes and modifications.
我们将以下行添加到现有的hosts
文件中,并使用CTRL+S
或File
> Save
来保存更改和修改。

As we do not have Administrator rights it will ask us for Admin rights with the following screen. We will click to the Yes
.
由于我们没有管理员权限,因此会在以下屏幕上询问我们管理员权限。 我们将单击“ Yes
。

We will be asked the following question and click to Yes
.
我们将被询问以下问题,然后单击“ Yes
。

Now we have admin rights where we can save using CTRL+S
shortcut or File
->Save
menu.
现在,我们拥有管理员权限,可以使用CTRL+S
快捷方式或“ File
->“ Save
菜单进行Save
。
检查新主机和DNS设置 (Check New Host and DNS Settings)
We can check the newly added DNS records by using the ping command. We will ping the srv.poftut.com
host like below. As it does not exist the ping responses will fail but we can see that the IP address is 5.6.7.8
which we have set in the hosts file.
我们可以使用ping命令检查新添加的DNS记录。 我们将如下所示对srv.poftut.com
主机执行ping srv.poftut.com
。 由于不存在,因此ping响应将失败,但是我们可以看到我们在hosts文件中设置的IP地址为5.6.7.8
。

使用主机文件阻止网站(Block Website Using Hosts File)
Well changing the hostnames, DNS records or IP addresses gives us the ability to block sites in the given host by using hosts file. For example, add the IP address 1.1.1.1
for the google.com
domain like below will block the access to the Google for the specified domain names.
更改主机名,DNS记录或IP地址可以使我们能够通过使用hosts文件来阻止给定主机中的站点。 例如,为google.com
域添加IP地址1.1.1.1
(如下所示)将阻止对指定域名的Google访问。
1.1.1.1 google.com
1.1.1.1 www.google.com
1.1.1.1 gmail.com
通过将主机设置为只读来锁定主机文件 (Lock Hosts File By Making Read-Only)
We can prevent the hosts file from accidental change or modification by making it read-only. We will open theC:\Windows\System32\drivers\etc\
or %WinDir%\System32\drivers\etc\
path in the Windows Explorer and right-click to the hosts
file and select the Properties
like below.
通过将其设置为只读,可以防止hosts文件意外更改或修改。 我们将在Windows资源管理器中打开C:\Windows\System32\drivers\etc\
或%WinDir%\System32\drivers\etc\
路径,然后右键单击hosts
文件,然后选择如下所示的Properties
。

Then we will see the Properties window where we will click the Read-only
checkbox from the General
tab and then click to the Apply
.
然后,我们将看到“属性”窗口,在其中单击“ General
选项卡上的“ Read-only
复选框,然后单击“ Apply
。

翻译自: https://www.poftut.com/modify-hosts-file-in-windows-10-tutorial-to-add-and-change-dns-records/
dns中的主机记录