23

A common way to "block" a website (eg, to eliminate it as a distraction) is to edit /etc/hosts and point the domain to the loopback address. Eg:

# Stop goofing off
127.0.0.1 youtube.com

On Mavericks, I find that changes like this are seemingly ignored by the OS; after editing the file (with sudo) and saving, the site that should be blocked still loads.

I've tried resetting my DNS cache as follows:

dscacheutil -flushcache
sudo killall -HUP mDNSResponder

But the site still loads.

How can I get OSX Mavericks to notice a change to /etc/hosts?

  • 1
    Could it be that it's the browser DNS cache that is not flushed? What IP does ping youtube.com use? – Leonid Shevtsov Feb 18 '14 at 14:32
  • Side note: if you're using DNS to temporarily block sites, selfcontrolapp.com for OSX works nicely. It modifies the hosts file and seems to flush browser cache automatically. They also mention similar apps for other platforms: github.com/SelfControlApp/selfcontrol/wiki/FAQ – Nathan Long Nov 17 '14 at 14:52
18

According to the the hosts(5) manual page, the /etc/hosts file is used by mDNSResponder. Your attempts are correctly flushing the computer wide cache but you also need to flush the browser's private cache.

After each edit of /etc/hosts reset the mDNSResponder cache using this Apple technical note, OS X: How to reset the DNS cache:

sudo dscacheutil -flushcache

After doing this, reset your browser caches:

  • Safari: use the menu item Safari > Reset Safari.
  • Chrome: use the menu item Chrome > Clear Browsing Data.
  • 1
    This fixed it. Under Chrome's "Clear Browsing Data", there are checkboxes for types of data. "Empty the cache" is the relevant one. – Nathan Long Feb 18 '14 at 15:34
5

Delete your browsers cache and add 127.0.0.1 www.youtube.com to your host file.

youtube.com resolves to www.youtube.com. Your browser is caching that information and redirects you to the www-page.

0

In OS X 10.9 Mavericks, there are problems with trying to list multiple hostnames on the same line of /etc/hosts. I tried all of the other DNS-related tips in this post and the related post to no avail, until I stumbled on the "problems" link above.

I used to have something like this:

127.0.0.1 localhost host1 host2 host3 ...

but I found that resolution of those hosts would either take forever or not work at all. Trying to access the sites via browsers would be generally OK (although sometimes very slow to resolve), but trying to ping from the command line would not work.

Changing it to the following format made everything work fine for me, and things now work great with ping, in the browser, and everywhere else:

127.0.0.1 localhost
127.0.0.1 host1
127.0.0.1 host2
127.0.0.1 host3

More details.

0

On top of Graham's answer, try chrome://net-internals/#dns (for Google Chrome) to see what is cached. Then click Clear Host Cache to wipe it all out.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .