Skip to main content

How to block online ads with the hosts file

Am I the only one who is skeptical clicking on ads I see online? Yes, I know it is the lifeblood of entrepreneurs, but I really don't care to view more than I have to. We all know the 7 +- 2 rule; we have a limit of the amount of information we can take in at a single time. It's a fact. We are not infinite in our abilities alone - let's just let the computers do the thinking for us. 

Google adsense ads
Inline Adsense ads

While I recently tried to set up a more elegant solution, I wanted to share with you how you have the power to block ads (in case you didn't know already) and regain [more] control of what you are looking at online.


This is the easy answer, just install Adblock Plus (Chrome).

Adblock plus (credit to
Adblock Plus on the Chrome web store

Adblock does it all for you. Ads? No more. It's really a golden bullet. However, if you want to grow as a developer, sometimes it pays to try and do things in a different way in order to learn how more things work under the hood.

Hosts file

Do you remember that little file you may have heard about in your networking class? Well, here it is again. The hosts file.

Fun fact, the hosts file was a precursor to DNS.

A simple file that maps IP addresses to host names, how can it help block ads online? Before we get into that, we need to briefly understand how ads appear on our websites in the first place.


Ugh. And no, I'm not talking about these pixels.

Pixel Mario (credit to
Pixel mario

I'm talking about this crap.

Chrome network screencap of marketing pixels
Marketing pixels

If you've read my other post, you'll understand more that marketers are profiting off selling your information (and how things should change so you should get paid for your information instead), but I digress. 

In a simple form, 1x1 <img> tags that are in the HTML of webpages send data to marketers. These tags are provided by marketers, who in turn of taking user behavior information, provide the website owner with $. 

The website owner also puts on their webpages a marketing script, along with some <div> tags to hold the ads themselves. When you visit the webpage, the marketing script loads, pulling information the marketer already has, and populates the <div> tags with ad content.

Of course, this is very simplified and does not apply to every party who interacts with marketers. Some parties only use the pixel, others just use the ads. There's a web of business here.

Using the hosts file

If we think what the hosts file can do, it can actually give us complete control over any ads. How? We can write in the hosts file that we want a particular host name to map to null. Null in a hosts file looks like this.

A sample hosts file
Writing entries in the hosts file is the invalid, un-routable address. By using this for all the host names that load pixels, we can block marketers from tracking our user data, as well as prevent ads from loading on webpages.

Technically, you could also use here in place of, but it's quicker to process and it's easier to type as well!

Doesn't that mean we have to type out every single host in our hosts file? Yes and no. You can take the liberty upon yourself and get carpel tunnel (please don't), or - you can just get a curated list from the github repo. Copy the full list (raw list link here) and save it in your hosts file. 

A Powershell command prompt
Being cool and opening the hosts file up from Powershell

The hosts file is at C:\Windows\System32\drivers\etc\hosts. Save the entries in the file (be sure to run your text editor as an administrator in order to save the file!) and run this command in a cmd prompt or Powershell prompt to flush the DNS cache so your updates take effect right away. 

ipconfig /flushdns

You should see happy errors in your web browser's console now.

Blocked pixels in Chrome's console tab

Google ad not loading
Sorry Google

Next steps

Obviously, this only works on your local computer. We will explore in a future post how to block ads for your entire network (yes - this is possible).


  1. My favorite pre-configured hosts file that is updated on a regular basis:

    1. Thanks MVPS is awesome. I loaded it on my iPhone and use AdBlocker that loads all the DNS entries. Then I run a VPN to that AdBlock to block those ads.

      For Windows, there's an old program called eDexter that will turn the ugly can't find host to 1x1 pixel image (local) or any image of your choosing like a smiley face, or whatever.

  2. Great article, that's old school and I like it. Alternatively, if you have a machine at home, try a pihole.

  3. Check out unified hosts file:

  4. The way I block all the ads both in my phone and laptop, I have a running proxy server (Squid) on my Digital Ocean server and I'm connecting to the internet over this proxy. I found the list below and added to the Squid's blacklist. It has 2,975 different ad platforms so I don't see any ads even in my phone. But I have a smart TV and it doesn't have a proxy feature so I wonder is there any solution for that.

    1. @Ozgur, some home routers allow you to setup these lists as well. I'm running a custom firmware on my router (AdvancedTomato) that lets me subscribe to a list. It updates that list nightly. So now, anything that's on my home network (wired or wifi) passes through that list. This would also cover a smart tv.

    2. @SeanW They don't have a firmware for my router (Huawei B618S-22D). But that's a great advice, thanks. Maybe I'll find another custom firmware like that. And I have a Raspberry Pi Zero W. Maybe I can create an access point in Raspberry somehow and use a proxy server to block ads but I don't know.

    3. I used to have adblocker on the router level. Used Tomato as well with a block list. I kept it minimal, but I found there were some instances where I wanted to visit a site or ad block broke the web site too much. On a phone or computer, you can turn it off easily enough per site... but on the router level, you have to go in and whitelist the line. It is a burden compared to individually whitelisting.

  5. This is pretty much what Pi-Hole does. But for your entire network - so all devices connected to WIFI get an ad-free (or ad-lite) experience.


Post a Comment

Popular posts from this blog

How to maintain good health as a programmer

It's important to improve your skills, but you can't neglect your health as a programmer. Here are four, very easy steps you can take to maintain your health at your desk job.

Protect your eyes You can read the full American Optometric Association paper, or you can simply wear eye protection. Bottom line, is UV light harms your eyes. That must be why I really enjoy looking at deep-blue Christmas lights...
I would recommend any pair of glasses while on your computer that have blue-light reflective coating, or a blue light filter. I personally have a pair of these, and I immediately noticed a difference at the end of the day - my eyes didn't feel sore or tired (that is what long exposure to UV light does to your eyes). This is also a good article on the subject.

You can also consider monitors that have built-in blue light filters, but they tend to be more on the expensive side of things.
Follow the 20-20-20 rule Every 20 minutes, look 20 feet away for 20 seconds; it's wha…

[Automatically] minifying .cshtml files in .NET Core

What gets me more excited than anything? Performance.

Its always exciting to me to perform at max efficiency, because why should you be performing any less? What's even better than efficiency than setting it and forgetting it? Nothing. Fact of the matter is while you may not be caring about efficiency, you should - because your business does. Lack of efficiency costs dollars and time.

You aren't off the hook even if you are doing programming as a hobby or are self-employed.

We are going to explore how to minify your .cshtml files in a .NET Core web application, and why you should care to do so.

Why you should careDo your reading;read this too.
Time is a limited resource, and we are tending to be more impatient nowadays, these people think so too. The bottom line is that if you want a chance for your web application to be successful, you can't skimp on quality and hope that you will be a success. Your applications need to deliver quality, and an "easy" way to conve…