How do I block ads on YouTube?


I dont mind an ad at the beginning but blocking just the ones random ones while playing would be nice.


I’ve written a Python script that can be run as a cronjob or separately. It will search the logs for youtube advertisement queries and add them to the local blacklist automatically.
It also creates a file blocklist.txt for further use.
For now when I detect an ad I run the script manually over ssh. Otherwise it runs once a day.

You can find it here.

Hope this helps


To clarify your terminology, you are adding these domains to your “blacklist” (a local file), not to blocklists (publicly accessible lists of domains).


That’s correct. It also creates a local file with the domains.


Thanks for the idea eLVee!

But how is that different than blocking those domains with a simple RegEx? Plus, you can’t know for sure that does are YouTube ads - you’ll be blocking pretty much every video…

Please correct me if I’m wrong, thanks!


Not really different. Just experimenting! So far it still plays all video’s and block almost everything from my side. But ran into some errors with .sn-. domains.
Regex is a lot simpler than this, but still. Liked a challenge to see how far I would come.

1 Like

this will block preroll but as of writing 8 out of 10 times it will stop the video from playing as google keeps redirecting to a different site randomly which is also blocked by the regex therefore the video wont play until a response is sent of a successful connection:

So what is needed to fix this? I guess BLOCKINGMODE=IP doesn’t work. Some people have reported that pointing the ad server domains back to the IP address serving works for some reason, but you don’t seem to be able to do that with regex blocking.


This thread is a bit on the verbose side. So how does one block YouTube ads.


One can’t use Pi-hole for that.

I use uBlock org. for Youtube and many other rascals.


Great add-on but the problem is it does not work on things like smart TV’s or Fire Sticks…


That a loss for us and win for Alphabet.

Create a free service and when everbody can’t do without it any more you are going to have return on your investment by introducing advertising and subscribtions.


I don’t mind an add at the beginning, but you start throwing random ads in 4 times in a 12 min video that’s just greedy.


pk198105’s solution works, it just needs that last issue to be resolved somehow.


I tried the python script and it ended up blocking most youtube video streams.

Had to disable it and remove the blocked domains it had blacklisted. It did more than just block the ads DNS hosts.


this will also block mobile ads or just youtube on pc in general?


Have blocked 100% ads on pc
Later make solution more easy for pi hole :smiley:


The exact same domains and ips now serve both ads and videos.

I guess uBlock blocks at the browser by checking the request urls. Blocking with dns isn’t going to work anymore :frowning:

I read someone talk about using a dns from a remote country to reduce the number of ads served but I don’t see how that would work since the ads are served based on request ip not resolving the dns.

Any other ideas?


Ads are based on ip address location


I’m not so sure about that. Using a vpn recently as a test the IP was located in US - checked the geo location of the IP and with trace route. But the ads were showing from Dubai.

I wonder if the dns configured there was resolving for Dubai instead of sending a geo local YouTube ip in US

Very odd anyway.


That’s not correct. Yes, you can block ads/trackers delivered by 3rd-party services like That’s why Pi-hole works so well as it works on the DNS level.

However, if a (reputable) site like delivers ads via, say,


or whatever you can’t block this with Pi-hole - you would have to block completely (which is not what you want). You need an adblocker like uBlock Origin as the filter syntax used in its filterlists is much more flexible.

An ad like


can easily be blocked by uBO with the following filter rule:


You can’t do that with Pi-hole.