hosts 文件是一个基于文本的文件,位于所有现代Windows版本中的同一位置。大多数人不需要进入它,但如果你这样做,你需要知道如何打开和编辑主机文件。
但是,在某些版本的Windows中,不允许直接编辑 hosts 文件。事实上,如果您尝试保存更改,则会收到错误消息。幸运的是,您可以使用一个简单的技巧来编辑Windows 10、8、7(Windows 10)、Vista或 XP 中的 hosts 文件。
主机文件有什么作用?(What Does The Hosts File Do?)
hosts 文件的用途最初是DNS 服务器(DNS servers)现在所做的——将友好的域名映射到不友好的 IP 地址。
记住像YouTube.com(YouTube.com)这样的域名比记住像172.217.1.238这样的 IP 地址要容易得多。您无需每次想观看YouTube视频时在地址栏中输入 IP 地址,只需输入域名,您的设备使用的DNS服务器就会将该名称映射到正确的地址,以便您访问网页容易多了。
主机文件,因为它作为一种DNS
服务器,工作方式相同。您可以输入域名应解析到的 IP 地址。换句话说,您可以命令您的计算机加载与DNS服务器不同的 IP 地址,从而让您做一些很酷的事情。
为什么要编辑主机文件?(Why Edit The Hosts File?)
编辑 hosts 文件的一个原因是,如果您想要一种超级基本的方法来阻止网站在您的计算机上加载(block websites from loading on your computer)。为您要阻止的站点输入(Enter)一个无效或错误的 IP 地址,每次您尝试访问它时,hosts 文件都会加载您选择的 IP 地址。根据您选择的内容,它可能会完全阻止该网站(ompletely block the website)。
您可以使用相同的技术使主机文件阻止恶意站点。甚至还有 hosts 文件条目列表(例如这个(such as this one)),您可以下载并导入到您的 hosts 文件中,以阻止数百个恶意网站或广告。
hosts 文件的另一个用途是加快您的网页浏览速度(speed up your web browsing)。如果您使用的DNS服务器已关闭或运行速度不够快,则在主机文件中手动输入 IP 地址和域名映射可以让您的计算机几乎立即找到这些地址,而不是依赖DNS服务器。
提示(Tip):了解如何使用备用 DNS 服务器(how to use alternate DNS servers)来获得更快的互联网访问。
Windows 主机文件位置(Windows Hosts File Location)
您需要知道在哪里可以找到 hosts 文件,然后才能打开它进行编辑。这是 Windows 10 到Windows XP中的主机文件位置:
C:\Windows\system32\drivers\etc
如何编辑主机文件(How To Edit The Hosts File)
hosts 文件是一个文本文件,这意味着您需要一个文本编辑器来对其进行更改。但是,由于 hosts 文件非常基础,不需要处理超链接、图像等,您可以使用基本的文本编辑器(即,无需使用像Microsoft Word这样的成熟编辑器)。
打开主机文件(Opening The Hosts File)
让我们从简单地打开(opening)主机文件开始。我们将使用Notepad,它内置于
Windows 10到Windows XP中。
- 按住Windows(Windows)键,然后按一次R ,打开“运行”(Run)对话框。
- 键入记事本(notepad),然后按Enter打开程序。
- 转到文件(File )>打开(Open)。
- 在底部的文件名文本框中,键入(或复制并粘贴)上面显示的主机文件位置,然后按Enter。
- 选择文本框右侧的菜单以将Text Documents (*.txt)更改为All Files (*)。
- 双击文件列表中的hosts以在(hosts)记事本(Notepad)中打开 hosts 文件进行编辑。
编辑主机文件(Editing The Hosts File)
hosts 文件中有几个示例向您展示如何格式化您的条目。您必须先放 IP 地址,然后是域名,并且两者必须至少用一个空格隔开(制表符是格式化它们的好方法)。
这里有些例子:
127.0.0.1 www.google.com
13.249.79.104 www.bing.com
正如您在上图中看到的并从 hosts 文件中的说明中看到的那样,#符号用作注释,这意味着它后面的任何内容都被避免。这就是为什么我们上面给出的两个示例行在这个 hosts 文件中很有用;他们没有被那个符号跟随。
相关(Related):编辑主机文件以添加本地 DNS 查找(Edit Hosts File to Add Local DNS Lookup)
如果你想屏蔽带有 hosts 文件的网站(block websites with the hosts file),例如YouTube、Amazon、Google、Reddit.com等,你可以输入一个虚假的 IP 地址,比如0.0.0.0。
注意(Note):当您输入要与特定 IP 地址关联的站点的URL时,请务必排除开头部分并仅键入子域,例如 www 或您要添加的站点的任何内容。
保存主机文件(Saving The Hosts File)
以下是我们保存主机文件的方法,以确保无论您使用哪种操作系统、选择哪种文本编辑器或如何配置Windows设置,它都能正常工作:
- 转到文件(File )>另存为(Save As)。
- 将Save as type(Save as
type)选项更改为All Files (*)。
- 将该文件重命名为hosts.backupfile,然后将其保存到您的桌面。
- 关闭记事本(Close Notepad),然后在桌面上找到刚刚创建的 hosts 文件。你想删除 . backupfile
名称的一部分,只留下主机(hosts)
(单词后没有句点或文本)。
- 完成此操作后,从文件单击桌面上的其他内容,并在询问时确认名称更改。
- 复制主机文件。
- 打开您之前打开的主机文件位置(etc文件夹)。
- 粘贴您从桌面复制的主机文件,并确认是否有任何覆盖提示(如果您看到它们)。
How to Edit the Hosts File in Windows
Τhe hosts file is a text-baѕed file located in the
same рlace in all modеrn Windowѕ versions. Most people don’t ever need to get
into it, but if you do, you’ll need to know how to оpen and edit the hoѕts
file.
However, in some versions of Windows, editing the
hosts file directly isn’t allowed. In fact, you’ll get an error if you try to
save the changes. Fortunately, there’s a simple tip you can use to edit the
hosts file in Windows 10, 8, 7, Vista, or XP.
What Does The Hosts File Do?
The purpose of the hosts file was originally to be what DNS servers do now – map friendly domain names to unfriendly IP addresses.
It’s much easier to remember a domain name like YouTube.com than an IP address like 172.217.1.238. Instead of having to type the IP address into the address bar each time you want to watch YouTube videos, you can just enter the domain name, and the DNS server your device is using will map the name to the right address so you can get to the webpage a lot easier.
The hosts file, since it serves as a sort of DNS
server, works the same way. You can enter an IP address that a domain name
should be resolved to. In other words, you could order your computer to load a
different IP address than the DNS server would, essentially letting you do a
few cool things.
Why Edit The Hosts File?
One reason to edit the hosts file is if you want a super basic way to block websites from loading on your computer. Enter an invalid or wrong IP address for the site you want to block, and each time you try accessing it, the hosts file will load the IP address you chose. Depending on what you picked, it might completely block the website.
You can use the same technique to make the hosts file block malicious sites. There are even lists of hosts file entries (such as this one) you can download and import into your hosts file to block hundreds of malicious sites or ads.
Another use for the hosts file is to speed up your web browsing. If the DNS server you’re using is down or not working quickly enough, manually entering the IP address and domain name mappings in your hosts file lets your computer find those addresses nearly instantly instead of relying on the DNS server.
Tip: Learn how to use alternate DNS servers to get faster internet access.
Windows Hosts File Location
You need to know where to go to find the hosts file
before you can open it for editing. This is the hosts file location in Windows
10 through Windows XP:
C:\Windows\system32\drivers\etc
How To Edit The Hosts File
The hosts file is a text file, meaning that you need a text editor to make changes to it. However, since the hosts file is super basic and doesn’t need to deal with hyperlinks, images, etc., you can use a basic text editor (i.e., there’s no need to use a full-fledged editor like Microsoft Word).
Opening The Hosts File
Let’s start with simply opening the hosts file. We’ll use Notepad, which is built into
Windows 10 down through Windows XP.
- Open the Run dialog box by holding down the Windows key and then pressing R once.
- Type notepad and
then press Enter to open the
program.
- Go to File > Open.
- In the file name text box at the bottom, type (or copy and
paste) the hosts file location shown above and then press Enter.
- Select the menu to the right of the text box to change Text Documents (*.txt) to All Files (*).
- Double-click hosts from the list of files to open
the hosts file for editing in Notepad.
Editing The Hosts File
There are a few examples in the hosts file to show you
how to format your entries. You have to put the IP address first followed by
the domain name, and the two have to be separated by at least one space (a tab
is a good way to format them).
Here are some examples:
127.0.0.1 www.google.com
13.249.79.104 www.bing.com
As you can see in the image above and read from the directions in the hosts file, the # symbol is used as a comment, meaning that anything that follows it is avoided. This is why the two example lines we give above are useful in this hosts file; they’re not being followed by that symbol.
Related: Edit Hosts File to Add Local DNS Lookup
If you want to block websites with the hosts file, such as YouTube, Amazon, Google, Reddit.com, etc., you can enter a false IP address like 0.0.0.0.
Note: When you enter the URL of
the site you want to associate with a specific IP address, be sure to exclude
the beginning part and only type the subdomain, such as www or whatever it
happens to be for the site you’re adding.
Saving The Hosts File
Here’s how we’ll save the hosts file to make sure it
works no matter which operating system you use, which text editor you picked,
or how your Windows settings are configured:
- Go to File > Save As.
- Change the Save as
type option to All Files (*).
- Rename the file to hosts.backupfile,
and then save it to your desktop.
- Close Notepad and then find the hosts file you just created
on the desktop. You want to delete the .backupfile
part of the name and leave only hosts
(with no period or text after the word).
- After you do that, click away from the file to something else
on the desktop, and confirm the name change when asked.
- Copy the hosts file.
- Open the hosts file location you had open before (the etc folder).
- Paste the hosts file you copied from the desktop, and confirm
any overwrite prompts if you see them.