Home » How-To » How to Block Websites on Windows Using Hosts File

How to Block Websites on Windows Using Hosts File

Want a free and effective way to block distracting websites on your Windows computer? Whether you’re aiming for better productivity or trying to keep your family safe online, editing the Windows hosts file is a powerful tool built right into your operating system. This method works universally across all web browsers on your PC without needing any extra software. This guide will walk you through every step of the process, from finding the hosts file to making sure your changes take effect immediately.

What is the Hosts File and How Does It Block Websites?

Before a web browser connects to a website, Windows first checks the hosts file. The hosts file is a simple text document that can act as a local address book for website domains. You can manually create entries in this file to tell your computer where to go—or where not to go—when it tries to access a certain domain.

To block a website, we simply tell the hosts file to redirect that website’s address (like www.facebook.com) to a non-existent or internal IP address on your own computer. When your browser tries to connect, it gets sent to this dead end, and the website fails to load. It’s a surprisingly simple and effective trick.

Step-by-Step Guide to Blocking a Website

The process involves running Notepad with administrative rights, opening the hosts file, adding the websites you want to block, and then saving the file. Let’s break it down.

Step 1: Open Notepad as an Administrator

This is the most important step. The hosts file is a protected system file, so you need administrative privileges to edit it. If you try to open and save it normally, you will get a “permission denied” error.

  1. Click the Start button or press the Windows key.
  2. Type “Notepad” into the search bar.
  3. In the search results, right-click on Notepad and select Run as administrator.
  4. A User Account Control (UAC) prompt will appear. Click Yes to grant administrative permissions.

You will now have a blank Notepad window open, but this instance of Notepad has the power to edit system files.

(Note: An illustrative image would be placed here in a live article)

Step 2: Open the Hosts File

Now that Notepad is running with the correct permissions, you need to navigate to the hosts file’s location.

  1. In Notepad, click File > Open.
  2. In the “File name” box, paste the following path and press Enter:
  3. C:\Windows\System32\drivers\etc
  4. By default, you won’t see any files because Notepad is looking for .txt files. Click the dropdown menu in the bottom-right corner that says “Text Documents (*.txt)” and change it to “All Files (.)”.
  5. You will now see several files. Select the file named “hosts” and click Open.

(Note: An illustrative image would be placed here in a live article)

Step 3: Add Websites to the Block List

The hosts file will open, showing some text that explains its purpose. Any line that starts with a # symbol is a comment and is ignored by Windows.

To block a website, you will add a new line at the very end of the file.

  1. Go to the bottom of the file and start a new line.
  2. Type 127.0.0.1, press the Tab key or a few spaces, and then type the website address you want to block.
  3. For best results, you should block both the www and non-www versions of the site. Create a separate entry for each.

The IP address 127.0.0.1 is the “localhost” or “loopback” address. It’s your own computer’s internal IP address. By pointing a website’s domain to this address, you prevent your computer from reaching the actual site on the internet.

Example Block List:

To block Facebook and Twitter, you would add the following lines to the end of your hosts file:

# Block distracting social media sites

127.0.0.1       www.facebook.com

127.0.0.1       facebook.com

127.0.0.1       www.twitter.com

127.0.0.1       twitter.com

127.0.0.1       www.instagram.com

127.0.0.1       instagram.com

Step 4: Save Your Changes

Once you’ve added the websites you want to block, you need to save the file.

  1. Click File > Save.
  2. Close Notepad.

Since you opened Notepad as an administrator, it will save without any permission errors.

Step 5: Flush the DNS Cache (Important)

Windows keeps a cache of recently visited websites to speed up Browse. To make sure your changes to the hosts file take effect immediately, you need to clear this cache.

  1. Open the Command Prompt as an administrator (just like you did for Notepad in Step 1).
  2. Type the following command and press Enter:

DOS

ipconfig /flushdns

  1. You will see a confirmation message: Successfully flushed the DNS Resolver Cache.

This command forces Windows to reread the hosts file for any future website requests.

How to Test and Unblock Websites

Testing: To test if the block is working, simply open any web browser and try to navigate to one of the websites you added to the hosts file. You should see an error page saying the “site can’t be reached” or something similar.

Unblocking: To unblock a website, simply follow the same steps to open the hosts file as an administrator and either:

  • Delete the lines you added.
  • Or, place a # symbol at the beginning of each line to turn it into a comment, which makes Windows ignore it. This is a good way to temporarily unblock a site without having to re-type the line later.

About the author

Ankit Gupta

Ankit Gupta is an Engineering graduate and an MBA post graduate. He brings with himself 5 years plus global writing experience on technology, travel, finance and medical. He follows technological developments especially Microsoft. Also, he has a deep liking for wild life and travels to various wildlife conservatories to be with the nature.