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, such as browser add-ons or a VPN with malware protection, 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 – the hosts file. You don’t need to look far – it’s right here, on your device.

In this entry, we will take a look on how to block harmful websites, banners, trackers, etc., 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.

What Is the Hosts File?

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 modify 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

Modifying 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: Here 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,,, 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.

Modifying 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 [domain name] or paste the list
  4. Save your changes

Modify the hosts file on Linux

  1. Open “Terminal”
  2. Type the following: sudo nano /etc/hosts
  3. Enter your password
  4. Type [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 [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 modify 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.

Did you find these tips useful? Let us know in the comments below.


Article by channel:

Read more articles tagged: Malware