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.

6 Responses to “How to: Block ads with a router”

  1. Jacen says:

    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. Paul Tow says:

    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. Jacen says:

    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. Scott says:

    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. dan says:

    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. Paul Tow says:

    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.

Leave a Reply