How to: Block ads with a router

Does the idea of blocking online advertisements appeal to you? Many solutions exist, such as the AdBlock Plus extension for Firefox, CSS-based ad-blocking, and Privoxy. What you may not be familiar with, however, is using a router to block ads. Imagine: ad-blocking for an entire network, for all browsers and all operating systems, without any additional configuration on the clients. This includes devices which might not otherwise have a way to block ads, and it can be made even nicer by having the router automatically update its ad-blocking rules. All you need is the Tomato firmware on a compatible router and this guide.

  1. Check if you have a compatible router. If you do, follow the installation instructions carefully, realizing that you are modifying firmware at your own risk. Set your router as you like and then continue to step 2.
  2. Administration -> Scripts -> Init

    Administration -> Scripts -> Init


    In your router’s config panel, go to Administration -> Scripts. Paste the following in the “Init” script, which will run when the router is booted. This will create an empty text file called adblock.conf

    sleep 3
    touch /etc/adblock.conf
    sleep 2
    logger adblock.conf created

  3. Administration -> Scripts -> WAN Up

    Administration -> Scripts -> WAN Up


    Once the router obtains an Internet/WAN connection, we want it to download the latest ad server list. Still under Administration -> Scripts, choose WAN Up and paste the following:

    logger WAN UP Script will execute after 30sec please wait.....
    sleep 30
    wget -O /etc/adblock.conf "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext"
    service dnsmasq restart

  4. Advanced -> Scheduler

    Advanced -> Scheduler


    Now we want to set the router to update its ad blocking rules on a regular basis. Go to Administration -> Scheduler.





  5. Check the box labeled “Enabled” under Custom 1, 2, or 3.

  6. For the time, choose a reasonable update interval that isn’t overly aggressive. I chose to update once a week (every 10080 minutes, Everyday).
  7. Paste the following into the box labeled “Command.”
    wget -O /etc/adblock.conf "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext" && service dnsmasq restart;
  8. Advanced -> DHCP / DNS

    Advanced -> DHCP / DNS


    Almost done! Go to Advanced -> DHCP / DNS, and make sure that “Use Internal Caching DNS Forwarder” is checked.





  9. Paste the following into the box labeled “Dnsmasq custom configuration.”

    conf-file=/etc/adblock.conf
  10. If you ever suspect that the adblocking is breaking a particular website (such as streaming video), you can temporarily disable it just by removing the text in step 9.

