There are several ways to block websites on your Windows PC, but the easiest of them is by editing the native hosts file. Few lines of code and you can block any website, malware, annoying ads seamlessly on your entire machine. However, as simple as it sounds, the process is quite tedious and chances are you might corrupt the hosts file altogether.
So generally, there are many ways to edit and create hosts files and many popular hosts file editor in Windows 10. The conventional way is to manually edit the hosts file and here is how you do it.
Manually edit Host Files
Just a heads up, If you are not comfortable with huge lines of code and the idea of editing configuration files, kindly skip to the next method. Manually editing hosts file is tedious because you cannot directly edit the hosts file in its original location, if you did, you will get an error like ” you don’t have permission to save hosts file windows 10″. So, we’ll have to move it from the below-mentioned location to the Desktop.
You will need Administrator privileges for this activity
After you have moved the hosts file, double click on it. Since the hosts file doesn’t have an extension, Windows doesn’t know which software should be used to open it. Hence, you will see a pop-up asking you to choose an app to open the file. Select Notepad and hit Enter. You will be presented with an editable format of the hosts file which looks like the following screenshot.
Now, the hosts file has several lines of code which are prefixed by a “#”. This means the lines are commented and are of no significance. For our use-case, in order to block a website, we need to enter the website name prefixed by “127.0.0.1” or “0.0.0.0”. For example, if I want to block Facebook.com on my computer, I will attach the following line at the end of the file.
This line ensures that whenever I enter Facebook.com on any browser on my local system, the connection will be redirected to my own IP Address. Since my local system doesn’t have a web server running, I will end up with a “Website not reachable” error message.
Move the file back to the “C:\Windows\System32\drivers\etc” location. You will have a pop-up asking for Administrator permissions, just continue with it. Once done, open your preferred web browser, clear cache and now the website will be blocked throughout your system.
- Cannot Directly edit the source file.
- No way to have multiple hosts files.
- No option of importing hosts files online.
1. Host File Editor
The problem with the manual method is the complexity of the steps and high chances of rendering the hosts file corrupt. To escape the trouble, you can use a Host File Editor. Host File Editor lets you directly edit the hosts file without moving the original file to and fro. There are tons of Host File Editors available on GitHub but the one I prefer is Hosts File Editor.
The tool is pretty simple and to the point. You have the old school Excel cells where you have to enter the IP Address, followed by the website URL and comments(optional). For example, below are a couple of entries made in the Hosts File Editor.
Once you are done making entries, make sure you check the checkbox besides them to make the entry active. If you leave the checkbox unchecked, the particular entry would be commented in the original hosts file making it ineffective. Lastly, click on the Save button to write your changes to the original hosts file.
The best thing about the editor is you can enable and disable the website blocking on the go with a mere click of a button. Traditionally, I would have to delete or comment every entry in the hosts file. But, with the hosts file editor, I just have to click on “Disable Hosts File” and the blocked websites will be available. To enable the hosts file back, click again on the same “Disable Hosts File” option.
When you close the Hosts File Editor, it is minimized rather than closing. So, head over to the bottom tray and quit the application.
2. Import hosts file list
The problem with blocking websites via Hosts file is to you need to manually enter the name of all the websites. Suppose, you want to block adult websites for the entire network. Imagine how gruesome it would be to enter 5000+ website names and IP Addresses in the Host File Editor. The better way to deal with this method is to use an existing Host File over the Internet.
A fabulous example is this GitHub page maintained by Steven Black which is a repository of several hosts file unified. These files serve various purpose from blocking adult content to ads, malware, and fake news. Now, instead of directly copy-pasting the hosts file, a better way would be to use HostsMan, a hosts file manager for Windows.
To start with, install the software. Next, make sure you run it as an Administrator. As soon as the software launches, you will see a button called “Select Sources”. Click on it and HostsMan will display a popup with pre-populated sources.
You can read the description of the pre-populated Host sources, visit their website to verify the contents. But, since we want to add our own custom source, we will have to manually make an entry. To do that, click on the “Add Source” button at the bottom left.
The next page shows blank text boxes for you to enter a suitable Host name, host file URL. After you have entered the URL, click on the “Test connection” button to verify the file. You can find URL, either with a quick Google search or use this URL and scroll down at the bottom of the page to find the ‘Host file recipe’ table.
If you get a message saying “This source seems to be compatible”, you can go ahead and click on OK.
HostsMan also allows you to backup your hosts file prior to an update. It even recognizes changes made by a different tool to the hosts file.
3. Multiple Hosts Files
If you use your machine for web development, there are chances that you might need multiple hosts files. For example, you might need a hosts file to redirect a DNS Names to your own IP Address but at the particular instance, you need the URL to be redirected to a remote IP Address. The manual way would be making redundant entries in the hosts file and then commenting out accordingly. But, having multiple hosts files and using a DNS switcher makes the task easier.
You can use a tool called Hosts Switcher. It lets you create multiple hosts file within it and then activate a particular hosts file at a time. This eases the entire process and you can even backup hosts file using the same tool.
Download Hosts Switcher
All in all, I would really recommend you to use HostsMan over any other Hosts File Editor. For a one-off use case, Hosts File Editor is the ideal option. Another use case would be putting up GitHub hosts file on the router so you can block websites for the entire network. Now, routers don’t have the ability to dynamically update hosts files, so you can setup a Raspberry Pi to do the same job for you. Here’s a Python Script you can run on the Raspberry Pi. It automates the task of fetching hosts file online and updating it on the router.
While, Host file is useful but limited. For example, it does not allow for wildcards like DNS. So, you may want to look into DNS blocking using services like OpenDNS.
For any issues or queries, let me know in the comments below.