How do I block ads on YouTube?

Thanks a lot to @ErikFontanel for the script!

However, I had to do some modifications, some of then suggested in this tread, to make it work, at least partially.

This is what I did:

#!/bin/sh
# This script will fetch the Googlevideo ad domains and append them to the Pi-hole block list.
# Run this script daily with a cron job (don't forget to chmod +x)
# More info here: https://discourse.pi-hole.net/t/how-do-i-block-ads-on-youtube/253/136

# File to store the YT ad domains
FILE=/etc/pihole/youtube.hosts

# Wolfram Alfa AppID
APPID=Your-AppID

# Fetch the list of domains, remove the ip's and save them
curl 'https://api.hackertarget.com/hostsearch/?q=googlevideo.com' \
| awk -F, 'NR>1{print $1}' \
| grep -vE "redirector|manifest" > $FILE

# Replace r*.sn*.googlevideo.com URLs to r*---sn-*.googlevideo.com
# and add those to the list too
curl "http://api.wolframalpha.com/v2/query?input=googlevideo.com&appid=${APPID}&format=plaintext&podstate=WebSiteStatisticsPod:InternetData__Subdomains&podstate=WebSiteStatisticsPod:InternetData__Subdomains_More" \
| grep -Po "r\d+---sn-.+.googlevideo.com" >> $FILE

# Scan log file for previously accessed domains
grep "^r*.googlevideo\.com" /var/log/pihole*.log \
| awk '{print $8}' \
| grep -vE "redirector|manifest" \
| sort | uniq >> $FILE

# Add to Pi-hole adlists if it's not there already
if ! grep $FILE < /etc/pihole/adlists.list; then echo "file://$FILE" >> /etc/pihole/adlists.list; fi; 
  • I have to wrap wolframalpha's url in double quotes " because simple quoting ' broke the url and make it dysfunctional. It had something to do with the & characters in the middle of the url.
  • As others suggested I changed grep -Eo "r\d±–sn-.+.googlevideo.com" to grep -Po "r\d+---sn-.+.googlevideo.com"
  • I added the variable APPID so it's easier to add they wolframalpha's AppID
  • Till there everything OK and seems that the $FILEgets more populated in each step. However, I can make the last step work. I mean If I run it on the CLI like below, test.txt is empty.
grep "^r*.googlevideo\.com" /var/log/pihole*.log \
| awk '{print $8}' \
| grep -vE "redirector|manifest" \
| sort | uniq >> test.txt
  • So I guess the last part is not collecting urls from the log. Besides, I don't see any of the ones that are not blocked in my log in the final file.

On top of all of that I still see youtube ads and I've notice that there are other kind of .googlevideo.com subdomains with a ixhs suffix. I don't know if this mean something.

r9---sn-uxap5nvoxg5-ixhs.googlevideo.com
r5---sn-uxap5nvoxg5-j2is.googlevideo.com
r2---sn-uxap5nvoxg5-ixhs.googlevideo.com
r4---sn-aigzrn7z.googlevideo.com
r12---sn-uxap5nvoxg5-ixhs.googlevideo.com
r16---sn-uxap5nvoxg5-ixhs.googlevideo.com
r4---sn-5go7yn7l.googlevideo.com
r1---sn-5goeen76.googlevideo.com
r8---sn-uxap5nvoxg5-ixhs.googlevideo.com
r1---sn-a5mlrnel.googlevideo.com
r6---sn-5hne6nlr.googlevideo.com
r6---sn-uxap5nvoxg5-ixhs.googlevideo.com
r7---sn-uxap5nvoxg5-ixhs.googlevideo.com
r5---sn-5ualdn7z.googlevideo.com
r9---sn-uxap5nvoxg5-ixhs.googlevideo.com
r10---sn-uxap5nvoxg5-ixhs.googlevideo.com
r3---sn-uxap5nvoxg5-ixhs.googlevideo.com
r5---sn-i5heen7l.googlevideo.com
r14---sn-uxap5nvoxg5-ixhs.googlevideo.com
r10---sn-uxap5nvoxg5-ixhs.googlevideo.com
r16---sn-uxap5nvoxg5-ixhs.googlevideo.com
r4---sn-uxap5nvoxg5-ixhs.googlevideo.com
r9---sn-uxap5nvoxg5-ixhs.googlevideo.com
r2---sn-uxap5nvoxg5-ixhs.googlevideo.com
r3---sn-ab5sznlk.googlevideo.com
r6---sn-uxap5nvoxg5-ixhs.googlevideo.com
r7---sn-uxap5nvoxg5-ixhs.googlevideo.com
r11---sn-uxap5nvoxg5-ixhs.googlevideo.com
r4---sn-uxap5nvoxg5-ixhs.googlevideo.com
r10---sn-uxap5nvoxg5-ixhs.googlevideo.com
r11---sn-uxap5nvoxg5-j2is.googlevideo.com
r9---sn-uxap5nvoxg5-ixhs.googlevideo.com
r3---sn-5hne6n7s.googlevideo.com
r5---sn-uxap5nvoxg5-ixhs.googlevideo.com
r5---sn-5go7yne6.googlevideo.com
r12---sn-uxap5nvoxg5-ixhs.googlevideo.com
r10---sn-uxap5nvoxg5-ixhs.googlevideo.com
r5---sn-ab5szn76.googlevideo.com
r6---sn-vgqs7nlz.googlevideo.com
r11---sn-uxap5nvoxg5-ixhs.googlevideo.com
r1---sn-hp57ynel.googlevideo.com
r6---sn-uxap5nvoxg5-ixhs.googlevideo.com
r12---sn-uxap5nvoxg5-ixhs.googlevideo.com
r1---sn-vgqskn7l.googlevideo.com
r7---sn-uxap5nvoxg5-ixhs.googlevideo.com
r4---sn-uxap5nvoxg5-ixhs.googlevideo.com
r8---sn-uxap5nvoxg5-5goe.googlevideo.com
r1---sn-5go7yn7e.googlevideo.com