22 comments.

  1. Amazing! Works like a charm, and really easy to modify if needed. I was wondering though, how does this compare to the following AdBlocking script: http://www.linksysinfo.org/forums/showthread.php?t=57556

    Also, what might you need to do if you want to remove the ad-blocking all together and clean up the adblock.conf file?

  2. A major difference is that rules in a hosts file (the format used by that script) do not apply to other subdomains. They only apply to the exact domain given, which results in a significant amount of duplicate rules. For example, a hosts file would need rules for ad1.adserver.com, ad2.adserver.com. ad3.adserver.com, and so on. My script retrieves the ad server list in a DNS server configuration format, and thus avoids that problem. However, it’s worth noting that the MVPS hosts file is very comprehensive, and may be worth a try.

    When I wish to temporarily disable adblocking, I simply remove “conf-file=/etc/adblock.conf” from Advanced -> DHCP/DNS and then paste it back in when I want adblocking again.

    By cleaning up the adblock.conf file, do you mean editing it?

  3. Actually I meant how to remove the adblock.conf file completely, but nevermind. I wasn’t thinking. >_>

    I really don’t like the Script from the link I gave you too much, it’s really big and I honestly can’t get it to work for some reason. That and it hasn’t been updated in forever.

    I do have another question for you though, if I wanted to add(Not replace) other Ad Server Lists like Yoyo’s. (Such as for instance, the other 3 listed in the link I previously gave.) how could I implement and automate that?

  4. Great article. Like others have said though I was unable to get it working with my old router (WRT64G version 1) running the latest version of Tomato.

    Tomato works great, but the script in the article seems to cause the boot time of the router to go way way up if the power goes out, or if I have to reboot the router for some other reason. In order to fix this, I had to completely remove the script. Couldn’t even get the router to boot in a reasonable amount of time without unplugging the wan interface. (maybe it’s my old slow hardware, and it parses that .conf.)

  5. This is pretty nice – I noticed that in Google Chrome it will replace ads that are built within frames with a 404 message. This is true on Youtube. Is there any way to modify that?

  6. If Chrome supports user stylesheets (CSS)- and many browsers do- then you can use a stylesheet to collapse the ad to keep 404 messages from showing. I’ve not yet used Chrome and wasn’t able to find any info about how to use such stylesheets with it. If you can find out how to do that, use FloppyMoose’s premade adblocking stylesheet.

    CSS and my Tomato adblocking script complement each other well. My script prevents ads from even downloading on any device on the network, and the CSS gets rid of the 404 messages.

  7. How do I manually add whitelist ?

  8. Chrome has extensions to hide ads.

    Of course, the problem with it is that it doesn’t block them. it loads them but doesn’t display them.

    If you want to hide the 404 messages, you could use one of the ad-hiding extensions, such as “ad-thwart” or “ad sweep”

    So you have your router blocking them from loading, and an extension to remove the (now broken) frames.

  9. Just wanted to say your instructions worked amazingly well. I’m a Safari users on OS X and am noticing much faster speeds by uninstalling the adblocking plugin I was using and working with the router setup instead. I have a CSS ad blocking file that I can enable if I take my machine to other networks. I love the fact that this setup blocks ads on my iPod Touch. Simply brilliant. I also use gasmask to block various nasty sites by adding them to my hosts file.

  10. Worked like a charm on the latest Tomato build and is a much lighter-weight script than anything else I’ve seen. Thanks!

  11. Thank You for these instructions. I did change a few things to use MVPS hosts file.

    sleep 5
    touch /etc/hosts.txt
    sleep 2
    logger hosts.txt created

    logger WAN UP Script will execute after 30sec please wait…..

    sleep 30

    wget -O /etc/hosts.txt “http://www.mvps.org/winhelp2002/hosts.txt”

    sed -i ‘s/127.0.0.1 localhost// ; s/127.0.0.1 /address=\// ; /^\#/d ; s/#.*// ; s/.$/\/0.0.0.0/’ /etc/hosts.txt

    sed -i ’1,3d’ /etc/hosts.txt

    service dnsmasq restart

    wget -O /etc/hosts.txt “http://www.mvps.org/winhelp2002/hosts.txt” &&

    sed -i ‘s/127.0.0.1 localhost// ; s/127.0.0.1 /address=\// ; /^\#/d ; s/#.*// ; s/.$/\/0.0.0.0/’ /etc/hosts.txt &&

    sed -i ’1,3d’ /etc/hosts.txt &&

    service dnsmasq restart

    conf-file=/etc/hosts.txt

    I placed this here in case others might want to use the MVPS
    host file. Thanks again :)

  12. I get a lot of “This webpage is not available” errors in Chrome. Ideas?

  13. I suggest installing the AdBlock extension from Chrome’s extension gallery alongside this.

    Another option is to create a CSS (cascading stylesheet) and set that as a user stylesheet in Chrome. I don’t see a way to set a user stylesheet in Chrome’s preferences, but there’s probably a way to do it through an extension.

    Personally, I’d go the AdBlock. I use a Safari port of the Chrome extension and it works very well.

  14. Thanks, works great! Is there a way to whitelist specific sites that were working before applying this script?

  15. Not that I know of, unfortunately.

  16. I had to modify the sed ml provided, did not seem to format the list correctly and dnsmasq failed to start up..

    sed -i ‘s/127.0.0.1[ \t]*localhost// ; s/127.0.0.1[ \t]*/address=\// ; /^\#/d ; s/#.*// ; s/.$/\/0.0.0.0/ ; /^\/0.0.0.0/d’ /etc/hosts.txt

  17. Can anyone recommend a site that I could visit to check the effectiveness of the script. I can see some ads have gone while others remain. Not sure if its me or something else!!!

    Nodds

  18. I like ml’s suggestion for using the mvp hosts file, but there is a much easier way to implement it.

    Follow the steps in this article and use this command in place of the wget commands:
    wget -O /etc/hosts.txt “http://www.mvps.org/winhelp2002/hosts.txt”

    On the last step, replace
    “conf-file=/etc/adblock.conf”
    with
    “addn-hosts=/etc/hosts.txt”

    Enjoy!

  19. Excellent article. One change that I did was not have the ‘Init’ script or the ‘WAN_UP’ script. Instead I am saving the file to permanent store /jffs/adblock.conf.

    Of course I had to download the adblock.conf once using the telnet session, the cron job (scheduler) downloads the file once a week and following line in dnsmasq.conf loads the adblock.conf

    conf-file=/jffs/adblock.conf

    Thanks for sharing.

  20. Great stuff, how do I know it is working? What’s a good URL to test?

  21. Unable to get it working thus far. I’m testing using the information found here “http://winhelp2002.mvps.org/hostsfaq.htm#Frequently”

    marcin$ ping coolwebsearch.com
    PING coolwebsearch.com (64.15.72.102): 56 data bytes
    64 bytes from 64.15.72.102: icmp_seq=0 ttl=52 time=43.241 ms
    64 bytes from 64.15.72.102: icmp_seq=1 ttl=52 time=42.110 ms

    It should resolve to 127.0.0.1, but it does not.

  22. GREAT WORK!! I was a little hesitant to try this, as it seems just a little dated now. But, I read through it, thought about everything, tested the update site, scanned through the list of blocked sites, then tried it.

    After almost 2 1/2 years, this little how-to works perfectly. I’m posting a link to this page on G+ and Facebook. Hope you get lots of hits!

Post a comment.