Block Websites Using Hosts File in Windows 10

How to Block Websites Using the Hosts File in Windows 10

Every Windows version comes with a special Hosts file which helps resolving DNS records. In addition to your network configuration, the file can be used to define a domain=IP address pairing which will have priority above the value provided by the DNS server. Using this trick, you can block certain web sites from opening in the web browser on your computer.


This trick is also useful in many other situations. For example,  web devs can make their computer resolve a domain to a localhost address. If you have a home LAN, mapping a network device name to its IP address with the Hosts file will allow you to open the device by its name from File Explorer. This is useful when your network devices run a bareboned Linux distro which doesn't provide names Windows can recognize over the network.

The Hosts file is just a regular text file which can be modified using any text editor. The only catch is that the editor app should be started elevated (as Administrator). The hosts file is located in the system directory, so non-elevated apps will fail to save it.

The Hosts file consists of lines of text. Each line must include an IP address in the first text column followed by one or several host names. Text columns are separated from each other by white space.  For a historical reason, <Tab> is often preferred, but spaces will also do the trick. Lines started with a hash character (#) are comments. Windows ignores blank in the hosts file.

To Block Websites using Hosts File in Windows 10,

  1. Open the Start menu, and go to Windows Accessories.
  2. Right-click the Notepad app and select More - Run as Administrator.Run Notepad Elevated
  3. In Notepad, click the File menu - Open, or press the Ctrl + O keys.
  4. Navigate to the folder C:\Windows\System32\drivers\etc.
  5. Select "All Files" from the drop-down menu.
  6. Double-click the hosts file.Notepad Open Hosts
  7. On a new line in the hosts file opened in Notepad, type 127.0.0.1. This is your localhost address (the default local address of the PC).
  8. Press Tab or add spaces after you localhost address, and type the web site address (e.g. google.com or www.facebook.com) you want to block.
  9. Save the file(Ctrl + S).Windows 10 Edit Hosts File

You are done!

Note: Use one entry per line. Entries should look as follows:

127.0.0.1 google.com
127.0.0.1 www.facebook.com

How to test the changes

To test the changes you have made, open a command prompt and use the ping command to see the address in the output.

In my case, the remote address of the google.com domain will be resolved to my local computer.

Windows 10 Test Hosts File

 

Finally, you might want to unblock a blocked web site. Here's how it can be done.

To Unblock Websites Blocked in the Hosts File,

  1. Open the Start menu, and go to Windows Accessories.
  2. Right-click the Notepad app and select More - Run as Administrator.Run Notepad Elevated
  3. In Notepad, click the File menu - Open, or press the Ctrl + O keys.
  4. Navigate to the folder C:\Windows\System32\drivers\etc.
  5. Select "All Files" from the drop-down menu.
  6. Double-click the hosts file.Notepad Open Hosts
  7. Add the comment symbol #  at the beginning of the line that contains the blocked web site you want to unblock. Or, delete the whole line.Windows 10 Block Unblock WebSites Using Hosts
  8. Save the file(Ctrl + S).

Note: Adding the comment symbol is useful when you want to temporarily unblock an web address, or block/unblock it on demand.

That's it.

5 thoughts on “Block Websites Using Hosts File in Windows 10

  1. Dave

    I’ve been using a premade hosts file for years. Get it at “http://winhelp2002.mvps.org/hosts.htm”.

    I find it easiest to copy the file to the desktop then delete the old one. Do my editing and then move it back to it’s original location as many security options block modification of this file.

    Important Note: The HOSTS file now contains a change in the prefix in the HOSTS entries to “0.0.0.0” instead of the usual “127.0.0.1”.
    This was done to resolve a slowdown issue that occurs with the change Microsoft made in the “TCP loopback interface” in Win8.1.

    This change in the prefix should not affect everyday users. I’ve had some feedback that COMODO antivirus, and System Mechanic seems to have issues with the “0.0.0.0” prefix … to resolve this issue:

    You can use the “Replace” function in Notepad to convert the entries, or either of these freeware utilities (see below) has an option for converting the entries from “0.0.0.0” to “127.0.0.1.

    Reply
  2. WTF

    I used

    127.0.0.1 microsoft.com

    to not have Win8 phone home, but MS still resolves ! it is not working as advertised!

    Reply
    1. Sergey Tkachenko Post author

      Win8 doesn’t ‘phoning home’ directly to ‘microsoft.com’ it uses a number of other domains with different names, all owned by Microsoft.

      Reply
  3. Timur Shevchuk

    I did all the recommendations with updating the hosts file, and it’s still not working. Using windows 10

    Reply
    1. John Bartas

      @Timur – It would help if you list the sites blocked. Here’s the list I’m using.
      ########################
      # Block windows update
      ########################
      127.0.0.1 *.download.windowsupdate.com
      127.0.0.1 *.microsoft.com
      127.0.0.1 *.update.microsoft.com
      127.0.0.1 *.windowsupdate.com
      127.0.0.1 *.windowsupdate.microsoft.com
      127.0.0.1 download.microsoft.com
      127.0.0.1 download.windowsupdate.com
      127.0.0.1 ntservicepack.microsoft.com
      127.0.0.1 test.stats.update.microsoft.com
      127.0.0.1 windowsupdate.microsoft.com
      127.0.0.1 wustat.windows.com

      It seems complete, but I’ve also shut off every Windows 10 update switch I can find in the registry, so this is just my back-up plan. Good luck.

      If anyone can add to the list, that would be great.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *