Tool of the Week: Blocking distracting websites using the hosts file

Every once in a while i decide that I need to block a distracting website from my computer in order to remain productive. An example might be me compulsively checking Facebook  or google reader throughout the work day instead of staying on task and focused.  The key here is I need a simple barrier between me and the distraction to remind me I should be on task.

There are many apps and programs on the internet to block websites, but I've found the simplest way both on mac and PCs is to edit the hosts file.  The way the internet works is this: When you type in a web address in human recognizable form (e.g., www.facebook.com) the computer then must look up an address in computer recognizable form (e.g., 31.13.74.7) to find the appropriate website.  Your computer has a local hosts file that over-rides the address lookup normally provided by a DNS server.  If you wish to block a website, you can set an override to a local IP address that will not allow the page to load.

On a mac, start by opening the terminal.  Type in the command:

sudo nano /private/etc/hosts

The terminal will then prompt you for your computer password (note, no characters will appear as you type, but enter your password and press enter).
Screen Shot 2012-12-30 at 3.02.19 PMThe file in a simple editor should appear and look something like this.  Now you can edit your hosts file.  Using the arrow keys, navigate to below the last line of text and enter in additional websites you wish to block, using the localhost IP address as the ip address:

127.0.0.1             facebook.com
127.0.0.1             www.facebook.com
127.0.0.1             www.addictinggames.com
127.0.0.1             addictinggames.com

Next click control-o to save and hit enter, followed by control-x to exit.  You have now edited your hosts file!  The last step for the changes to take effect is to clear your catch (All the addresses that your computer remembers because you've visited them).  Use the command:

dscacheutil -flushcache

and you should be good to go!

One last note is that editing the hosts file can be useful in reverse also.  If your office has blocked websites like facebook, they may have done so using a DNS trick. Editing the hosts file with the correct IP address for a site like facebook will restore your ability to use facebook at the office (However you still may get reprimanded for violating company policy).  To determine the IP address for a website, using a computer at an unblocked location, ping the website using the terminal:

ping www.nytimes.com

using control-c to stop the ping. The result should look something like:
Screen Shot 2012-12-30 at 3.14.02 PMThen, if I wish to get around the block at work, I could add the entry:

178.149.168.130             www.nytimes.com

to my hosts file at work.

A final note: To temporarily unblock a website, you can use the # key to make a line invisible to the computer:

#127.0.0.1             facebook.com
#127.0.0.1             www.facebook.com
127.0.0.1             www.addictinggames.com
127.0.0.1             addictinggames.com