The Hosts file acts as a local DNS and maps hostnames to IP addresses. By adding website URLs to the Hosts file and mapping them to the localhost IP address, you can block access to those sites since they will point to your own computer.
In this small guide, I will explain step-by-step how to use Command Prompt to edit the Hosts file and block websites in Windows.
Prerequisites
Before getting started, make sure you have:
- Windows 7, Windows 8, Windows 10, or Windows 11 operating system. These steps may also work on previous Windows versions.
- Administrator access on your computer. Editing the Hosts file requires admin privileges.
Step 1 - Create a Website List Text File
First, create a text file containing the list of websites you want to block, one site per line. This will make it easy to add them to the Hosts file later.
To do this:
- Open Notepad on your computer. You can do this by pressing the Windows key and typing "Notepad", then clicking on the Notepad app.
- Enter the websites you want to block, one per line like this:
- Save the text file on your desktop as "blockedsites.txt" or any other name you prefer.
www.facebook.com
www.youtube.com
www.netflix.com
Step 2 - Open Command Prompt as Administrator
To edit the Hosts file, you need to open Command Prompt with admin privileges. Here's how:
- Press the Windows key on your keyboard and type "Command Prompt".
- Right-click on the Command Prompt app result and select "Run as administrator". This will open an elevated Command Prompt window.
- If prompted by User Account Control, click Yes to allow it to make changes with administrative privileges.
Step 3 - Open the Hosts File in Notepad
Now we need to open the Hosts file itself in Notepad using the following command:
notepad c:\windows\system32\drivers\etc\hosts
This will open the Hosts file in a new Notepad window so we can edit it.
Step 4 - Add Websites to Block
With the Hosts file open in Notepad, go to the bottom of the file and enter the websites you want to block.
For each website URL, add an entry like this:
127.0.0.1 www.facebook.com
127.0.0.1 www.youtube.com
Where the IP address 127.0.0.1 points back to your own computer (localhost) followed by the website URL.
Copy and paste the list of URLs to block from the text file you saved earlier.
Step 5 - Save the Hosts File
Once you have added all the sites to block, save and close the Hosts file in Notepad. When prompted, click Yes to confirm saving the file.
Step 6 - Flush the DNS Cache
For the website blocking to take effect, you need to flush and refresh the DNS cache on your system.
Do this by running the following command in the Command Prompt:
ipconfig /flushdns
Now hit Enter. This will flush the DNS resolver cache so your computer gets the updated Hosts file.
Step 7 - Restart the Browser
The last step is to restart your web browser so it loads the new Hosts file and blocks the websites.
Close all open browser windows and restart the browser. You should now see that the sites you blocked are inaccessible and don't load.
And that's it! In a few simple steps, you have blocked websites using Command Prompt in Windows. You can repeat this process at any time to block additional websites by adding them to the Hosts file.
Perform the above steps only when you know What you are doing. We don't encourage you to edit or make any changes with or within the Host file on the System.
Caution When Editing the Host File!
While blocking websites via the Hosts file may seem simple, there are some risks and precautions you should keep in mind:
- Make sure to create a backup of the original Hosts file before making changes.
- Editing the Hosts file requires administrator access, so be careful not to make changes unless you absolutely trust the source.
- Be cautious about blocking essential websites - you may inadvertently block access to important services like Windows Updates or antivirus software sites.
- Websites often use multiple domains and subdomains, so you may need to block more than just the main site URL.
- Some websites may not work properly when blocked via Hosts, so test access after making changes.
- Blocking sites using Hosts only affects that single computer's access. It does not block other networked devices or users.
- Improperly formatting the Host file entries may cause connectivity issues or a failed DNS resolution.
Overall, use good judgment when blocking sites using the Hosts file. Be specific in the sites you block and test that only intended content is restricted. And keep backups in case you need to revert changes or troubleshoot problems.