Blocking unwanted pop-up ads with the /etc/hosts file

Post your tutorials and howtos here.
User avatar
rolgiati
Posts: 209
Joined: 08 Jun 2013 16:10

Blocking unwanted pop-up ads with the /etc/hosts file

Postby rolgiati » 27 Jun 2013 23:34

Unless you have a Local Area Network that uses fixed IP addresses instead of DHCP, you probably dont know anything about your /etc/hosts file; in short, this is where your machine keeps a list of computer names and addresses, so that you can "$ ping tom" instead of "$ ping 192.168.1.3" when you want to check a connection is working on your LAN. When you send the command "$ ping tom" the computer first looks up for a "tom" entry in /etc/hosts and if found uses the corresponding IP address to send the ping (if tom is not found, it will query the DNS, but that is another story).

But /etc/hosts can be put to other uses, such as blocking access to URLs that do not interest you, like http://www.doubleclick.com and other purveyors of unwanted Internet rubbish !
(It can also prevent the little dears from hogging the bandwidth watching Youtube, but that too is another story).

The system is very simple: if you have a "127.0.0.1 www.unwantedsite.com" entry in /etc/hosts, any query by your browser, or any other program, to http://www.unwantedsite.com will be directed to 127.0.0.1, which is your own machine, and it will fail since the page it wants is not present on your box; so you will not have to wait for the ad to download, and you will end up with something like "web page not found" on the page instead of the unwanted add ;-3)

So on you go, and start collecting the URLs you want to avoid, and add them to /etc/hosts.

Or if you are lazy like me, you look up in Google (or other) for "block hosts file", download the lists, mix them together, do a "$ sort -u" to remove duplicates...
(please note that since Windows uses a similar hosts file, which has the same structure, we can use under Linux hosts files made for Windows too without any problem)

Or you can start with my own list which is available zipped on http://www.filesnack.com/files/ctnf2nnx for anyone who cares to use it.

Once you have rolled your own file of bad hosts, or downloaded one, you will append it (as root) to /etc/hosts with

Code: Select all

# cat badhosts >> /etc/hosts
If you roll your own, remember that you must have different entries for "doubleclick.com" and "www.doubleclick.com" if you want to block both.

It will certainly happen later that you come across a URL that you need after all; in that case a quick edit with mousepad or vi (launched as root from a console) will let you find and remove the offending lines, and access the site.

In the same way, when you want to add one single URL to the file, a quick

Code: Select all

# echo "127.0.0.1 www.otherbadsite.com" >> /etc/hosts
will complete your file.

theuncontactable
Posts: 6
Joined: 07 Jul 2013 08:45

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby theuncontactable » 07 Jul 2013 09:08

I would be inclined to use /etc/hosts.allow and /etc/hosts.deny for blocking ip addresses

User avatar
rolgiati
Posts: 209
Joined: 08 Jun 2013 16:10

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby rolgiati » 07 Jul 2013 11:58

theuncontactable wrote:I would be inclined to use /etc/hosts.allow and /etc/hosts.deny for blocking ip addresses
Then maybe you would care to explain here in detail how this can be done, for the edification of the assembled multitude ?

theuncontactable
Posts: 6
Joined: 07 Jul 2013 08:45

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby theuncontactable » 07 Jul 2013 14:26

No, I wouldn't as I'm not interested in security and therefore uses the browser's plugin to block out ads on websites.
/etc/hosts.allow and /etc/hosts.deny was the first thing I'd thought of when I read the tutorial. However I do have some concerns with using /etc/host as a firewall when Linux have other options that specialized in firewalling, like:

What happens if I ping 127.0.0.1?
Or somewhere down the track I've installed a database on the same machine and I want to connect to it where will it connect to?

User avatar
MAYBL8
Posts: 1487
Joined: 10 Mar 2013 18:41
Location: Maryland Heights, MO USA
Contact:

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby MAYBL8 » 07 Jul 2013 14:40

My 2 cents:
If You're using Firefox. Just install AdBlock.
There's even a Filter Updater extension for AdBlock, too, that'll auto-update when the list changes.


User avatar
wisemagic
Posts: 303
Joined: 13 May 2013 03:05
Location: New Iberia, Louisiana USA

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby wisemagic » 16 Aug 2013 07:11

This works quite well. Thanks
E5-2690 Xeon - 16GB PC12800 DDR3 1600 Quad Channel -
Chinese HUANAN MB - GeForce GTX 960 4GB PCI Express 16X HDMI
Dual 480.0 GB SSD Corsair - Corsair H60 Liquid CPU Cooler - Blu-ray / DVD LightScribe Recorder
Debian Sid - Kernel 4.17.2-zen

MT2sum
Posts: 25
Joined: 27 Jan 2014 21:19

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby MT2sum » 03 Feb 2014 22:15

MAYBL8 wrote:My 2 cents:
If You're using Firefox. Just install AdBlock.
There's even a Filter Updater extension for AdBlock, too, that'll auto-update when the list changes.
I installed Adblock from Firefox addons - no joy! I still get popups on both my M$ Win7 and SolydX. I'll go back to using /etc/hosts as I have in the past - it even gets rid of all the ads on many of the pages, just leaves bare spots instead of annoying ads.
Just my 2 centavos ......

