Kill those Ads!

Installing and using a hosts file..

When your computer's operating system connects to a domain (e.g. 'google.com'), it makes a "DNS call", to lookup the address of the requested domain (in Google's case, '64.233.187.99', among others). It will search in a few places, starting with your local DNS system, and, if nothing was found locally, finish up by asking a proper remote DNS server.

The order in which it performs these lookups, is called, unsurprisingly, the "Lookup Order", but all we really need to know, is that the first place it looks, is in your local hosts file, and if it gets an answer there, it looks no further. The local hosts file, as my Slackware system puts it, "describes a number of hostname-to-address mappings for the TCP/IP subsystem". Any number, in fact; you just add what you need.

Using this principle, we can easily spoof our local machine or network into believing that "smells-somewhat-like-pr0n.com" is actually at the address "192.168.1.100", "127.0.0.1"", or even "0.0.0.0". Same for "thoughtlessly-targeted-ads.com", and whoever else we'd rather not waste bandwidth on, or let the kiddies see; simply redirecting all bad host requests right back to our own machine, or wherever we like.

Clever folk will realize that a custom 404 page on a local server, maybe with a picture of your favourite seaside resort, or similar, would be a neat idea, and all bad requests will bring up a pleasing image, instead. Of course, you'll need a local web server running for that, which I guess is why we're in the /serv part of the site.

Anyways, this is a quick how-to page of instructions on how to add hosts. For Windows and Linux/UNIX, this is trivial. For a Mac, not so much, and this page is intended as quick Mac reference for people who either know what they are doing, have done it before, or don't care about the theory, potential issues, and troubleshooting methods behind this, and just want to ban hosts now.

Okay, let's do it!

Windows..

You have two hosts files already installed.
Windows XP/Vista..
C:\WINDOWS\system32\drivers\etc\hosts
C:\WINDOWS\system32\drivers\etc\lmhosts
Windows 2000..
C:\WINNT\system32\drivers\etc\hosts
C:\WINNT\system32\drivers\etc\lmhosts
Windows 98/ME..
C:\WINDOWS\hosts
C:\WINDOWS\lmhosts

While you are supposed to use the second one (local machine hosts) for your own hosts, it's usually better (more responsive) to use the first, or even both. The file itself contains information and examples, but very basically, you add your banned hosts like this..
0.0.0.0 badhost.com
Often folk will redirect bad hosts back to the local machine (aka "loopback address"), like this..
127.0.0.1 badhost.com

One final note for Windows users. It's not unusual for software/malicious web sites/etc to alter your hosts file for their own purposes; so when you're done, you might want to set the permissions on your host file to read-only.

Linux/UNIX..

Your hosts file is probably here..
/etc/hosts

If your Linux system is acting as a gateway machine, or local samba/WINS server, you can use the hosts file for all sorts of good stuff. Knowing your hosts file will enable you to run local mail servers, fake domains (for testing), and more. Check around this area of the site for lots more information about this.

Add hosts like this..
0.0.0.0 badhost.com

Mac..

On most operating systems, you simply add names to the local hosts file, and they will be used automatically; because the local hosts file is generally first in the "lookup order". Not so on a Mac. On a Mac we need to alter the lookup order, so that the local hosts file is consulted first. For a fuller description of this technique, go here , or more likely here

1. Create a new lookup order (aka "The Flat Files Method")

Open a Terminal session..

Get "root":
su  (enter your password)

Your prompt changes from "%" to "#". You are now superuser.
If that didn't work, click here

create the lookup folder:
cd /etc
mkdir lookupd
cd lookupd
ls

If any files exist, make backups of them, now create the lookup order itself..

this should all copy on one line:
echo LookupOrder CacheAgent FFAgent NIAgent DNSAgent YPAgent NILAgent > hosts

2. Fix NetInfo, specifying the search order.

Get a custom lookupd.txt file here. (thanks to Marcel Bresink)
Put it in your home folder.

Backup the locations directory (still su in the terminal)..

backing up the old lookup info:
nidump -r /locations/lookupd / > lookupd.original


backup the machines directory..

backing up the old lookup info:
nidump -r /locations/lookupd / > lookupd.original

backing up the old machines info:
nidump -r /machines / > machines.original


if you need to get them back use this..

to restore:
niload -r /machines / < machines.original


Install the new lookupd.txt

create new lookup order in netinfo:
niload -r /locations/lookupd / < lookupd.text

There should be no errors.

** reboot **

yes, you need to do this before step 3. sorry.
well, okay, you could restart the netinfo domain instead

3. Install the hosts file..

Download and unstuff this file into your home directory.

If you use an updated hosts file from some other source, open it in BBEdit and change it from a DOS filetype to a UNIX filetype, then save it again. (Netinfo won't import a DOS file). Alternatively, use dos2unix.

Finally, install the hosts file proper..

This next command is at the top of my own hosts file, commented out, for easy access. If you add hosts regularly, do this. In the future, all you need to do to add a host is; edit it into your hosts file, then do this command. In the terminal..

go go gadget hosts:
sudo niload -v hosts / < hosts

This takes ages. Go for a walk.

That's it, you're done. No more daft ads.


;o)

ps.. if you create websites, or even play with creating websites, you might want to check out the groovy trick on this page.


Welcome to the comments facility!


previous comments (two pages)   show all comments

Aaron - 22.11.04 1:00 pm

"This takes ages. Go for a walk."

There's an easy way to substantially reduce the load time. If you put an asterisk for a "wild card", you can consolidate many lines of your host file. For example, instead of

0.0.0.0 control.123banners.com
0.0.0.0 ftp.123banners.com
0.0.0.0 ftp.control.123banners.com
0.0.0.0 www.123banners.com

Just put in

0.0.0.0 *.123banners.com

If you do this for every domain, your load time will be a fraction of what it was.


Aaron - 22.11.04 1:00 pm

Actually, instead of doing the above by hand, just run the following command instead of
niload -v hosts / < hosts


sed -e 's/\(0.0.0.0 *\)[^\.]*\(\..[0-9a-zA-Z]*\.[a-z]*$\)/\1*\2/' hosts | sort | uniq | niload -v hosts /


This way you get the same speed optimization as above without having to edit that ungodly large file.


cor - 22.11.04 1:00 pm

I thought your first post quite absurd.

Of course, you don't need to go for a walk, you can just flip the terminal to the background and get on with your work while niload runs through all the host entries. The time taken to physically concanate all the duplicate domains would be something to the power of that it would take the mac to do it! so really, that was a big time-loser. smiley for :lol:

Not only that, I was thinking while I read it, "and some folks grab a fresh hosts list every week, imagine the work!"

But the more I think about it, the more I believe that concanating the hosts would be a very good idea, not only because of the time involved in niloading them into the netinfo database, but because of the time taken to process the entire hosts database on each and every network request. I'm assuming it does that, it seems likely that, at some level of the operating system, the list must be checked. And small lists check faster than big ones! Now we're starting to save some real time.

Then came your (extremely cute) sed, and now I'm thinking about how the whole process could be scripted; a wee curl, sprinkle on the magic sed, and Bob's yer uncle! sed could even handle the linebreak convertion, couldn't it? I'm a total sed n00b, though I've seen its incredible power demonstrated before. Point is, every single morning you could wake up to a freshly updated netinfo database. very useful.

And I didn't realise you could use wildcards, neat! Does that work for regular *nix host files, too, I wonder? Anyways, thanks very much for the enlightening input, Aaron, food for thoughts!

;o) Cor


redsky - 22.11.04 1:00 pm

My virtual host setting on apache

<VirtualHost 169.254.106.123:8080>
ServerAdmin kshetu@gmail.com
DocumentRoot "e:\shetu\www"
ServerName www.shetubd.info
ErrorLog logs/shetubd.info_error_log
CustomLog logs/shetubd.info_access_log common
</VirtualHost>


Now i want use rajib.info to point my virtual host.My os is windows 2000. Can you me easy to setup dns setting or others things.



cor - 22.11.04 1:00 pm

redsky, the spoof host settings point to the machine, the IP ot the machine, to be exact. After that, it's up to you to catch the requests with your apache virtual host conf (use a serveralias command, by the way). see here for more details of the DNS side of things.

;o) Cor


Gotta Question - 01.12.05 10:16 pm

At the very beginning of 2 you say:
Get a custom lookupd.txt file here. (thanks to Marcel Bresink)
Put it in your home folder.

Where is the home folder?

Thanks


cor - 02.12.05 2:58 am

Your home folder is where all your user data lives. Your desktop, documents, preferences, all that stuff is inside your home folder (it's a UNIX concept, but even Windows has it now, at last). If you click on your Mac desktop, and then press Command-UP, you will be inside your home folder.

This is also the location that the terminal will open at, by default. That's why why you put the text file in your home folder, so that when you open a terminal session, you can operate on the file without having to cd into another directory or enter long path strings into the command line.

The home folder is usually referred to (in a shell) using ~/
Which saves you typing the full path ito scripts and what-not. ~/Desktop, then, is a quick way to refer to your desktop, for instance.

On Mac OS X, the actual (long) location will be (now, let's see if I can remember) ..

/Volumes/HardDrive/Users/YourName

Replacing HardDrive with your hard drive's name, and YourName with yours, of course.

So now you know!

;o) Cor


restartNetInfo - 08.05.06 1:37 pm

thanks 1.000.000 for this tutorial!

you can restart netInfo with a shell script:

Get this file (perl script): http://darwinsource.opendarwin.org/10.1/configd_plugins-40/Kicker.bproj/restart-NetInfo

(there is an error in line 48 "fi", remove it or it will report an error in the next step)

Then go to the terminal, and type /bin/sh and drop the file you've downloaded.
press return.

that's it!


Simon - 19.02.10 11:15 pm

Hi guys!
I've got a problem on the 1st step((( I used a command "su" and it asked for a password, so i tried to type it and nothing happened - i mean non of symbols appeared... I tried to push on all the keyboard and only Enter works, so that command says "Sorry". I tried to change the languages and nothing helps((( Is there any way to fix it?
I'm using Leopard 10.6.2.
Thanks!!!


cor - 15.03.10 8:14 am

Simon, that's standard UNIX protocol for entering a password. The keyboard is working fine, it's just that you can't see the input (even an asterisk reveals the number of characters typed).

Simply type your password and press <Enter>, as normal.

;o) Cor


Reza Hashemi - 26.12.10 5:50 am

hey Thanks for your tips

but there is one website will get you to rock http://www.mvps.org/winhelp2002/hosts.htm it updates every so often smiley for :)


MAINTENANCE MODE!
Posting is currently disabled while I switch servers.

Welcome to corz.org!

If something isn't working, I'm probably improving it, try again in a minute. If it's still not working, please mail me!