Quick Links

Whether you've given a computer to your child or just want to keep things clean on your own machine, blocking sites that serve malware, porn, social networking, and gambling en masse is useful. And while there's lots of third party software out there for the job, the hosts file is a built-in option for every major operating system.

Related: How to Edit Your Hosts File on Windows, Mac, or Linux

The hosts file is a simple text document checked by your computer every time you connect to a domain name, meaning you can use it to redirect requests to sites you'd rather your computer not be able to access. We've shown you how to edit your hosts file on Windows, macOS, or Linux; here's how to use that power to block entire categories of websites on any machine using a beautifully curated list found on Github.

How to Block a Single Website

Let's say you want to block an individual site, like Facebook. Doing this is relatively straightforward. First, open your hosts file, then add this single line to the end of the document:

0.0.0.0 www.facebook.com

Save the document, and you will no longer be able to access Facebook in any browser. (If you can, try clearing your DNS cache or restarting your computer.)

So why does this work? The text we added, 0.0.0.0 www.facebook.com, is two things: an IP address followed by a URL. By adding these two things in order, we're telling the computer to route all requests for facebook.com to the IP address 0.0.0.0, which is not a routable address. The result: the site fails to load.

That's the basic principle. Now let's soup this approach up and block everything.

...Everything.

Block Malware, Porn, and Gambling Sites En Masse

If you want to block entire categories of websites---malware, say, or porn---you've first got to track down every single URL associated with those activities. Happily, the unified hosts file repo on Github has done this work for you. A curated list combining the efforts of many other communities, this page offers hosts files for blocking several combinations of categories.

All of the files block adware and malware, but many block other things such as porn, gambling, fake news, and even social networks. Find the combination of things you'd like to block, then click the link to the raw hosts file.

Select all the text, copy it, then paste it in your hosts file. Your computer will now block all of these things.

That's right: no more porn. No gambling either. Guess you'll just have to do some work.