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:


Windows 2000:


Windows 98/ME:


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., 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 to the domain name of known ad servers and malicious websites. 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: If you associate this domain name with the IP address of your local computer,, 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: localhost

That tells the rest of the file that is your local machine, as mentioned above. You could also use instead of — 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 You can find maintained HOSTS files online or start to build your own. See 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

Comments [9]

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

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

  3. 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. 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:
    # # source server
    # # x client host localhost

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

  6. 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. Open notepad as an administrator (right click, run as admin) choose open file, browse to hosts file, edit it and save

  8. You have all the URLs set to

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

  9. 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

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