当您在浏览器的地址栏中键入网站 URL(website URL)时,会向一种称为域名服务器(domain name server)(domain name server)的 Internet 服务器发送请求。此服务器获取您键入的URL,然后检查为托管您要查找的内容的实际服务器列出了哪些特定 IP 地址。
这样做的问题是,如果名称服务器出现问题,您将无法访问正确的站点。更糟糕的是,如果名称服务器被劫持,您最终可能会进入一个假网站!好消息是您可以手动指定特定 IP 地址和网站地址之间的链接,只需将本地DNS查找添加到“主机”文件即可。
什么是主机文件?
主机文件很容易理解。它只是一个纯文本文件。顺便说一句,这就是为什么您应该只使用Notepad(Notepad)之类的东西来编辑文件的原因,它不会尝试更改格式或向文件添加额外的格式。
在 hosts 文件中,您可以添加 IP 地址列表以及 IP 地址应指向的 Web 地址。您可以组合任何(any )IP 地址和 Web 地址,即使它们实际上并没有在一起。如果您愿意,可以将Bing.com(Bing.com)的实际 IP 地址指向 Google.com。
在向名称服务器发送请求之前,Windows 将始终首先检查主机文件。因此,如果您在 Web 浏览器中键入的地址列在 hosts 文件中,您将被重定向到该文件中列出的 IP 地址。
什么是本地 DNS 查找?
上一段中描述的过程是本地DNS查找。您的计算机会自行从本地磁盘查找 IP 地址,而无需联系外部服务器。就是这么简单!
为什么要将本地 DNS 查找添加(Add Local DNS Lookup)到主机文件(File)?
您希望将本地DNS查找添加到主机文件有几个原因。我们已经在文章开头提到了一些原因,但是人们已经找到了很多创造性的方法来使用这个简单的功能。
向主机文件添加条目的一个重要原因是速度。进行本地查找总是比使用外部服务器更快。特别是如果该服务器不可靠。将最重要或最常用的站点放在 hosts 文件中意味着您永远不必担心DNS服务会导致访问问题。
您还可以使用主机文件来阻止(block sites)您不希望该计算机访问的站点。您所要做的就是将一个 IP 地址放入该站点的主机文件中,该站点要么不去任何地方,要么指向一个良性 IP 地址。常见的重定向包括 0.0.0.0 和 127.0.0.1,这就是所谓的“环回”地址。查看我们的YouTube 视频(YouTube video)了解更多详情。
如果您有路由器、IP 摄像机、网络附加存储等本地设备,您可以使用主机文件为其 IP 地址提供一个易于记忆的名称。
使用预制主机文件
如果将数十个甚至数百个站点添加到您的主机文件听起来很乏味,那么您并不孤单!好消息是网络上有很多地方可以找到预制列表,您可以简单地复制并粘贴到您自己的主机文件中。
这样做的主要问题是这些列表中可能隐藏着恶意重新路由。这意味着您要么需要手动验证每个 IP 地址,要么确保您信任列表的来源。
在Windows 10中编辑主机(Hosts) 文件(File)
由于主机文件留下了一些恶作剧的空间,您不能只是打开它并进行编辑。您需要计算机上的管理员权限才能进行更改。在对主机文件进行任何更改之前,我们强烈建议您将其原始内容复制并粘贴到另一个文本文件中,以防万一出现问题!
要在Windows 10(Windows 10)中修改 hosts 文件,请执行以下步骤:
- 打开开始菜单(Start Menu)并输入“记事本”。
- 出现记事本(Notepad)后,右键单击它并选择“以管理员身份运行”。(Run)
- n 记事本,单击打开(Open)并前往 c:WindowsSystem32Driversetchosts 并打开主机文件。请记住(Remember)将文件类型更改为“所有文件”。
- 您应该会看到来自Microsoft的默认主机文件。
- 不要担心文档中已有的任何文本。您可以保持原样并在文件底部添加您自己的条目。任何以“#”符号开头的行都被视为注释,不用于名称查找。
向列表中添加本地查找非常简单。只需(Just)写下网站的 IP 地址,后跟一个空格,然后是网站地址。
请记住(Remember)在关闭文件时保存您添加的内容。这就是你所要做的!现在,您的计算机在访问您指定的站点时将跳过整个DNS查找过程。(DNS)
如何查找网站的 IP 地址
如果您不知道要添加到主机文件中的站点的 IP 地址怎么办?在 Windows 10 中使用命令提示符(Command Prompt)查找站点的 IP 地址实际上非常容易。方法如下:
- 打开开始菜单并输入“cmd”。
- 命令提示符(Command Prompt)打开后,键入“tracert”,后跟一个空格和要检查的站点。在本例中,它是“google.com”。然后按键盘上的Enter键。
- 在这里,您将看到该站点的 IP 地址。您现在可以在主机文件中使用此地址。
拥有最多的主机
谁会想到隐藏在Windows文件夹深处的这么小的文本文件会变得如此有用?现在你是一个合适的本地查找专家,控制你的计算机访问网站的方式。
How to Add a Local DNS Lookup to Hosts File
When you type a website URL into the address bar of your browser, a request is sent to a type of internet server known as a domain name server. This server takes the URL you typed and then checks which specific IP addresses are listed for the actual servers that host the content you’re looking for.
The problem with this is that if something’s wrong with the name server, you’re not going to get access to the correct site. Even worse, if the name server has been hijacked, you might end up at a fake site! The good news is that you can manually specify the link between specific IP addresses and website addresses, simply by adding a local DNS lookup to your “hosts” file.
What Is The Hosts File?
The hosts file is simple to understand. It’s just a plain text file. Incidentally, this is why you should just edit the file with something like Notepad, which won’t try to change the format or add extra formatting to the file.
Inside the hosts file, you can add a list of IP addresses along with the web address that the IP address should point to. You can combine any IP address and web address, even if they don’t actually go together. You could make the actual IP address for Bing.com point to Google.com if you wanted to.
Windows will always check the hosts file first before sending a request to a name server. So if an address you type into your web browser is listed in the hosts file, you’ll be redirected to the IP address listed in the file.
What Is A Local DNS Lookup?
The process described in the previous paragraph is a local DNS lookup. Your computer looks up the IP address by itself, from your local disk, without needing to contact an external server. It’s that simple!
Why Add Local DNS Lookup To The Hosts File?
There are a few reasons you’d want to add a local DNS lookup to your hosts file. We’ve already mentioned a few reasons at the outset of the article, but people have found quite a few creative ways to use this simple feature.
One important reason to add entries to your hosts file is speed. It will always be faster to do a local lookup than going out to an external server. Especially if that server is unreliable. Putting your most important or most frequently used sites in the hosts file means you never have to worry about your DNS service causing access issues.
You can also use the hosts file to block sites that you never want that computer to access. All you have to do is put an IP address into the hosts file for that site which either doesn’t go anywhere or points to a benign IP address. Common redirections include 0.0.0.0 and 127.0.0.1, This is the so-called “loopback” address. Check out our YouTube video for more details on that.
If you have local devices such as routers, IP cameras, network-attached storage and so on, you can use your hosts file to give their IP addresses an easy to remember name.
Using Premade Hosts Files
If it sounds pretty tedious to add dozens or even hundreds of sites to your hosts file, then you aren’t alone! The good news is that there are plenty of places on the web where you can find pre-made lists that you can simply copy and paste into your own hosts file.
The main problem with this is that there could be malicious reroutes hidden in these lists. Which means you either need to verify each IP address by hand or make sure that you trust the source of the list.
Editing The Hosts File In Windows 10
Since the hosts file leaves some room for mischief, you can’t just open it and edit away. You’ll need administrator privileges on your computer in order to make changes. Before you make any changes to your hosts file, we strongly recommend you copy and paste it’s original contents into another text file just in case something goes wrong!
To modify your hosts file in Windows 10, follow these steps:
- Open the Start Menu and type “Notepad”.
- Once Notepad appears, right-click on it and select “Run as administrator”.
- n Notepad, click Open and head to c:\Windows\System32\Drivers\etc\hosts and open it the hosts file. Remember to change the file type to “All Files”.
- You should see this default hosts file from Microsoft.
- Don’t worry about any of the text already in the document. You can just leave it as is and add your own entries at the bottom of the file. Any line that starts with a “#” symbol is treated as a comment and not used for name lookups.
Adding a local lookup to the list is super-easy. Just write down the IP address of the site, followed by a space and then the website’s address.
Remember to save what you’ve added when closing the file. That’s all you have to do! Now your computer will skip over the whole DNS lookup process when accessing the sites you’ve specified.
How To Find A Website’s IP Address
What if you don’t know the IP address of a site you want to add to your hosts file? It’s actually pretty easy to find a site’s IP address using the Command Prompt in Windows 10. Here’s how:
- Open the Start Menu and type “cmd”.
- After the Command Prompt opens, type “tracert” followed by a space and the site you want to check. In this example, it’s “google.com”. Then press the Enter key on your keyboard.
- Here you’ll see the IP address of the site.You can now use this address in your hosts file.
The Hosts With The Most
Who would think that such a small text file hidden deep in the Windows folder could turn out to be so useful? Now you’re a proper local lookup guru, taking control of how your computer accesses websites.