User avatar
MAYBL8
Posts: 1487
Joined: 10 Mar 2013 18:41
Location: Maryland Heights, MO USA
Contact:

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby MAYBL8 » 03 Feb 2014 22:25

I just noticed there is a new AdBlock Plus with Pop ups that can be tried.
http://jessehakanen.net/adblockpluspopupaddon/


User avatar
Orbmiser
Posts: 829
Joined: 24 Aug 2013 22:58
Location: Portland,Oregon

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby Orbmiser » 04 Feb 2014 00:27

Strange as never had problems with popups with adblock extension on Windows7 or Linux.

But I heard and of course no personal experience in such browsing :o . But browsing porn popups are more about opening ad's in new windows.And can enable blocking by not allowing web sites to open links in new window in java settings.
.
Portfolio
http://500px.com/Orbmiser
Flickr
http://www.flickr.com/photos/orb9220/

SolydK - Kernel 3.16-2-amd64 - KDE 4.14.1 Update Pack: 2014.10.15

User avatar
fleabus
Posts: 1227
Joined: 16 Sep 2013 04:24
Location: Winchester, VA USA

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby fleabus » 04 Feb 2014 08:11

Adblock Edge, A fork of Adblock Plus without the "acceptable ads" feature.

And/or the mvps hosts file which I have always used.
Maybe redundant, but I usually do it anyway.
Depends on my mood and current size of tinfoil hat. :D

Using mvps hosts in linux:

https://bbs.archlinux.org/viewtopic.php?id=150944
http://www.putorius.net/2012/01/block-u ... ts-on.html

or more generic:

https://bbs.archlinux.org/viewtopic.php?id=139784

Adapted for my own use, something mo-simple, from the second link:

first,

Code: Select all

# cp /etc/hosts ~/.hosts_orig
From then on use a script to append mvps hosts.txt to ~/.hosts_orig and replace /etc/hosts.

Code: Select all

#!/bin/bash
cd /tmp
wget http://winhelp2002.mvps.org/hosts.txt
rm /etc/hosts
mv hosts.txt /etc/hosts
cat ~/.hosts_orig >> /etc/hosts
Add chron job to keep updated.
Add your entries from now on to ~/.hosts_orig

Shamelessly cribbed from various googling, works well for me.
May or may not work for you, or you may have to adapt it as I did,
thought I'd at least throw the example out here...

As said above, separate hosts.deny and hosts.allow is probably better.
I haven't got around to researching that yet.

All my pages load clean, spotless, ad-free.

MT2sum
Posts: 25
Joined: 27 Jan 2014 21:19

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby MT2sum » 04 Feb 2014 21:20

Well, I downloaded the badhosts file mentioned above - Almost 4100 pages (on Libre Office Writer) but I cut it down to 3700 pages by reducing the size of the font from 12 to 6. Now if I can just get the terminal to take the list I'll be golden ;)

About the "Allow/Deny" option .... aren't you just going to have to list them anyway? I haven't looked into this but is seems like redundancy to the /etc/hosts file I use. :?

User avatar
ScottQuier
Posts: 1781
Joined: 18 Jul 2013 15:55
Location: Newport News, VA

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby ScottQuier » 04 Feb 2014 23:11

MT2sum wrote:Well, I downloaded the badhosts file mentioned above - Almost 4100 pages (on Libre Office Writer) but I cut it down to 3700 pages by reducing the size of the font from 12 to 6. Now if I can just get the terminal to take the list I'll be golden ;)
Have you figured out how to use streams, pipes, and redirects? Let the OS do the grunt work for you. Check out this as a starting point --> http://www.ibm.com/developerworks/libra ... -v3-103-4/
Scott
Quoting zerozero, "The usage of PPA's in debian-based
systems is risky at best and entails serious compatibility
problems; usually it's the best way to destroy an install"

MT2sum
Posts: 25
Joined: 27 Jan 2014 21:19

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby MT2sum » 10 Feb 2014 20:24

Scott, I never did get into that stuff before (should have but didn't) - time to look closer :!: I took a look - a lot of geek-speak that I'll have to google to translate (I'm a computer illiterate), so it will take some time for me. I copied and pasted to Libre Office so I can study it in my spare time - Thanks for the link! Lots of other good instructions/tutorials in the Linux 101 series too! Hope this can help others that may need some help ......... like I do :lol:

User avatar
ScottQuier
Posts: 1781
Joined: 18 Jul 2013 15:55
Location: Newport News, VA

Re: Blocking unwanted pop-up ads with the /etc/hosts file

Postby ScottQuier » 10 Feb 2014 20:50

I'm pleased I was able to help a little.

If you have questions - ask. Someone will have an answer and be happy to share. :)
Scott
Quoting zerozero, "The usage of PPA's in debian-based
systems is risky at best and entails serious compatibility
problems; usually it's the best way to destroy an install"


Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 2 guests