Posted: . At: 11:32 AM. This was 7 years ago. Post ID: 10359
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Use the iptables firewall to block ads on your Linux machine.


I have found a nice script on this website, this contains a very long list of iptables commands that allow ad blocking using iptables on Linux.

See it here: https://pgl.yoyo.org/as/iplist.php?ipformat=iptables&showintro=1&mimetype=plaintext.

I have found this does not work in iptables when fed in as a list of rules. The way to use this is to edit it and add this at the very top.

Bash
#!/bin/sh

So that it looks like this.

Bash
#!/bin/sh

/sbin/iptables -A OUTPUT -o eth0 -d 103.245.223.131 -j REJECT # athena-ads.wikia.com
/sbin/iptables -A OUTPUT -o eth0 -d 103.245.223.194 -j REJECT # athena-ads.wikia.com
/sbin/iptables -A OUTPUT -o eth0 -d 103.31.6.184 -j REJECT # ads.imgur.com
/sbin/iptables -A OUTPUT -o eth0 -d 103.31.6.32 -j REJECT # ads.imgur.com
/sbin/iptables -A OUTPUT -o eth0 -d 103.31.6.33 -j REJECT # ads.imgur.com
/sbin/iptables -A OUTPUT -o eth0 -d 103.31.6.34 -j REJECT # ads.imgur.com
/sbin/iptables -A OUTPUT -o eth0 -d 103.31.6.35 -j REJECT # ads.imgur.com
/sbin/iptables -A OUTPUT -o eth0 -d 103.31.6.36 -j REJECT # ads.imgur.com

Then place the script in the /etc/network/if-up.d directory and then make it executable.

Bash
┏jcartwright@jcartwright-System-Version╼╸╸╸╸╸╸╾
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━◉:/etc/network/if-up.d$ sudo chmod +x ads
[sudo] password for jcartwright:

Then run all scripts in the aforementioned directory using this command.

Bash
┏jcartwright@jcartwright-System-Version╼╸╸╸╸╸╸╾
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━◉:/etc/network/if-up.d$ sudo run-parts /etc/network/if-up.d/
wpa_supplicant: unknown mode: ""
run-parts: /etc/network/if-up.d//wpasupplicant exited with return code 1

This worked very well. Connections to the ad networks in the iptables script will just timeout.

Bash
┏jcartwright@jcartwright-System-Version╼╸╸╸╸╸╸╾
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━◉:~/Documents$ ping clickthrutraffic.com
PING clickthrutraffic.com (95.211.219.66) 56(84) bytes of data.
^C
--- clickthrutraffic.com ping statistics ---
79 packets transmitted, 0 received, 100% packet loss, time 79864ms

To download this list easily, use the htmlq utility and then save the content from the <pre> tags to plain text.

Bash
┏jcartwright@jcartwright-System-Version╼╸╸╸╸╸╸╾
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━◉:~/Documents$ curl --silent -L https://pgl.yoyo.org/as/iplist.php?ipformat=iptables | htmlq pre | w3m -dump -T text/html | awk NF > ads

This does work just fine to easily save the iptables list.


2 thoughts on “Use the iptables firewall to block ads on your Linux machine.”

  1. Could you be kind enough to list these IP tables or provide files in the format appropriate for inputting into our routers, since changing settings individually on all devices throughout ones home is tedious and inefficient, in general.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.