How to block ads on any web browser on Linux

If you want to block ads while surfing the web on a web browser, there are browser-specific plugins to use (e.g., AdBlock or Adblock Plus on Chrome and Firefox). But if you are looking for a browser-agnostic or plugin-free way to remove ads from web pages (i.e., block ads on any web browser), this guide can help you.

A simple way to block ads on any web browser is to set up a local http proxy which then filters advertisements embedded in web pages, and renders ads-free web pages for you. One such proxy is Privoxy which is equipped with advanced filtering capabilities that can filter a wide range of well-known ads from web pages.

To install and start Privoxy on Ubuntu or Debian, run the following.

$ sudo apt-get install privoxy
$ sudo /etc/init.d/privoxy start

To install and start Privoxy on Fedora, use these commands.

$ sudo yum install privoxy
$ sudo service privoxy start

To install and start Privoxy on CentOS or RHEL, first set up EPEL repository on your system, and then run the following.

$ sudo yum install privoxy
$ sudo service privoxy start

After installation, you can check whether Privoxy is running successfully as follows.

$ sudo netstat -nap|grep privoxy
tcp        0      0 127.0.0.1:8118              0.0.0.0:*                   LISTEN      2093/privoxy

Now, edit proxy setting for your web browser as follows.

  • Proxy host: 127.0.0.1
  • Port: 8118

Once proxy setting has been edited, you can browse web sites as usual. You will find that websites are now free of ads. For example:

New York Times website before enabling Privoxy:

New York Times website after enabling Privoxy:

You can customize Privoxy's default filtering rules by editing /etc/privoxy/default.action.

$ sudo vi /etc/privoxy/default.action
. . . .
#----------------------------------------------------------------------------
# JavaScripts and Texts for ad and popup generation
#----------------------------------------------------------------------------
# Blocked URL = http://pagead.googlesyndication.example.com/foo/bar/baz.js
pagead*.googlesyndication./.*\.js
# Blocked URL = http://a.tfag.de/js.ng/
/js\.ng/
/popunder
/js/slider\.js
/t\.php\?cat=.*&kw=.*&sc=
scripts.chitika.net/.*\.js
jlinks.industrybrains.com/
.adinterax.com/.*\.js
.googleadservices.com/gampad/.*\.js
js.adsonar.
. . . .

After filtering rules are updated, make sure to restart Privoxy.

On Ubuntu or Debian:

$ sudo /etc/init.d/privoxy restart

On CentOS, RHEL or Fedora:

$ sudo service privoxy restart

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

11 thoughts on “How to block ads on any web browser on Linux

  1. I run a Linux based router called smoothwall. I have a plug in for smallwall called AdZap that removes ads for anyone on my network. Nothing is needed on the client side, since the router is stripping the ads.

    • That’s so much more difficult for the average user than simply installing a browser plugin. Plus, the filters for those plugins are automatically updated. I’ve used privoxy before, and this sort of application of it might be useful on a server or something, but… no.

      • quite possibly. I'm not touting that smoothwall is the way to go (or eve the best option). The concept/architecture is the important bit.

  2. I like Adblock Plus, it works for me and I trust it (for the moment). But this alternative looks good.

  3. One of the big advantages of something like adblock is I can disable it easily for specific items or pages.

  4. Well this is useful in my small company where I can set this up on my Linux firewall/gateway and filter ads for 10-12 people.

  5. I use a combination of router-based ad blocking (fabulous for those painful mobile apps with ads) and AdBlock for the sneakier JavaScript ads.

  6. There's a far simpler way to block ads than to go through installing new software and configuring the browser to use a proxy.

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

    is a list of known ad services, including popup windows and the like and it aliases the names to 127.0.0.1, which isn't running a web server, so you don't even waste time connecting to those sites. Some later versions of Linux (Ubuntu) get upset with using 127.0.0.1, so you can change it to 127.1.1.1. So download it using

    wget http://winhelp2002.mvps.org/hosts.txt -O - |sed 's/127.0.0.1/127.1.1.1/' > hosts

    Then "install" using

    sudo bash -c 'cat hosts >> /etc/hosts'

  7. I don't use plugins or router blocking. I just tell my nameserver that it is authorative for doubleclick.net and a few other domains and give apache a few rules for them. As a result no machine that gets a dhcp address from me can see any ads. Its a nice setup.

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *