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.

    1. Yes, I completely agree with you. Pi-Hole does it one step better than the hosts file.

  6. For a change, there is an article that focuses on a specific topic and is not all over the places. I like the fact that it is actually problem-solving and not just random and baseless subject. Great going!
    Report Writing Services

  7. I was able to clear a couple of doubts, for which I’m deeply thankful. It is rare that you come across such an insightful work. A big thumbs up from my side.
    dubai bus schedule
    17 bus route dubai
    Dubai City
    Place to visit in Dubai
    28 bus route Dubai
    ikea dubai
    Dubai Gold Rate
    Skydive Dubai
    Dubai mall cinema
    c18 bus route

  8. The good thing about this blog is that I can relate to it myself. It is surely going to help me in my future endeavors. Keep up the good work. dubai bus routes and timings


Post a Comment

Popular posts from this blog

UI redesigns are mostly a waste of time

To preface the article, I primarily work on, and prefer, back-end code. I've been involved in both web and software development for over 4 years now and worked with many front-end and back-end frameworks.

Before all of the UI designers that read this go out and riot and champion against me for saying UI redesigns are a waste of time, let me say that I do value design. I think at the bare minimum, a product or website needs to be usable, and if you possess a good eye and steady hand, you should feel compelled to create something that looks pleasing.

Just stop redesigning the UI all the time.

UI redesigns, in my opinion, are a waste of time 95% of the time. Let me explain further.

No one cares Come see our fresh new look! What about our new material design, come see! I'm sorry, but besides fixing the UI where it impacts the usability of your application, no one is raving about how a redesign makes the application any better.
People use applications because of their purpose, not be…

Two productivity tricks in Visual Studio you need to be using

Up your productivity game by using these two tricks you probably didn't know existed in Visual Studio.

F1 key Starting with the best, the F1 key. You didn't know this key did something did you, well, you'll be surprised to know that the F1 key opens up a help page on whatever you have your cursor on. Don't know what a keyword means or does, don't know what the parameters of Parallel.ForEach are, hit F1.

Clicking the F1 key while your cursor is over the SameSiteMode as shown in the above screenshot, takes us to this page where we can learn more about the SameSiteMode.
The task list For when you want to clean up your code base, open up the task list and get on to fixing those bugs! The task list opens a window that shows all instances of your //TODOs(as well as other symbols in your code). You can open this window by going to View > Task List or (ctrl + \, T).

Take this example.

The blue highlighting on the TODO comment is coming from Resharper, and isn't going …