PDA

View Full Version : HOWTO: Amend your hosts file to block ads (by site)


goldaryn
August 22nd, 2006, 11:17 AM
Note: this is very much a beginner's guide.


Q: What is a HOSTS file / Why might I want to do this?
A: It's explained nicely at http://www.mvps.org/winhelp2002/hosts.htm


Q: But why do this in Ubuntu? My Ubuntu is like a shield of steel!
A: Many reasons. But mainly to save bandwidth by not loading lots of rubbish. For each site listed as 127.0.0.1, content (usually ad banners etc) from that site isn't loaded. In a web browser that picture/frame will 404.

So, how to do it:
The host file in Linux lives at /etc/hosts. We are going to add some lines to it.

Disclaimer:

***** NOW HEAR THIS *****

A) ***BACK UP*** your /etc/hosts BEFORE YOU START

B) You must APPEND to (not overwrite) the file. You need the stuff that's already in there! That means: paste in the additional lines AFTER what's already in there!

C) I do not recommend doing this if you HOST WEB PAGES from your PC.


End of disclaimer.



1) >>>>>>> BACKUP YOUR HOSTS FILE <<<<<<<<<

$ sudo cp /etc/hosts /etc/hosts.old

2) Download a good hosts file. I recommend the MVPs one:

http://www.mvps.org/winhelp2002/hosts.txt

3) Open your hosts file.

$ sudo gedit /etc/hosts

4) Paste in the contents of hosts.txt into the bottom of /etc/hosts.

5) It should take effect immediately. Close and reload your web browser then try to go to one of the blocked sites. It should 404.

Luc

bodhi.zazen
September 16th, 2006, 04:04 AM
Here is a link to a site with a script to update your hosts file:

hostsfile.mine (http://hostsfile.mine.nu/downloads/)

Look on the very bottom of the page for this:

Unix/Linux bash Hosts updater script (txt) (m)

Here is the script if you would like to examine the code:

Hosts update script (http://hostsfile.mine.nu/downloads/updatehosts.sh.txt)

Download the script
Make the script executable (chmod a+x updatehosts.sh)
Install tofrodos (sudo apt-get install tofrodos)
sudo ./updatehosts.sh

Optional
127.0.0.1 = your computer
0.0.0.0 = nowhere

"0.0.0.0" is faster as "127.0.0.1" will wait for a timeout :twisted:


This command will change 127.0.0.1 to 0.0.0.0 :
sudo sed -i -e 's_127.0.0.1_0.0.0.0_g' /etc/hosts

WARNING: We now need to manually edit /etc/hosts and change the line(s) at the top with localhost and your hostname back to 127.0.0.1 localhost

#These may be on one line or multiple lines
127.0.0.1 localhost
127.0.0.1 Ubuntu # Change Ubuntu to your hostname

It is best to do this from the terminal with vim or nano as graphical editors will take forever to load (the hosts file is large).

angler
October 16th, 2006, 09:02 PM
After running the script, which ran seemlessly, I don't see any changes to the HOSTS file... which one should it change? I was hoping to do like I did in windoze and cut/paste the loopbacks into the file, but I lost admin rights when I did it. Maybe I made a n00b mistake, I'll try it again. thanks

bodhi.zazen
October 16th, 2006, 11:34 PM
it modifies /etc/hosts

If you lost admin rights, boot to failsafe.

If you do not know, to get your host name, at the CLI type:hostname

nano /etc/hostsAdd the line127.0.0.1 localhost.localdomain <hostname>
<hostname> = your host nameAt the top fo the file.

Crtl-X to exit, type Y to save.

Rebootreboot

666porcondissaum
November 17th, 2008, 10:10 PM
Thank you very much.:confused: