HOWTO: Amend your hosts file to block ads (by site)
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
Re: HOWTO: Amend your hosts file to block ads (by site)
Here is a link to a site with a script to update your hosts file:
hostsfile.mine
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
- 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 :
Code:
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
Quote:
#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).
Re: HOWTO: Amend your hosts file to block ads (by site)
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 ******* 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
Re: HOWTO: Amend your hosts file to block ads (by site)
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:Add the line
Quote:
127.0.0.1 localhost.localdomain <hostname>
<hostname> = your host name
At the top fo the file.
Crtl-X to exit, type Y to save.
Reboot
Re: HOWTO: Amend your hosts file to block ads (by site)
Thank you very much.:confused:
Re: HOWTO: Amend your hosts file to block ads (by site)
Following this post, I read:
Unix/Linux bash Hosts updater script (txt) (m)
Here is the script if you would like to examine the code:
Hosts update script
* Download the script
* Make the script executable (chmod a+x updatehosts.sh)
* Install tofrodos (sudo apt-get install tofrodos)
* sudo ./updatehosts.sh
I downloaded the updatehosts.sh.txt
but, doing: chmod a+x updatehosts.sh -- did not remove the .txt. So there I quit and am asking for clarification:
Do I rename the file updatehosts.sh.txt manually before or after running chomd a+x?
Re: HOWTO: Amend your hosts file to block ads (by site)
Quote:
Originally Posted by
Mark_in_Hollywood
Do I rename the file updatehosts.sh.txt manually before or after running chomd a+x?
It does not matter if you rename the file before or after the chomd.
Re: HOWTO: Amend your hosts file to block ads (by site)
On Ubuntu Lucid Lynx 10.04 the script is currently broken because of the following changes to the tofrodos package.
Quote:
Originally Posted by /usr/share/doc/tofrodos/NEWS.Debian.gz
tofrodos (1.7.8.debian.1-2) unstable; urgency=low
With this release the symlinks "unix2dos" and "dos2unix" are dropped from the
package. This will allow the introduction of the original dos2unix package,
which also supports conversion to MacOS style files.
Should you have scripts depending on these symlinks, I recommend using
shell-aliases, like in the following example for bash:
alias unix2dos="/usr/bin/fromdos -u" (or todos)
alias dos2unix="/usr/bin/fromdos -d" (or just fromdos)
-- Alexander Reichle-Schmehl <tolimar@debian.org> Thu, 21 Jan 2010 20:59:11 +0100
I simply updated the script by replacing all occurrences of dos2unix with fromdos using gedit (CTRL+H). Works like a charm now.
Re: HOWTO: Amend your hosts file to block ads (by site)
Just want to put a reminder here for all who are looking into using /etc/hosts to block ads: this seem to break desktopcouch, and with that UbuntuOne and Gwibber and Evolution. Multiple beam.smp processes will be spawned, sucking up CPU cycles, and seem to take forever to parse lines in /etc/hosts
The bug is here: https://bugs.launchpad.net/ubuntu/lu...ch/+bug/530541
Chad on the page suggested dnsmasq or similar as an alternative to /etc/hosts for the time being...
Re: HOWTO: Amend your hosts file to block ads (by site)
Awesome! \\:D/ I love this script! Thanks!