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

NOTE: THESE INSTRUCTIONS ARE FOR EARLY OS X.
FOR modern macOS, simply add hosts to the hosts file, like Linux/UNIX (above).

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) Cor

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!


anonymous - 22.11.04 1:00 pm

I get stuck at the end of 2. The bit where you write "There should be no errors." I get:

su: lookupd.text: No such file or directory

I have the file in my home directory - what else could be going wrong?

Should I "undo" the previous step to ensure my machine functions normally?

Thankyou - I feel like I am closer to the objective!


anonymous - 22.11.04 1:00 pm

I located the file by putting it into Library and instead of

niload -r /locations/lookupd / < lookupd.text

I entered

niload -r /locations/lookupd / < /Library/lookupd.text

Seemed to do the trick


corz - 22.11.04 1:00 pm

it probably should say before that line..

ensure you are *in* your home directory!

cd does that

glad it all worked out!

;o)


anonymous - 22.11.04 1:00 pm

Yeah I'm a bit rusty on the command line gear.

I finally got it (redefining the IP for a specific domain) to work with a combination of this article and this one:
http://www.macwrite.com/criticalmass/ mac-os-x-hosts-panther.php

Wishing now that I took more careful notes of exactly what I did - for the inevitable next time.

Thanks a million for publishing this stuff.


corz - 22.11.04 1:00 pm

ahh! there it goes!

That's the link I dropped at the top of this very page, except the old location (they keep moving that damn page!), thanks for rediscovering that, I made it clickable. I'll leave both in, just in case.

as for taking notes, that's pretty much what this page is, because there always a "next time", and it was handy having it online, I could reach it from other macs. then google found it. and then you. cool.

it's good that it's useful, I might even spruce it up a bit!

;o)

ps.. get in your terminal! smiley for :D



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.


next comments (1 page)

First, confirm that you are human by entering the code you see..

(if you find the code difficult to decipher, click it for a new one!)


Enter the 5-digit code this text sounds like :

lower-case tee, Upper-Case Elle, thuree, lower-case en, Upper-Case Bee


 

Welcome to corz.org!

I'm always messing around with the back-end.. See a bug? Wait a minute and try again. Still see a bug? Mail Me!