您的 IP 地址: 未知 · 您当前的状态: 受保护未受保护的未知
博客 How-To

Block ads and malware with your hosts file

With the internet full of flashy pop-ups, scams and malicious traps, security-concerned internet users look for ways to protect their privacy and stay safe online. Most of the time, a combination of common sense and reliable security tools can help you to block annoying ads and malware-hosting sites. However, if you need additional protection or simply are looking for alternatives, here’s one – you can edit your hosts file to block ads.

Lewis Lambert Fox

Lewis Lambert Fox

Jul 06, 2021 · 4 min read

Block ads and malware with your hosts file

In this entry, we will take a look at how to block harmful websites, banners, and trackers, by editing your computer’s hosts file. Not only does this method keep you safe from various cyber parasites and junk, it also makes pages that you visit load faster and saves you some precious bandwidth. Read on to learn how to block ads with hosts file.

What Is the Hosts File?

First, what is your hosts file location, and how does it work? The hosts file is a plain text file (hosts.txt) that translates websites’ hostnames into numeric IP addresses, before DNS (the Domain Name System). So when you enter a name of a website into your browser, the hosts file tries to resolve it first and only if the attempt is not successful, the request is sent to a DNS server. When the request is resolved by the hosts file, it doesn’t have to leave your device.

What’s handy about this method is that you can edit your hosts file yourself to specify the websites you want to be blocked or accessed through a particular IP address. Unlike in ad-blockers, changes that are made in the hosts file are applied not only on a particular browser, but across the entire OS,. So if you choose to block a website, you won’t be able to access it until you unblock it.

Depending on your security preferences, you can edit entries in your hosts file to block ad servers, third party cookies, banners and websites that are known for hosting malware or spyware. This also makes a great alternative to parental control software as you can block adult sites you don’t want your kids to visit.

Use the Hosts File to Block Advertisements and Malware

It’s time to learn how to edit hosts files. Editing your hosts file to block certain websites or pop-up windows from being shown on your browser is actually very simple, even if you are a complete technology newbie. The hosts file already resides on your OS, so you only need to make some changes in it. There are two ways of doing that:

  • Add a specific entry. If you wish to block a particular website (e.g. Facebook), all you have to do is open your hosts file and write down: 0.0.0.0 Facebook.com. Here 0.0.0.0 is “a non-routable meta-address used to designate an invalid, unknown or non-applicable target,” so any address you put after it will be inaccessible. Note that you can add as many entries as you want, each time starting a new line.
  • Use block lists. There are a number of publicly available lists of harmful addresses that you can add to your hosts file. Probably the largest and most reputable of them is Steven’s Black list, constantly collecting entries from several well-curated sources, such as yoyo.org, adaway.org, someonewhocares.org, Badd-Boyz, etc. To use it, simply download the list and copy-paste its content to your hosts file. Save the changes and close the file.

The good thing about public blocklists is that they are regularly updated, so you don’t need to do that yourself. On the other hand, these lists sometimes may block websites that you consider legitimate and would like to access. So it’s up to you which method to choose.

Editing Your Hosts File

To block a website from being shown on your browser, open and edit the hosts file. The location of the file depends on which operating system you are using:

Edit the hosts file on Windows

  1. Search for “Notepad” and select “Run as Administrator”
  2. Open the file: c:\Windows\System32\Drivers\etc\hosts
  3. Type 0.0.0.0 [domain name] or paste the list
  4. Save your changes

Edit the hosts file on Linux

  1. Open “Terminal”
  2. Type the following: sudo nano /etc/hosts
  3. Enter your password
  4. Type 0.0.0.0 [domain name] or paste the list
  5. Press “Control-x”
  6. Press “y” to confirm your changes

Manage the hosts file on MacOS

  1. Go to “Applications”, click “Utilities” and open “Terminal”
  2. Enter the following: sudo nano /private/etc/hosts
  3. Type your password and press “Enter”
  4. Type 0.0.0.0 [domain name] or paste the list
  5. Click “Control+x” to save your changes
  6. Click “y” to confirm

If by any chance you’ve changed your mind and want to unblock any restricted websites, you are always welcome to edit your hosts file again. Simply select and delete the address you need to access or the whole list. Save your changes and restart the browser. The website you wish to access is available again.

Hosts file ad blocking is a relatively simple and effective way to protect yourself from annoying and malicious ads. Try it out today!

How to further enhance online protection

There are many other ways to protect yourself online, of course. For example, you might want to modify your browser's third party cookies settings, to prevent websites tracking you.

And if you want a simple all-in-one solution for your privacy and security needs, try NordVPN. This powerful encryption service comes with built-in malware protection, which can spot and neutralize the threat of malicious online ads.

Whether it's ransomeware, spyware, or some other virus, malicious software is everywhere online. Keep yourself secure with NordVPN.

Online security starts with a click.

Stay safe with the world’s leading VPN