Use your hosts file to block ads and malware

Douglas Crawford

Douglas Crawford

February 12, 2016

The hosts file is a plain text file called hosts.txt that maps hostnames to IP addresses.

The Domain Name System (DNS) is a remote database used to translate the easy-to-understand and remember web addresses (URLs) that we are familiar with, to their ‘true’ numerical IP addresses that computers can understand: for example translating the domain name to its IP address of

In most operating systems the host file is resolved in preference to DNS requests, so if the hosts file resolves the hostname, the request never leaves your computer. This means the hosts file can be edited to block the domain names of ad servers, banners, third party cookies, and assorted other malware, adware and spyware.

For example, adding the entry “” to the hosts file will block all ads served by that DoubleClick server to any web page you visit. is “a non-routable meta-address used to designate an invalid, unknown or non-applicable target.” An alternative address often used is, which is the localhost address, but is preferred as it is faster (does not wait for a timeout) and it does not interfere if a local web server is running.

How to edit your hosts file Windows

  1. Search for Notepad, right-click, and Run as administrator.

Hosts 1

  1. File -> Open > c:\windows\system32\drivers\etc\hosts. Under the last # enter: [domain name]      e.g.

Multiple hosts can entered, with each one on a new line.

Hosts 2

  1. Edit -> Save.

Note that editing the hosts file may cause an alert from your anti-virus program. If so, then “allow” the change. In our example, if you now visit you will find the website blocked. To un-block a host, either delete its entry or add a # symbol in front of it.

Instructions for editing the hosts file in Ubuntu and Mac OSX can be found here.

Host lists

A number of websites provide lists of domains belonging to known adware and malware, etc. domains, which can be added to your hosts file.

The best of these that I know of is the StevenBlack’s Amalgamated host file, which “consolidates several reputable hosts files and merges them into a single amalgamated hosts file with duplicates removed.”

It currently contains 27,148 unique entries amalgamated from the following “high quality” sources:

StevenBlack defines a “high quality” source as one that is actively curated, with the bigger the hosts file, the more curation required!

Hosts 3

The actual file in the repository you want is called hosts. To use, simply cut & paste the host entries into your hosts file, then Save.

Hosts file vs. browser – based ad blocker

Those paying attention might notice similarities between blocking content using the Hosts file and the block-lists used by browser-based ad blockers such as AdBlock Plus and uBlock Origin.

Host file pros

  • Domain resolutions take place at a very low level (so are quick)
  • Use no memory or processing power
  • Blocks ads and malware across the entire OS, not just the browser

Host file cons

  • Difficult to enable/disable on a per-site basis
  • Can only block entire domains (not individual elements)
  • Hosts file lists usually updated less often

Ad-blocker pros

  • Easy to enable/disable from with the browser on a per-site basis
  • Higher granularity (page elements, wild cards)

Ad-blocker cons

  • Only blocks elements within the browser
  • High memory and processing demands

Personally, I use both, as I really hate ads!

Tip 1

Editing the hosts file makes a great alternative to using parental control software.

Tip 2

Using a VPN often causes the OS to bypass checking the hosts file. The uBlock Origin browser plugin, however, can be set to use hosts files such as those by Dan Pollock’s  and

hosts uBlock Origin

Go to the uBlock Origin dashboard -> 3rd party filters (second tab at top) -> Multipurpose (about half way down)

My thanks to reader ihavenoname for sending this tip in.

Douglas Crawford

I am a freelance writer, technology enthusiast, and lover of life who enjoys spinning words and sharing knowledge for a living. You can now follow me on Twitter - @douglasjcrawf.

16 responses to “Use your hosts file to block ads and malware

  1. Hello,

    Could you please tell me if writing `` would also block access to I would have assumed yes, but some host files (e.g. Adaway’s at has separate entries for both `` and ``. Shouldn’t the former make the latter reduntant?


    1. Hi NoobUser,

      Hmm. I think and are different servers, and therefore require their own entries. Perhaps more importantly, given that we are just talking about adding another line of text to the Hosts file, I see no real harm in covering all bases. Even if this does create a little redundancy.

    1. Hi Jake,

      Simply open up the hosts file (as administrator), delete any entries you have added, and save. If you are very concerned, then you can always make a backup of the original hosts file. This will allow you to delete the modified file and replace it with the original, should you wish to revert back.

  2. Thank you for your great effort in explaining the whole issue we face with add Block. I have been visiting few pages that I trust they are hardware reviewers like they review SSD, computers and etc,,, every time I browse this website they block me either I watch their videos or I disable my Adblock. Can you look at that matter with websites starting blocking you till you disable the Adblock ?

    1. Hi Mozef,

      Thanks for the suggestion. It is a great idea for an article. although I’m not sure there is are any foolproof solutions to the problem at the moment. FWIW, I use Privacy Badger instead of Adblock Plus. This is not banned by most websites (although some, such as Wired, still require me to disable it).

    1. Hi name,

      This depends on the VPN software used – for example the Cisco AnyConnect VPN Client appears to be well known for this issue. Many clients will push the VPN providers’ DNS settings to the local hosts file. I don’t now a way around this, I’m afraid.

      1. Hi ihavenoname,

        I use uBlock Origin, but cannot find a way to do this (admittedly uBlock Origin is different to uBlock). Could you expand a little further please?

        1. in ublock origin (firefox) go to dashboard, 3rd party filters (second tab at top), half way down is “multipurpose” which gives options for hosts such as MVPS

          1. Hi ihavenoname,

            Thanks! That is a great tip, and I have added it to the article :).

  3. Hi,

    Can you explain the difference between this method and Ublock (or any adblockers), and why this is better than have a adblockers??

    1. Hi Dunkhan,

      Hosts file domain resolutions take place at a very low level (so quick), use no memory, and no ram. They also block ads and malware across the entire OS, not just the browser. Downsides include not being able to enable to disable on a per-site basis, and that you can only block entire domains (not individual elements). This is a very good question, and I will expand the article to cover it.

  4. hi

    thanks for the StevenBlack’s Amalgamated host file URL;

    in my case, I collected ’round 500 urls… if i add the 27,148 unique entries of the SB’s list, will I experience some problems or any sluggish effect?


    1. Hi Terry,

      You shouldn’t do, as the lookup process is very quick, and hosts.text is just a plain text file (mine is only 15kb size with the entire StevenBlack’s Amalgamated host list added!). I certainly haven’t noticed any slowdown on my PC.

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Information will never be shared with any third party.
Enter your email address to receive your Beginner's Guide to Online Security for Free
You'll also receive great privacy news and exclusive software deals!
Enter your email to get the ebook:
Your Information will never be shared with any third party.
Enter your email address to receive your Ultimate Online Privacy Guide eBook!
You'll also receive great privacy news and exclusive software deals!
Enter your email to get the eBook:
Special VPN Deal
Exclusive Offer
Get a Special Deal - 72% OFF!
With a biannual subscription
Exclusive Offer for Visitors!
50% Off Annual Plan
Limited Time Only
Exclusive price of
Exclusive Offer
Get NordVPN for only
Exclusive Offer
Get NordVPN for only