Windows 主机文件(Windows Hosts file)是Windows用来控制和映射 IP 地址的文件。通过编辑Hosts文件,可以自定义Windows以阻止或重定向特定网站,甚至是程序和应用程序使用的协议。(Windows)
在本文中,我们将引导您完成正确编辑HOSTS 文件(HOSTS file)的步骤。此外,如果您想观看该过程的视频,请随时查看我们的YouTube 视频(YouTube video)。
在 Windows 中编辑主机文件
要开始编辑Windows Hosts 文件(Windows Hosts file),您首先需要找到它。打开Windows 资源管理器并单击(Windows Explorer and click)这台电脑( This PC)或我的电脑(My Computer)。双击 C:,然后是 Windows文件夹并向下滚动页面,直到到达 System32文件夹。在该文件夹中,打开驱动程序(drivers),然后打开等(etc)。您现在将看到几个文件,其中之一是hosts。
现在,请注意hosts文件的文件类型(file type)列为File。因为没有设置默认程序(default program)来打开这样的文件类型(file type),所以双击主机文件只会给您一个Windows 提示(Windows prompt),询问您要使用哪个程序来打开文件。
在此提示中,您可以选择使用记事本(Notepad)编辑 hosts 文件。因此,只需单击以选择记事本(Notepad),然后单击确定(OK)按钮。从那里,记事本(Notepad)将启动主机文件信息。
演示了这种打开 hosts 文件的方式,以显示 hosts 文件在Windows中的实际位置,但您将无法编辑它,因为它是一个系统文件(system file)。为了编辑文件,您必须先打开记事本,以(Notepad)管理员(Administrator)身份运行。
单击开始(Start)并输入记事本(Notepad),但不要单击(t click)记事本(Notepad)打开它。相反(Rather),右键单击(right-click)记事本列表(Notepad listing)以调出上下文菜单(context menu)。选择选项以管理员身份运行(Run as Administrator)。
打开记事本,选择File > Open。导航到 C:\Windows\System32\drivers\etc。您将看到一个空白屏幕(blank screen),显示提示No items match your search。使用下拉菜单将Text Documents (*.txt) to All Files更改为所有文件。现在,您可以选择hosts文件并单击Open。
将文件添加到主机文件非常简单。主机文件使用以下格式:
IP Address exampledomain.com
使用 HOSTS 文件阻止网站
在Windows(Windows)中阻止网站就像在 hosts 文件底部键入以下内容一样简单:
127.0.0.1 www.exampledomain.com
所以,如果我想阻止像 www.nytimes.com 这样的网站,我可以添加以下行:
127.0.0.1 www.nytimes.com
我们实际上告诉Windows网站 www.nytimes.com 应该重定向到IP 地址 127.0.0.1(IP address 127.0.0.1),这只是我们本地系统上的环回地址。(loopback address)如果您的计算机上没有本地网站设置(website setup),您只会在网络浏览器中看到一个(web browser)错误页面(error page)。
很酷(Pretty cool),嗯!?显然,您可以看到如何以多种不同方式使用它:恶作剧、家长控制等。如果您不想以这种方式阻止该网站,您也可以将其重定向到另一个网站。为此,您必须先找到其他站点的IP 地址(IP address)。
为此,只需打开命令提示符(command prompt)(单击Start 并输入(Start and type)CMD )(CMD)并输入以下命令:
ping examplewebsite.com
在我的示例中,我对 Adob e.com 执行(Adobe.com)了 ping 操作。IP 地址(IP address)为192.150.16.117 。现在我可以简单地将这个号码插入到 www.nytimes.com 前面的 hosts 文件中。
现在,当我访问 www.nytimes.com 时,我会重定向到Adobe.com!不错(Nice)!请注意,如果这不适用于您输入的网站,则可能是因为您使用的URL 。例如,如果您使用www.nytimes.com而不是没有www的(www)nytimes.com ,它会有所不同。访问该网站并查看您要重定向的网站的确切URL 。您应该始终先尝试不使用 www 以查看是否有效。
如果网站使用像Google.com之类的(Google.com or something)HTTPS,如果您使用主机名(host name),它仍然应该重定向。没有办法在HOSTS 文件中指定网站的(HOSTS file)HTTPS 版本(HTTPS version),但如果您只使用主机名(host name)(即 google.com),它应该重定向网站的HTTPS和非HTTPS 版本。(HTTPS version)
最后,您可以使用 hosts 文件在网络上为您自己的设备创建简单的快捷方式。例如,我的路由器位于我的家庭网络上的(home network)192.168.1.3,但我可以将以下行添加到我的主机文件中,然后只需在地址栏中输入myrouter.com 。
myrouter.com 是否真的是一个网站并不重要,因为首先读取主机文件并且您被重定向到文件中指定的IP 地址(IP address)。值得注意的是,并非所有浏览器都可以使用 hosts 文件,因此如果它不起作用,则可能是问题所在。我使用 IE、Microsoft Edge、Chrome 和 Firefox(Chrome and Firefox)对其进行了测试,它适用于所有浏览器。
总体而言,hosts 文件仍然有用,即使在Windows 10中也是如此。它在Windows 8、 7、Vista等中仍然可以正常工作。如果您有任何问题,请随时发表评论。享受!
Edit the Windows Hosts File to Block or Redirect Websites
The Windows Hosts file is a file that Windows uѕes to control and map IP addresses. By editing the Hosts file, Windows can be cυstomized to block or redirect specific websіtes and even protocols that are used by programs and appliсations.
In this article, we’ll walk you through the steps to correctly edit the HOSTS file. Also, if you would rather watch a video of the process, feel free to check out our YouTube video.
Edit Hosts File in Windows
To get started editing the Windows Hosts file, you first need to locate it. Open Windows Explorer and click on This PC or My Computer. Double-click on C:\, then the Windows folder and scroll down the page until you reach the System32 folder. Inside of that folder, open drivers and then open etc. You’ll now see several files, one of which is hosts.
Now, notice that the file type for the hosts file is listed as File. Because there is no default program set to open a file type like this, double clicking the hosts file will simply give you a Windows prompt, asking you which program you would like to use to open the file.
From this prompt, you can choose to edit the hosts file with Notepad. So, simply click to select Notepad and click the OK button. From there, Notepad will launch with the hosts file information.
This way of opening the hosts file was demonstrated to show where the hosts file is actually located within Windows, but you won’t be able to edit it because it’s a system file. In order to edit the file, you have to open Notepad first, running as an Administrator.
Click on Start and type in Notepad, but don’t click on Notepad to open it. Rather, right-click the Notepad listing to bring up the context menu. Select the option Run as Administrator.
With Notepad open, select File > Open. Navigate to C:\Windows\System32\drivers\etc. You will get a blank screen that displays the prompt No items match your search. Change Text Documents (*.txt) to All Files using the drop down menu. Now, you can select the hosts file and click Open.
Adding files to the hosts file is very simple. The hosts file uses the format:
IP Address exampledomain.com
Block Websites using HOSTS File
Blocking a website in Windows is as simple as typing the following into the bottom of the hosts file:
127.0.0.1 www.exampledomain.com
So, if I wanted to block a website like www.nytimes.com, I could just add the following line:
127.0.0.1 www.nytimes.com
What we are actually telling Windows is that the website www.nytimes.com should redirect to the IP address 127.0.0.1, which is just the loopback address on our local system. If you don’t have a local website setup on your computer, you’ll just get an error page in your web browser.
Pretty cool, huh!? Obviously, you can see how this can be used in several different ways: a prank, parental control, etc. If you didn’t want to block the website in that way, you could also redirect it to another website. In order to do this, you have to find the IP address of the other site first.
To do that, just open a command prompt (click on Start and type in CMD) and type in the following command:
ping examplewebsite.com
In my example, I pinged Adobe.com. The IP address is 192.150.16.117. Now I can simply plug that number into my hosts file in front of www.nytimes.com.
Now when I visit www.nytimes.com, I get redirect to Adobe.com! Nice! Note that if this doesn’t work for the websites you are entering, it could be because of the URL you are using. For example, it makes a difference if you use www.nytimes.com as opposed to nytimes.com without the www. Visit the website and see exactly what the URL is for the website you want to redirect. You should always try without the www first to see if that works.
If the website uses HTTPS like Google.com or something, it should still redirect if you use the host name. There is no way to specify the HTTPS version of a website in the HOSTS file, but it should redirect the HTTPS and non-HTTPS versions of the website if you use just the host name (i.e. google.com).
Lastly, you can use the hosts file to create simple shortcuts to your own devices on the network. For example, my router is at 192.168.1.3 on my home network, but I could add the following line to my hosts file and simply type in myrouter.com into my address bar.
It doesn’t really matter if myrouter.com is actually a website or not because the hosts file is read first and you are redirected to the IP address specified in the file. It’s worth noting that not all browsers may use the hosts file, so if it’s not working, that could be the issue. I tested it using IE, Microsoft Edge, Chrome and Firefox and it worked on all of the browsers.
Overall, the hosts file is still useful, even in Windows 10. It also still works just fine in Windows 8, 7, Vista, etc. If you have any questions, feel free to comment. Enjoy!