33

Blocking website ads with a hosts file

 5 years ago
source link: https://www.tuicool.com/articles/hit/jYJV7vu
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
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 . URJFRfR.png!web 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.

Extensions

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

vmeIBva.png!web 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.

Pixels

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

AjiymyB.gif Pixel mario

I'm talking about this crap.

riQRJjm.png!web Marketing pixels

If you've readmy 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.

2aANvq2.png!web Writing entries in the hosts file

0.0.0.0 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 127.0.0.1 here in place of 0.0.0.0, but it's quicker to process 0.0.0.0 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. 

EzmauuF.png!web 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.

aY7Z32F.png!web

63eiEnu.png!web 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).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK