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.
- To get started, launch Terminal (Applications > Utilities).
- 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
- 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.
- 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
- 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 - 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
- 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.
- 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.
November 3rd, 2010, 12:07 AM
Help! I put in:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
Into terminal and nothing happened : ( Whats is going on?