Raj's adventures with Linux, MacOSX, the HD revolution, and any other toys he might pick up along the way.

Tuesday, October 2, 2007

Editing hosts file for convenience and ad blocking

DNS ServersWhen you type in www.google.com into your browser, we all know that there is a DNS server somewhere that gets queried to resolve that name into an IP address that can actually be used to start connections. But did you know that you can override these lookups locally? The TCP/IP stack looks in a file called "hosts" that resides somewhere on your system to FIRST resolve names to IP addresses before querying the DNS. This file is just a plain text document with a list of IP addresses and hostnames. On most *nix machines (MAYBE not on all BSDs though), the file lives in /etc. On Windows, it's usually in windows\system32\drivers\etc. Play around with it.

Make Digg Load FasterI've had a bug with the digg ad servers that causes each digg page to take upwards of a minute to load, with my status bar saying "Transferring data from y.digg.com", so I added the following line:

127.0.0.1 y.digg.com

This redirects all requests to y.digg.com to the IP address 127.0.0.1 which always corresponds to your own machine, the localhost. Since your machine isn't hosting any evil ad server, your browser can't establish a connection and gives up immediately trying to contact y.digg.com, making digg load faster. There are other fun things you can do, like add aliases to your hosts file for commonly accessed hostnames:

192.168.1.110 rajserv

This could let you do things like ssh rajserv on a local network instead of having to type in the whole IP address.

General AdblockingThe more general thing to do with this technique is to find hosts files on the net that have a list of all the bad adservers on the web. One such file can be found here, or you can just google for "hosts file". Some care must be taken editing these, just make sure to keep back up copies of the file when you are editing. On *nix, you will have to be root, so make sure to do the proper su or sudo before editing.Having one of these adblocking hosts files pretty much blocks every ad/banner/cookie you can imagine, making the web experience a lot faster for many sites. Enjoy!