Subscribe to MacYourself via RSSFollow MacYourself on Twitter

How to block access to a website in any Mac internet browser

How to block access to a website in any Mac internet browser

No matter what browser you use – Safari, Firefox, Opera, Chrome – you can easily block access to certain websites by editing your Mac’s hosts file. In fact, it’s completely free and no extra software is required.

Parents, employers, and computer administrators are well aware that all websites should not always be accessible from all computers. Children, for example, should be blocked from common adult websites. And it might come to a point where employees need to be prevented from visiting productivity-wasting social networking sites. Whatever the reason, blocking certain websites can be done in Mac OS X by editing the hosts file.

A Mac’s hosts file is a simple text file that dictates what the system should do when specific domains or IP addresses are accessed. You can trigger a “page cannot be displayed” error or even redirect them to other domains/IPs of your choice. For the sake of this tutorial, all blocked websites will be pointed to 127.0.0.1, which is the localhost or system itself.

  1. To get started, launch Terminal (Applications > Utilities).
  2. As is the case when any major system settings are being changed, making a backup is a good idea. So let’s make a copy of the hosts file before diving in. Copy and paste the following code in Terminal:

    sudo /bin/cp /etc/hosts /etc/hosts-original

  3. Press Enter/Return on your keyboard to run the command. Terminal will ask for your password. Type it in carefully and press Enter again. The cursor will not move, so it will look like your keystrokes aren’t being registered. Rest assured they are, though.
  4. Now we can start editing the hosts file. Copy and paste this code in Terminal (all one line):

    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

  5. The hosts file will open in TextEdit. Notice the following lines and do not delete them under any circumstances:

    127.0.0.1                 localhost
    255.255.255.255     broadcasthost
    ::1                            localhost
    fe80::1%lo0             localhost

  6. Create a new line directly under the last one shown above. Type the following, replacing the sample domain with the domain or IP address you want to block:

    127.0.0.1 sample.com www.sample.com

  7. Continue adding lines following the format in step 6 for each website you want to block. When you’re done, quit TextEdit and save the hosts file when prompted.
  8. Back in Terminal, run the following command to flush the computer’s DNS and put the new hosts file into effect. You can restart your Mac instead, if you prefer.

    dscacheutil -flushcache

That’s all there is to it! Websites added to the hosts file will no longer be accessible from any user account on that Mac. If you want to re-enable access to blocked websites, just repeat this process and remove the lines you added in the hosts file. Just remember not to delete the original 4 lines shown in step 5.

Tags: , ,

14 Comments Have Been Posted (Leave Your Response)

That’s a great tip. For anyone who doesn’t like using Terminal, there’s a free app you can download that lets you change the hosts file and activate the changes.

It’s called Gas Mask – http://www.clockwise.ee/gasmask/

Help! I put in:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
Into terminal and nothing happened : ( Whats is going on?

well i had unblocked the website as it said it didnt work so i tried deleting the file but it still didnt work could you please help

thanks works great

HELP!! When I do step 4 it tries to open the text fil, but then automatically closes it again and when I try inserting the same command it tells me the following:
“2012-10-15 21:43:10.982 TextEdit[988:8e07] NSTrackPersistentURLs: LSSharedFileListInsertItemURL() failed at inserting URL file://localhost/etc/hosts (/etc/hosts)”

I’ve tried restarting the Terminal, but then it doesn’t work anyway. Once I got to the text file with the second attempt, but then it didn’t allow me to edit it saying I didn’t have the rights. Probably it’s because it didn’t ask me for the password when I did it for that second time. But most of the time with the second attempt it doesn’t want event to complete the first step properly.

I’ve tried restarting computer, it still doesn’t work- does the same stuff again.
Please can anybody tell me why is that, can I fix it and make it work?
Thank you!

I see, there’s not much response going on to this post :(
Still hoping to get some at some point though!
Thanks!

Don’t do this in textedit do it entirely in terminal. After the first command do: sudo nano /etc/hosts that will open it in the nano editor. Make your changes hit control-x then y then enter.

Had the same problem doing this in V 10.7 as
Hosts file was in a different location:

/private/etc/hosts

Thanks for the instruction. I get to the point where I want to add the line to the host and I get a message that I do not own the host and I can not change it. I have the option to create a duplicate. How do I get around this? Thanks for the help.

I have the same problem as Dan M – although I put in the computer password, I get to the final bit, where you add the website and it says I can’t but says do I want to create a duplicate. What’s the problem and how do i solve it?

It didn’t work in parental controls, but your instructions works, so I highly appreciate it, so now I can do my mid-terms without the guilt of procrastination! :D Thanks A LOT!!

You are having problems editing hosts file because by default, only the system has rights to write to it. To change the permissions from the default of 644, Type “chmod 664 hosts” (without quotes). You may want to change it back to 644 after your done editing hosts. Note that if you ever run “repair permissions” from disk utility, it will get changed back to 644 since that is the default.

I changed hosts, but it still not working. I mean I still have access to thewebsite

[…] 명시 적으로 차단하는 소프트웨어가 설치되어 있지 않다고 가정하면 웹 사이트를 차단하고 차단 해제하는 방법이 있습니다 […]