But I can't see any of those in my /etc/pihole/youtube.hosts file

Ideas?

Try

grep "r*\.googlevideo\.com"
1 Like

Now... it works perfectly!

Thanks!

1 Like

I'm leave here the final script that worked for me after @Chipster suggestion.

#!/bin/sh
# This script will fetch the Googlevideo ad domains and append them to the Pi-hole block list.
# Run this script daily with a cron job (don't forget to chmod +x)
# More info here: https://discourse.pi-hole.net/t/how-do-i-block-ads-on-youtube/253/136

# File to store the YT ad domains
FILE=/etc/pihole/youtube.hosts

# Wolfram Alfa AppID
APPID=Your-AppID

# Fetch the list of domains, remove the ip's and save them
curl 'https://api.hackertarget.com/hostsearch/?q=googlevideo.com' \
| awk -F, 'NR>1{print $1}' \
| grep -vE "redirector|manifest" > $FILE

# Replace r*.sn*.googlevideo.com URLs to r*---sn-*.googlevideo.com
# and add those to the list too
curl "http://api.wolframalpha.com/v2/query?input=googlevideo.com&appid=${APPID}&format=plaintext&podstate=WebSiteStatisticsPod:InternetData__Subdomains&podstate=WebSiteStatisticsPod:InternetData__Subdomains_More" \
| grep -Po "r\d+---sn-.+.googlevideo.com" >> $FILE

# Scan log file for previously accessed domains
grep "r*\.googlevideo\.com" /var/log/pihole*.log \
| awk '{print $8}' \
| grep -vE "redirector|manifest" \
| sort | uniq >> $FILE

# Add to Pi-hole adlists if it's not there already
if ! grep $FILE < /etc/pihole/adlists.list; then echo "file://$FILE" >> /etc/pihole/adlists.list; fi; 
2 Likes

I don't know but I have the feeling that this is going to end blocking the whole youtube.
All the googlevideo.com queries are r*.googlevideo.com. So sometimes I have to reload if I want to watch a video so it changes the domain (or something like that).

This feature is now implemented and available for early-on testing if you want to get your hands dirty. I guess that means the race is open for the best blocking regex!

3 Likes

something interesting... I can't watch (some) videos from youtube in my iPad or iPhone either on the web or the app, but on the computer is OK...

When I check it's always the same domain blocked r16---sn-uxap5nvoxg5-ixhs.googlevideo.com.

I don't know if this is the best approach... I still get adds from time to time in the computer but I can see some videos on other devices anymore.

This has been reported above also.

I can confirm that blocking these domains, affects youtube playback on mobile devices.

doesn`t exist

clients6.google.com blocks messages from Hangouts from being sent.
Does anyone know what i.ytimg.com is? I see it quite frequently.

1 Like

I think it handles the youtube thumbnails iirc.

You're right. Thanks!

Can someone explain me step-by-step how this work? How can i use the script? I see many scripts in this topic. Wich must i use?

don't worry they aren't working... I'm using them and I still see ads.

There is still no solution to block YouTube ads on your network?

I have the solution revealed here and it's not working. Even worse, sometimes it's blocking ads on mobile.

I'm not an expert in this field, but I think the real challenge is separare youtube's domains that bring to you videos you want to see and videos that are ads.

This is exactly it. Youtube was first to do this and it has worked very well for them to defend against DNS based ad blockers

I've read that there ABP seems to block YouTube ads but I've haven't tried and I probably won't. I'm not sure what they are using.

I use ublock origin.

I have added the lists from uBlock Origin into my PiHole, because they are open source and the lists are avail on github.
Not sure about ABP.

add-ons and applications like uBlock, and ABP use a slightly different technology to block ads. where pi-hole only looks at XXXYYY.com
applications like listed above are able to look at XXXYYY.com/ZZZ