Google Search

Custom Search

Wednesday, May 26, 2010

HOWTO: BLOCK WEBSITES USING THE HOSTS FILE

What is the HOSTS file?

The HOSTS file is a list of web addresses. It basically tells your computer where to look for a certain website. When you type an address in the address bar of your web browser, it checks for instructions on the HOSTS file; if no record about a website is found, it opens a website for you. Otherwise, it opens a website from an IP address defined on the HOSTS file. If the address leads to 127.0.0.1, you won’t be able to access the website, because 127.0.0.1 is an IP address of your computer. If 127.0.0.1 is set as an address of a website, you will only be able to see it if your computer is a server.

Blocking ad servers and malicious websites is a tough task these days. There are so many tools out there to keep ads blocked, remove spyware, and scan for viruses. Most of this unwanted content comes from known ad servers and known malicious websites. You can harness the power of the Windows’ HOSTS file to actually block some of this stuff.

In a nutshell

For your version of Windows, the HOSTS file is located in

Windows XP:
C:\WINDOWS\system32\drivers\etc\

Windows 2000:
C:\WINNT\system32\drivers\etc\

Windows 98/ME:
C:\WINDOWS\

It does not have an extension — it is just called hosts. The purpose of the HOSTS file is to allow you to manually enter IP addresses associated with websites, so the DNS server can quickly resolve an address. Say you know that google.com is the IP address 64.233.187.99 — you could enter that into the HOSTS file so when your machine requests google.com, it will immediatly know to go tohttp://64.233.187.99/. Instead, we can take known ad servers, and tell them their IP addresses are 127.0.0.1. For those of you that don’t know, 127.0.0.1 is your local machine, if you were running a server on it. So say, some ad server hosts a banner ad athttp://www.adsite.com/ads/23bh3.jpg. If you redirect this server to your lcoal machine in the HOSTS file, when a website calls that hosted image from the ad server, it will try to loadhttp://127.0.0.1/ads/23bh3.jpg. Since that folder and that image don’t exist on your computer, the image won’t show, and you’ve blocked the ad from showing on that website.

Putting it together

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, 127.0.0.1 is your local machine, like I explained above. You could also use 0.0.0.0 instead of 127.0.0.1 — either will work, but whatever you use for localhost, use below. After that, enter known ad servers and use your local IP (whichever you chose at the top). You can find maintained HOSTS files online or start to build your own. Here’s a little snippit of what my HOSTS file looks like:

127.0.0.1 localhost
127.0.0.1 92.132.206.rev.adknowledge.com
127.0.0.1 a-con1.adknowledge.com
127.0.0.1 a-lbs.adknowledge.com
127.0.0.1 a-pwr.adknowledge.com
127.0.0.1 a-sw1.adknowledge.com
127.0.0.1 aa1-1.adknowledge.com
127.0.0.1 aa1.adknowledge.com
127.0.0.1 aa2-1.adknowledge.com
127.0.0.1 aa2.adknowledge.com
127.0.0.1 aa3-1.adknowledge.com

Extra step: Windows XP/2000 only

In XP and 2000, the DNS Client (a system service), will make your machine come to a grinding hault if your HOSTS file is too large. This service is unnecessary and can be stopped and disabled. Go toStart > Run > enter “services.msc” then hit [Enter]

Navigate to DNS Client, right-click it, select Properties, under Start-up Type, select Manual. ClickApply. Then below that, click the Stop button. You should do this before you save a large list of servers to your HOSTS file.

Disabling/Enabling the HOSTS blocking

If you ever need to disable the blocking, just rename the hosts file to anything. I usually rename it tohosts.disable. After you do that, you need to restart your browser to see the changes. To enable it again, just rename it back to hosts.

You could also write a script to do this for you. An example called disable.bat:

cd C:\WINDOWS\system32\drivers\etc && ren hosts hosts.disable

And another example called enable.bat:

cd C:\WINDOWS\system32\drivers\etc && ren hosts.disable hosts