Macos – Hosts file on Mountain Lion stopped working suddenly

hosts-filemacososx-mountain-lion

I have used my hosts file (located in /private/etc/hosts) several months to block distracting websites during the work day. This worked all well until now.
Today it suddenly stopped working.

Some sample lines from the hosts file:

127.0.0.1 facebook.com
127.0.0.1 www.facebook.com

I placed that text in the hosts file by the following steps:

sudo nano /etc/hosts
wrote the lines above, then ^O to write the file, Enter to confirm the filename and ^X to exit the editor.

Between the localhost IP and the domain name I have a tab. The line endings are Unix style (LF), and the weird part is that when I use the ping command it seems to do its job properly:

ping facebook.com
PING facebook.com (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.137 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.122 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.118 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.110 ms
^C
— facebook.com ping statistics —
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.110/0.122/0.137/0.010 ms

But when I try to access facebook.com in Safari or Firefox I am still able to get to the web site. This is also the case for other website that I have blocked in a similar way. I have emptied the cache for both browsers, but this didn't solve the problem.

What can I do to solve this problem?

Update 1:
I'm now checking all the websites I've blocked this way and found out that the behaviour is not consistent across different domains. These are the "time-waster" I'm blocking in /private/etc/hosts:

#Block time-killers
127.0.0.1 9gag.com
127.0.0.1 flabber.nl
127.0.0.1 geenstijl.nl
127.0.0.1 dumpert.nl
127.0.0.1 facebook.com
127.0.0.1 www.9gag.com
127.0.0.1 www.flabber.nl
127.0.0.1 www.geenstijl.nl
127.0.0.1 www.dumpert.nl
127.0.0.1 www.facebook.com
##

All sites from this list ping to 127.0.0.1, however 9gag.com and flabber.nl are unreachable by any browser, but geenstijl.nl, dumpert.nl and facebook.com are reachable.

I have tried restarting, this did not solve the problem. Before this problem I have not changed the system configuration by an update of some sort.

Update 2:
Three hours ago I could access facebook.com through Safari and Firefox, now I can't anymore. geenstijl.nl and dumpert.nl are still accessible though. I haven't changed anything in the past three hours, just used Word and browsed the web with Safari.

Update 3:
Now, four hours after the 2nd update the hosts file works as normal again. In the process off fumbling with the hosts file I removed the non-working entries and re-added them one by one, testing each one after it was added. I have no idea what was happening and can't run wireshark on the traffic anymore as there is no faulty behaviour I can observe.

Update 4:
And the problem is back again. The same sites as in update 1 show the erroneous behaviour.

Update 5:
Everything again works as it should. I'll keep the solutions posted here in mind when I encounter the error again.

Best Answer

DNS resolution in OS X went haywire in the update from Snow Leopard to Lion. After a clean install everything should work properly, but if you have gone the update route, things might be haywire.

Option 1: IPv6 addressing

Many websites and ISP's support IPv6 if IPv4 is unreachable. Put the definitions in the beginning of your /etc/hosts like this:

# Block Facebook IPv4
127.0.0.1   www.facebook.com
127.0.0.1   facebook.com

# Block Facebook IPv6
fe80::1%lo0 www.facebook.com
fe80::1%lo0 facebook.com

Option 2: Use DNSMasq

If the previous advice fails, you can install DNSMasq.

Related Question