How to Block or Unblock Websites on Mac Terminal

If you have been using Mac Terminal for any length of time, then you know how powerful it can be. You can do things behind the scenes to configure and change how your Mac performs. You can often do things that you can do from the desktop.

Blocking and unblocking specific websites can be particularly useful if you have children or employees who use the computer and you want to prevent them from visiting certain websites.

My name is Eric, and I am a software engineer and technology enthusiast. I enjoy using command-line interfaces like Mac Terminal, and over the years, I have learned many cool tricks. I also enjoy teaching others some of the things I have learned.

If you want to see how to use Mac Terminal to block or unblock websites, keep reading, and I will show you this simple method that anyone can do.

Steps to Block a Website with Mac Terminal 

Blocking a website using Mac Terminal is a quick and easy process, but you will need to make sure you have an administrator’s account and know the password to that account. This should be the account you used to initially set up your Mac.

Step 1: Open Terminal

Open LaunchPad and type Terminal in the search field at the top of the screen. You will then see the icon for the Terminal application—double-click on it.

Step 2: Open the Hosts File 

This is where you will need your administrator account password. This file is protected so that only accounts with admin privileges can open and modify it.

Type the command below to open up the hosts file.

sudo nano /etc/hosts

Once you hit enter/return, you will be prompted to enter your password. Type in the password and hit enter/return again. 

The file will then open in the editor on your screen.

Step 3: Locate “127.0.0.1 localhost”

In the editor, find the line that reads 

127.0.0.1 Localhost

Use the arrow keys on your keyboard to cursor down to the line just below 127.0.0.1 Localhost and hit the enter/return key to enter a new line.

Step 4: Enter the Websites You Want to Block

Now that you have a new line, enter the website that you wish to block by entering 127.0.0.1, then the tab key, and then the URL of the website you wish to block. The format is 127.0.0.1 <TAB> <website url>.

For example:

127.0.0.1 www.espn.com

If you have other websites you want to block, you can continue adding them as new lines and add as many as you would like.

Step 5: Close the Terminal

Once you have added all the websites you wish to block, you can exit the terminal, but you will want to save your changes first. Hit CONTROLO to save the changes, hit enter/return to save as the existing filename, and then hit CONTROLX to exit the editor.

Step 6: Open Your Browser and Test It Out

Now you will want to test it out to see if the website is really blocked. Open whatever browser you prefer. Type in the website and hit enter/return.

If it works, you should see that your browser will not go to the website, and you will get a message similar to the one shown below.

Steps to Unblock Website on Mac Terminal 

Since you now know how to block the website, you will also want to know how to unblock the website. Follow the steps below to unblock a site you have blocked.

Step 1: Open Terminal 

Open LaunchPad and type Terminal in the search field at the top of the screen. You will then see the icon for the Terminal application—double-click on it.

Step 2: Open the Hosts File 

Open the hosts file the same way you opened it in step 2 above when blocking websites. 

Type the command below to open up the hosts file.

sudo nano /etc/hosts

Once you hit enter/return, you will be prompted to enter your password. Type in the password and hit enter/return again. 

Step 3: Find the Blocked Website

Locate the website you wish to unblock and use the keyboard arrows to cursor down to that line.

Step 4: Use # to Unblock the Website

Type the pound symbol # at the beginning of the line. This symbol represents a comment and will keep the system from reading this line. 

You can just delete the entire line if you wish to do so, but using the comment method allows you to keep the line in the file and easily block the website again if you need to.

Step 5: Save and Exit the File

Save and exit the file by typing CONTROLO, hit enter/return, and then hit CONTROLX.

Step 6: Open Your Browser and Test It Out

Open your browser and test to see if you can now access the website.

Conclusion

Blocking and unblocking a website using Mac Terminal is quick and straightforward, as you can see from the steps above. If you would like to see a demonstration of how to do this in a video, you can take a look here.

As usual, let me know if you have any questions or feedback. I would love to hear from you!

Leave a Reply

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

  • Rado

    This method previously worked for me, it’s not now unfortunately. I don’t know what changed.

    Reply
    • Eric

      Hi Rado,
      I’m sorry to hear this is no longer working for you. There are two things that you can check to see if they are causing an issue. First, try checking your spelling and ensure that you have the web address spelled exactly as it is online. If needed you might want to copy and paste it from the original url at the website. Second, make sure that you do not have the one commented out. If you have a # symbol in front of the line it will comment out the line and it will have no effect at blocking the website.

      I know that you said it was working before, so it is odd that it has stopped working suddenly. You might try a simple reboot of your system to see if that fixes things. If it does not, do you know of anything that has changed since it stopped working? Have your upgraded your macOS recently? I hope that you are able to find a resolution to this problem.

      Reply
  • ruru

    errr i am getting quite confused but it’s really nice that your posting this so everyone can see!

    Reply
    • Eric

      Hi ruru,
      Thank you for the feedback. I am glad that I can post information like this to help Mac users. I hope that you have been able to figure out whatever it is you needed. Please let me know if there is a specific issue you are having with trying to block a website using the above method.

      Reply