Using HOSTS File To Block Web Access 4

Terminal

Share
Using HOSTS File To Block Web Access

Hey

Quite a while ago I mentioned about using Parental Controls to restrict the sites that are accessed on an account. There is a higher level version of this that can be used to block complete access to websites, the good thing about this trick is that it is a lot harder to remove, and for enterprising kids it will be a lot more difficult to find out and remove.

This tip is going to modify a file called hosts, its basically a text file which tells you computer what to do with specific domains and IP addresses. It similar to your DNS except it is used to change where they go. Hosts file is a high level file, as a result it will always take precedent over other files and applications. You do need a bit of technical experience with this tip, so proceed with caution.

The simplest way to do this is as follows, if you know how to use Terminal and nano you can use that. The first step is to follow this tip, and allow TextEdit to become an admin user. This allows you to edit the host file. First copy and paste this command into Terminal.

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

In TextEdit, open up the open new file window. This will be used to open the hosts file. Due to the way Finder works you won’t be able to navigate to the specific file, due to this open Finder, and go to Go > Go To Folder and type the following

/etc

The window that pops up contains lots of important files which allow your computer to run. In the list there should be a file called hosts. Drag this file onto the open window of TextEdit. The file should then open.

This is now you text file, and you should see a couple of lines. Do not delete any of the lines. The file should look like the following:

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

This contains IP addresses on the left and websites on the right. Currently there are no websites in the list. To add one to be blocked type the following:

127.0.0.1 www.example.com example.com

When you save the file any time you access example.com or www.example.com it should produce a 404 error. This file is sending any requests to example.com to your home IP address, since there is nothing there it wont load a page. If you host a website on your computer you can always change 127.0.0.1 to 0.0.0.0 As a result you can’t access the website. Ideally you don’t need to add the www and the none www, bit however this should work 100% of the time. The host file will take a couple of minutes to come into effect so, sit back and wait if the results don’t work straight way.

This method shouldn’t be used as a comprehensive list of website to block, since its a convoluted method to add them. Its best used to block one or two websites which you really don’t want to be accessed.

To restore access simply delete the line or add a hash (#) in front of the IP address. When you have completed finished quit text edit and return to what you were doing.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips

4 Responses to “Using HOSTS File To Block Web Access”

  1. 1

    use:

    dscacheutil -flushcache

    in terminal after editing and saving the /etc/hosts file to make the changes work straight away..

    Comment By m.piatek on July 18th, at 6:58 pm

  2. 2

    Very cool, this might come in handy.

    Comment By Tim on July 27th, at 3:26 pm

  3. 3

    Tried this trick and can’t get the site back. It made the website non-accessible, but after both deleting the line and adding a # before the IP address, I still can’t access the site work.

    Comment By rob on January 17th, at 12:30 am

  4. 4

    Sorry about the earlier post. This worked well, but in my situation I had to reboot to make the changes stick. Word to the wise.

    Comment By rob on January 17th, at 12:55 am