1. Home
  2. Mac OS X
  3. Block websites on macos hosts file

How to block websites on macOS from the hosts file

The hosts file is a file you will find on both Windows 10 and macOS. You can use it to block access to any website you want. We’ve discussed how you can edit the hosts file on Windows 10. If you want to block websites on macOS by editing the hosts file on it, you will find that it’s not as simple to access it. That said, editing it isn’t hard once you know what to do.

Block websites on macOS

In order to edit the hosts file on macOS, or even access it for that matter, you need admin rights. Open Terminal and enter the following command. You will be prompted to enter the admin user’s password.

sudo nano /etc/hosts

Enter the password and the new interface you see is basically the contents of the hosts file. Any changes you make here will be changes made to the file itself. You will not be able to run any commands in this area until you exit the file.

Use the arrow keys and move to the very last line, and then move to the next empty one. You’re going to enter the following here; www.domaintoblock.com

Make sure you do not skip the ‘www’ part. The URL given above is just an example so make sure you replace it with the one you actually want to block.

Example www.apple.com

Add as many websites that you want to block in this same format. Each website should be on its own line. The address will not change. Only the domain name that follows it will be different. Make sure there is a space between the address and the domain name.

Once you’re done making these changes, tap the Control+O keyboard shortcut to save the changes. Tap Enter and you’re done. You can now exit the file. Tap Control+X to exit it.

You’re now back in the normal Terminal interface where you can run commands. Here, you need to run the following command.

sudo dscacheutil -flushcache

This will flush the DNS cache. The command executes within seconds. Once it completes, open any browser and you will not be able to access the domain.

To undo the change, use the command given above to access the hosts file again and remove the lines that you added. You will use the same Control+O keyboard shortcut to save the changes and you will need to flush the DNS cache as well for the change to be applied.

1 Comment

  1. For some macs and browsers, you also have to type lines:

    0:0:0:0:0:0:0:0 facebook.com
    0:0:0:0:0:0:0:0 www.facebook.com

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.