Blocking Websites On Your Mac Using Terminal

If you want to prevent yourself from going to a website you can manually block a domain using the Terminal. To do so, you must edit your hosts file and redirect a domain to your local address. This could be useful to help you resist distractions while getting work done.
Video Transcript / Captions
Closed captioning for this video is available on YouTube: Blocking Websites On Your Mac Using Terminal.

So let's say you want to block a website on your Mac. Now you can set up a special account and use parental controls for that. But let's say you want to block yourself from going to a website. Like you want to resist the temptation from going to Facebook during the day while you're working. You just want to block Facebook so you just can't access it from your Mac.

Well you can do this using Terminal. So let's go to Terminal. I'm just going to use Command Space and then type Terminal and it launches. Now we're going to go and block it by editing a file called the Host File. So the way we're going to do it is we're going to type sudo, which basically is asking for superuser permissions because you're going to need it to edit this file. Then pico is the name of a text editor in Terminal. Then etc/hosts. It's going to ask you for your admin password. So you're going to have to type that in.

Now this is what it may or may not look like. It depends if you've edited it before or if you have an older operating system you've upgraded several times. You may have some older stuff in here. You want to use the arrow key to go down to the bottom and maybe create a little bit of a space there for the things you're going to add. You're going to type which is the address for your machine. That's called the local host. It's basically saying use this Mac as the location for whatever it is I'm about to say. Then you're going to type That's going to be on a line by itself.

Now you want to do the same thing,, for That is technically different domains.
Now I'm going to use Control O to write and return so it writes over that file. Control X to exit. Now I'm going to have to clear the cache because it's going to still know where is unless I tell it No. Assume that you have to go and refer to the Host File again. So the command for that is sudo dscacheutil space dash flushcache. You can see it there.

Now that I've done that facebook should be blocked. Let's go to Safari, and I'm going to refresh facebook and you can see it says Safari can't open the page because it can't establish a connection. So facebook is essentially blocked.

If I want to unblock it I can go back. Edit Host again. I don't have to retype this everytime. I can simply add a pound sign or number sign before the line which means it's commented out. So it will ignore these lines. Then I'm going to Control O, Control X and now facebook should work again. I still have to do the flushcache command. But now if I refresh the page facebook will work again.

Comments: 2 Responses to “Blocking Websites On Your Mac Using Terminal”

    1 year ago

    Can terminal be attacked by pishing if I access it so often to block websites? And add a fraudulent web site to redirect me to it?

    1 year ago

    Gaby: Phishing attacks involve tricking the human (you) into doing something. So I’m not sure how that would apply here. Would you obey an email that asked you to use the Terminal to change how a domain is handled?

Comments Closed.