Block ads and malware with your hosts file
Editing hosts files can allow you to block internet ads and risky websites. You can carry out this simple process on Windows, Linux, and macOS. Learn how to block pop-ups and banner ads and limit access to specific websites by editing hosts files.
What is the hosts file?
The hosts file is a plain text file that translates websites’ hostnames into numeric IP addresses, before the DNS (the Domain Name System).
When you enter a name of a website into your browser, the hosts file tries to resolve it first. Only if the attempt is not successful does the request go 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 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. This allows you to block websites known to spread viruses, spyware, and other malware.
Use the hosts file to block ads and websites
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 Black’s hosts list, which is constantly collecting entries from several well-curated sources, such as yoyo.org, adaway.org, someonewhocares.org, and Badd-Boyz. 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.
Editing the 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
- Search for “Notepad” and select “Run as Administrator.”
- Open the file: c:\Windows\System32\Drivers\etc\hosts
- Type 0.0.0.0 [domain name] or paste the list.
- Save your changes.
Edit the hosts file on Linux
- Open “Terminal.”
- Type the following: sudo nano /etc/hosts
- Enter your password.
- Type 0.0.0.0 [domain name] or paste the list.
- Press “Control-x.”
- Press “y” to confirm your changes.
Edit the hosts file on MacOS
- Go to “Applications,” click “Utilities,” and open “Terminal.”
- Enter the following: sudo nano /private/etc/hosts
- Type your password and press “Enter.”
- Type 0.0.0.0 [domain name] or paste the list.
- Click “Control+x” to save your changes.
- 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 will be available again.
You can also edit hosts files on Android, but this requires a rooted device and is a far more complex procedure.
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, choose a NordVPN plan with Threat Protection Pro, a built-in feature that includes malware protections and an ad-blocker.
Whether it’s ransomware, stalkerware, or some other virus, malicious software is everywhere online. Keep yourself secure with NordVPN.