Blocking Websites Using the Windows Hosts File

There are a lot of tools available for blocking ads, removing spyware, scanning for viruses. Most of this unwanted content comes from known ad servers and known malicious websites.

The Windows HOSTS file allows you to block some of this content very easily and for free. However, this is not a complete internet security solution. It is still recommended that you run anti-virus software on your computer and be careful of the websites you visit.

Defining the HOSTS file

The HOSTS file is located in the following locations for the respective versions of Windows:

Windows 7, Windows Vista, Windows XP, Windows Server 2003:

C:\WINDOWS\system32\drivers\etc

Windows 2000:

C:\WINNT\system32\drivers\etc\

Windows 98/ME:

C:\WINDOWS

The HOSTS file does not have an extension, but it is a plain text file. This file allows you to manually associate IP addresses with domain names (i.e., google.com) so the Domain Name Server (DNS) can quickly resolve an address.

To use the HOSTS file to block a website, assign the IP address of 127.0.0.1 to the domain name of known ad servers and malicious websites. 127.0.0.1 is the IP address of your local computer, if you chose to run a server on it.

As an example, let’s say some ad server hosts a banner ad at the URL: http://www.adsite.com/ads/12345.jpg. If you associate this domain name with the IP address of your local computer, 127.0.0.1, when that website tries to load that image, it will not be found because the image file does not exist on your computer. Therefore, the ad will not display on that webpage.

Editing the HOSTS file

Since the HOSTS file is a text file, open the file in Notepad, or other text editor, to edit it. This first line of the HOSTS file should list the local IP address:

127.0.0.1 localhost

That tells the rest of the file that 127.0.0.1 is your local machine, as mentioned above. You could also use 0.0.0.0 instead of 127.0.0.1 — either will work. However, whichever IP address you use for localhost, also use for the sites you want to block.

After the localhost entry, enter known ad servers and malicious websites and assign your local IP address to each domain name. Put the IP address at the beginning of the line, followed by a tab, followed by the domain name for the ad server or malicious website. Here’s an example of the HOSTS file:

Sample HOSTS file

NOTE: Some spyware programs automatically add ad servers and malicious websites to your HOSTS file for you.

For Windows XP/2000 users only

In Windows XP and Windows 2000, if your HOSTS file gets to large (over 135 kb), the DNS Client (a system service) will cause problems on your machine possibly causing it to lock up. This service is unnecessary and can be stopped and disabled.

Therefore, before you save a big list of ad servers and malicious websites in the HOSTS file, select Control Panel | Administrative Tools | Services from the Start menu.

Selecting Services

Scroll down to the DNS Client service, right-click on it, and select Properties from the pop-up menu.

Selecting Properties for DNS Client service

The DNS Client Properties dialog box displays. Select Manual from the Startup type drop-down list. Click Stop below Service status to stop the service. Click Apply and then click OK.

DNS Client Properties dialog box

For more information about the HOSTS file, see http://accs-net.com/hosts/what_is_hosts.html. You can find maintained HOSTS files online or start to build your own. See http://www.mvps.org/winhelp2002/hosts.htm to download a good, free, pre-populated HOSTS file.

There is a .zip file for download containing the HOSTS file, a batch file for easy installation, and a readme file with instructions for installation in the different versions of Windows. Beware that there are special instructions for Windows Vista and Windows 7.

by Lori Kaufman

More posts from the How-To Category

Running Folder2ISO

Create an ISO File from a Folder in Windows

User Account Control dialog box

Turn Off User Account Control (UAC) for a Specific Application

Clicking Add Folder

Automatically Delete Files Using Autodelete – Part II

01Google_Chrome_Displaying_PDF.jpg

Use Google Chrome As The Default PDF Viewer In Ubuntu

Popular Posts This Month

event id

Fix Windows 8 App Opens Then Closes

runas command

5 Windows Alternatives to the Linux sudo Command

5 Utilities for Changing DNS Servers in Windows Reviewed

windows defender

Windows Defender vs. Security Essentials vs Safety Scanner

Comments [9]

  1. byflipper says:

    Great article, thanks Lori! That 'manualization' part will be especially useful. Thank you.

  2. Joel says:

    thanks, thats useful for blocking certain stupid and addicting websites :)

  3. Rustam says:

    I edit the text file "HOST," but unfortunately I am not able to save this file. It prevents me, displaying a pop-up like "you have to get permission from your administrator to save files here." Actually, I use the administrator profile.

  4. 365 says:

    I'm still having problems doing this. I followed everything, I'm pretty sure. This is my hosts file [site URLs changed]:
    # Copyright (c) 1993-2006 Microsoft Corp.
    #
    # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
    #
    # This file contains the mappings of IP addresses to host names. Each
    # entry should be kept on an individual line. The IP address should
    # be placed in the first column followed by the corresponding host name.
    # The IP address and the host name should be separated by at least one
    # space.
    #
    # Additionally, comments (such as these) may be inserted on individual
    # lines or following the machine name denoted by a '#' symbol.
    #
    # For example:
    #
    # 102.54.94.97 rhino.acme.com # source server
    # 38.25.63.10 x.acme.com # x client host

    127.0.0.1 localhost
    127.0.0.1 badsite.com
    127.0.0.1 anotherbadsite.com
    127.0.0.1 yetanotherbadsite.com
    127.0.0.1 worsesite.com
    127.0.0.1 worstsite.com

  5. cmun says:

    i did what the article said to do but it won't let me save the HOSTS file. what do i do?

  6. kris says:

    yeah, wont let me save either asks if i want to save in my documents as just a text file instead…what to do. Im in administrator account…what permission?????????

  7. Ofurpesi says:

    Open notepad as an administrator (right click, run as admin) choose open file, browse to hosts file, edit it and save

  8. Chris says:

    You have all the URLs set to 127.0.0.1

    You can only have one site per I.P. Address – In order for it to work, it should read like this.

    127.0.0.1 localhost

    127.0.0.2 badsite.com

    127.0.0.3 anotherbadsite.com

    127.0.0.4 yetanotherbadsite.com

    127.0.0.5 worsesite.com

    127.0.0.6 worstsite.com

  9. cj says:

    The host file works great for blocking certain sites that I wish for my kids not to view, but one of them discovered my method. Is there any way that I can hide the host file and create a dummy host file? Or perhaps make another file to put somewhere else to block the sites?

Leave a Reply