How To Block Websites on Your Mac Using Terminal

terminal icon new

One of the greatest time wasters today has got to be social-networking websites. More specifically, Facebook and Twitter. I found myself spending unnecessary amounts of time on both of those websites during work or when I had nothing else to do. Let’s not lie, it’s an addiction, and many people are guilty of it besides me. Fortunately, there have already been temporary solutions such as SelfControl, a  freeware application developed by Steve Lambert. In the past I used SelfControl because it was simple and allowed me to setup a time frame that I could block out Facebook and/or Twitter. For students, I can almost certainly recommend using that application during finals week or when preparing for any exams in general. However, I wanted to stop frequenting Facebook on my Mac, so I needed a more permanent solution other than deactivating my account because I still want to be able to access Facebook from my iPhone. With a view commands in Terminal, this can be done in no time!

  1. Open up Terminal in /Applications/Utilities.
  2. In the command line, type in "sudo nano /etc/hosts".
  3. When asked, type your admin password.
  4. In the screen that will appear, you’ll see the host database for your computer.
  5. Using your down arrow, go to the bottom, right under where it says, " localhost".
  6. Type in "".
  7. On a new line, type in ""; this is for extra measure.
  8. Now, hold down Control + “O”. This will save the changes to the host database.
  9. Hit “Return” and then Control + “X” to exit the screen.
  10. Remove the existing cache by typing in "sudo dscacheutil -flushcache" in the command line, this refreshes the existing cache on the host database.
  11. You should receive an “Unable to connect” error message when you go to now.
  12. Now, back to doing something more productive…

screenshots: 1 2-3 4-7 10 11

26 Responses to “How To Block Websites on Your Mac Using Terminal”

  1. sarah Says:

    Wow this actually works, thanks!

  2. Michael Says:

    This is awesome! I’m going to use this during finals this semester. One question however… How do you undo it afterwards?

  3. Mason Sklut Says:

    Simple, just go back into Terminal following the same instructions and locate the website you blocked and remove it.

  4. Bob Smith Says:

    Ignore the double period in 127.0..0.1. That was just a typo on my part.

  5. gus Says:

    Cheers dude!

  6. chloe Says:

    I’ve tried a few times and it doesn’t seem to be working for me. Do you need to restart your computer or something?

  7. Dana Says:

    this did not work for me…what did I do wrong? I followed the instructions exactly.

  8. Aaron Says:

    Unfortunately, this does not work with all sites. It might be an IPv6 issue, but sites like facebook, yahoo and spotify resolve every time.

  9. Wyatt Says:

    How does one unblock this function. I used this method to block Facebook on my MacBook Pro about a year ago and now I’m trying to unblock it with no success. I keep on getting to the terminal and deleting the text edit “127….Facebook” etc and then I can’t get my deletion to save and stick. Every time I exit the terminal and then re-open the terminal the Facebook blocking text is still there. What do I do from here?

  10. Richard Says:

    How can I block facebook access over a whole lan? It is posible?

  11. Nancy Says:

    When I try to save I get an error message stating:

    “Error writing etc/hosts: No such file or directory”

    How can I overcome this?

  12. Kael Says:

    @Nancy: you have to type “/etc/hosts”, not “etc/hosts”.

  13. jay Says:

    simple steps to block the access of a site ..really nice but if you want unblock that website in future you can use VPN SERVICES. You can also use proxys. I tried and was able to access youtube in my college.

  14. Kashif Gul Says:

    when i type sudo nano /etc/hosts and give password, it says that [user] is not in sudoers file. this incident will be roported

    how can i add user in sudoers file?

  15. Marvel War Of Heroes Hack Says:

    naturally like your web-site however you have to take a look at the spelling on several of your posts. Several of them are rife with spelling issues and I to find it very bothersome to tell the truth nevertheless I’ll certainly come again again.

  16. daily engagement social break marketing advertising Says:

    Amazing difficulties below. I am incredibly contented to look this post. Thanks and I’m taking a look in front to the touch you actually. Will you generously decline us a email?

  17. jeff Says:

    anyone help, when i go to yahoo to games and click on pool, i get a message my computer will not allow site to open, something about security. Ive tried and can not figure out how to open. Anyone have any sugestions.


  18. William Says:

    Well we followed correctly once before, the instructions for blocking Facebook from our teenagers with success, on our MacBook Pro! It worked wonderful!
    I did a deletion of Facebook in terminal to help us sell a few things, that worked correctly also!
    But now can’t get Facebook blocked again, as after typing in “sudo dscacheutil -flushcache”, it’s not working to block Facebook!
    So what do we do from here? Can anyone kindly help us out please?


  19. Elizeu Says:

    William, maybe you’re doing the “copy&paste” the url… try write it down manually… ;-)

  20. William Says:

    Thanks Elizeu.
    But funny thing, it somehow corrected itself????
    Have no idea how, so it’s all good again.
    I’ll note down your response anyways, cause it’ll be handy to have!


  21. Says:

    Hi there, I desire to subscribe for this weblog to obtain hottest updates,
    therefore where can i do itt please help.

  22. Jonathan Morgan Says:

    Hi there,

    Thanks for posting this – I’ve been using this method for a while, and it’s been really helpful in keeping me focussed on work.

    However, because Facebook links/social media buttons are so prevalent on the web, I’m seeing a lot of Server Not Found messages.
    Can you suggest an easy way of blocking or hiding these messages?


  23. man and van reading Says:

    Attractive component of content. I just stumbled upon your website and in accession capital to claim that I get in fact loved account your weblog posts. Anyway I will be subscribing for your augment and even I fulfillment you access constantly quickly.

  24. Says:

    Appreciate one more insightful website. The place in addition may I get that will variety of information printed in this type of best technique? I have a task that we are only right now working away at, and I have already been with the glimpse out for similarly info.

  25. Yanis Says:

    How do you the remove the websites to unblock them? I can’t select them all and hit delete…nothing happens. Thanks!

  26. Stacey Jan Says:

    This is a great blog! Check out the top web hosting sites on the internet for 2014!

Leave a Reply