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

36 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!

  27. cheap michael kors grayson bag Says:

    Another smart idea for gifts for a 3 year old boy is to simply add on with more blocks for his building sets. You can get the wooden kind which might lose color or be drawn on but apart from this the blocks will stay in shape for as long as he needs them. Another good idea in this department is Lego or Clics. These are also toys that can last a life time and they give him lots of play to be busy with which is also much appreciated by parents that need some quiet…

  28. clash Says:

    Aԝesome article.

  29. Linnie Says:

    hello there and thank you for your info –
    I’ve certainly picked up anything new from right here.

    I did however expertise a few technical points using this site, since I
    experienced to reload the website a lot of times previous to I
    could get it to load properly. I had been wondering if your
    web hosting is OK? Not that I’m complaining, but sluggish loading instances times
    will very frequently affect your placement in google
    and can damage your high-quality score if advertising and marketing
    with Adwords. Well I’m adding this RSS to my email and could look
    out for a lot more of your respective fascinating content.
    Ensure that you update this again soon.

  30. Jazminne Says:

    Thank you SO MUCH FOR THIS SOLUTION. I didnt like that freeware option either. Cheers!

  31. kostas Says:

    when i press control and o this will be shown on my screen like i pressed o alone !! how can changes be saved ? i got a mac book pro bought in 2008 !!!

  32. kostas Says:

    i forgot to mention i still got the 10.5.8 leopard edition installed on my mac !!! :P

  33. Stephen Says:

    Thank you for sharing this!
    The instructions were a little confusing.
    But after looking at the screenshots it was clear to put the websites at the VERY END…not below localhost
    After restarting my browsers it worked perfectly.
    Again thank you for sharing this.

  34. Jestine Says:

    This post is very interesting but it took me a long time to find it in google.
    I found it on 14 spot, you should focus on quality backlinks building, it
    will help you to rank to google top 10. And i know how to help you, just type in google – k2 seo tips and tricks

  35. Says:

    Hi there! This blog post could not be written any better!
    Reading through this article reminds me of my previous roommate!
    He constantly kept talking about this. I most certainly will send this post to him.
    Pretty sure he’ll have a very good read. I appreciate you for sharing!

  36. Minecraft Bukkit Plugins Says:

    There’s definately a great deal to know about this issue.
    I really like all of the points you have made.

Leave a Reply