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

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?


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.

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.