您为什么想知道如何阻止网站?也许您想让您的孩子在互联网上保持安全(keep your kids safe on the Internet)?也许您想阻止分散注意力的网站以免拖延(block distracting websites so you don’t procrastinate)?它还可用于阻止嵌入广告的跟踪网站。
无论出于何种原因,您都可以使用称为 hosts 文件的文件来简单地阻止Windows 10上的网站。(Windows 10)
下面的文字可能有点技术性,特别是如果您从未深入了解Windows系统文件的具体细节。如果您想快速了解一下,请务必先观看我们简短易懂的YouTube 视频(YouTube video)。
什么是主机文件?(What Is The Hosts File?)
您听说过DNS(域名系统(Domain Name System))这个术语吗?它指的是分布在Internet上的文件系统,可帮助将诸如(Internet)helpdeskgeek.com之类的域名解析为其实际的Internet 协议(Internet Protocol)(IP) 地址,该地址恰好是13.225.198.106。这有助于您的计算机在您想查看此站点时知道要与哪个服务器通信。
在DNS于 1984 年问世之前,这是通过您计算机上名为 hosts 文件的本地文件完成的。那时(Back)计算机没有连接到许多其他计算机。最多,我们可能连接到几十个。将所有这些信息保存在一个普通的旧文本文件中很容易。主机文件存在于今天。
主机文件如何工作?(How Does The Hosts File Work?)
hosts 文件允许计算机查找其他计算机名称,例如localhost或域名,例如helpdeskgeek.com,它们的 IP 号就在文件中的名称旁边。这有点像您朋友的姓名如何与您手机中的电话号码相关联。您无需记住或输入号码,只需找到此人的姓名即可。
在我们进一步讨论之前,重要的是要了解某些 IP 地址是为本地使用而保留的。这意味着它们只能用于指向您直接网络中的计算机和设备。这意味着在您的通信连接到Internet之前的任何事情。
例如,IP 地址127.0.0.1始终表示您正在使用的计算机,也称为localhost。尝试在您的网络浏览器中输入。它可能会弹出一个空白页面。
还要了解www.website.com和website.com并不完全相同。为了让他们把我们带到同一个网站,他们都必须解析到同一个 IP 地址。
使用主机文件阻止网站(Blocking Websites With The Hosts File)
- 以管理员(Administrator)身份打开记事本(Notepad )。
- 单击文件(File )>打开(Open)。
- 导航到C:\Windows\System32\Drivers\etc
- 在那里,将文件类型从Text Documents (*.txt)更改为All Files (*.*)。
- 现在您可以看到主机(hosts)文件。打开它。
- 转到页面的最后一行并添加您自己的行。假设我们正在阻止Facebook。
输入127.0.0.1(127.0.0.1)的 localhost IP 。按 Tab 键并输入facebook.com。按 Enter(Press Enter)转到下一行。像上面那样创建第二行,但使用www.facebook.com而不是 facebook.com。
网站被封锁了吗?(Is The Website Blocked?)
打开命令提示符并运行ping facebook.com然后运行ping www.facebook.com。您应该看到它尝试 ping 的 IP 地址是 127.0.0.1。
现在打开您的网络浏览器并转到 facebook.com。它可能仍然加载!为什么?Facebook 的真实 IP 可能仍位于浏览器缓存中,浏览器将首先使用该 IP。删除浏览器的缓存(Delete your browser’s cache)并再次尝试Facebook。您现在应该看到一个错误页面。
我不想看到错误页面(I Don’t Want to See an Error Page)
您可以尝试将站点重定向到另一个 IP 地址,但现在效果不佳。恶意编辑主机文件曾经是黑客的常见攻击。现在,Web 浏览器会仔细检查证书以防止这种情况发生。但是,您仍然可以尝试。
另一种看不到错误页面的方法是在您的计算机上安装和设置 Web 服务器,(install and set up a web server on your computer)并让 hosts 文件指向该服务器。也许让它显示一个页面,上面写着“回去工作!” 或列出您的一些目标作为远离社交媒体的动力。
我还能用主机文件做什么?(What Else Can I Do With The Hosts File?)
您可以使用相同的技术,但粘贴各种恶意软件和跟踪广告网站的URL 。(URLs)您可以将恶意软件和广告网站(made hosts file of malware and ad sites)的预制主机文件复制并粘贴(pre)到主机文件中。(--)在撰写本文时,它最后一次更新是在2020 年3 月。(March)
How To Block Websites On Windows Using The Hosts File
Why would you want to know how to block webѕites? Maybe you want to keep your kids safe on the Internet? Maybe you want to block distracting websites so you don’t procrastinate? It can also be used to block tracking sites that are embedded in ads.
Whatever your reason, you can simply block websites on Windows 10 using something called the hosts file.
The text below can get a bit technical, especially if you have never gotten into the nuts and bolts of Windows system files. If you want a quick overview, be sure to check out our short and easy to follow YouTube video first.
What Is The Hosts File?
Have you heard the term DNS (Domain Name System)? It refers to a system of files distributed around the Internet that help resolve domain names like helpdeskgeek.com into its actual Internet Protocol (IP) address which happens to be 13.225.198.106. That helps your computer know what server to talk to when you want to view this site.
Before DNS came into being in 1984, this was done through a local file on your computer called the hosts file. Back then computers weren’t connected to many other computers. At most, we may have connected to a few dozen. It was easy to keep all that information in a plain old text file. The host file lives on today.
How Does The Hosts File Work?
The hosts file allows the computer to look up other computer names, like localhost, or domain names, like helpdeskgeek.com and the IP number for them is right beside the name in the file. It’s a bit like how your friends’ names are associated with their phone numbers in your phone. You don’t need to remember or punch in the number, just find the person’s name.
Before we go further it’s important to understand that some IP addresses are reserved for local use. That means that they can only be used to point to your computer and devices within your immediate network. That means anything before your communication connects to the Internet.
For example, the IP address 127.0.0.1 always means the computer you’re working on, also known as localhost. Try typing that in your web browser. It will likely bring up a blank page.
Also understand that www.website.com and website.com are not the exact same thing. For both of them to take us to the same website both must resolve to the same IP address.
Blocking Websites With The Hosts File
- Open Notepad as Administrator.
- Click on File > Open.
- Navigate to C:\Windows\System32\Drivers\etc
- Once there, change file type from Text Documents (*.txt) to All Files (*.*).
- Now you can see the hosts file. Open it.
- Go to the last line of the page and add your own line. Let’s say we’re blocking Facebook.
Enter the localhost IP of 127.0.0.1. Press the tab key and enter facebook.com. Press Enter to go to the next line. Create a second line like the one above, but use www.facebook.com instead of just facebook.com.
Is The Website Blocked?
Open the command prompt and run ping facebook.com then run ping www.facebook.com. You should see that the IP address it’s trying to ping is 127.0.0.1.
Now open your web browser and go to facebook.com. It might still load! Why? Facebook’s real IP may still be sitting in the browser cache and the browser will use that IP first. Delete your browser’s cache and try Facebook again. You should now see an error page.
I Don’t Want to See an Error Page
You could try redirecting the site to another IP address, but these days that won’t work well. Maliciously editing the hosts file used to be a common attack by hackers. Now, web browsers check carefully for certificates to prevent this. But, you can still try.
Another way to not see an error page is to install and set up a web server on your computer and have the hosts file point to that. Maybe have it display a page that says, “Get back to work!” or lists some of your goals as motivation to stay off social media.
What Else Can I Do With The Hosts File?
You can use the same technique but paste in the URLs of various malware and tracking ad sites. There is a pre--made hosts file of malware and ad sites that you can copy and paste into your hosts file. As of writing this, it was last updated in March, 2020.