(Fixed) Mac OS X Lion: /etc/hosts stopped working in 10.7.2, being ignored

Besides running this blog, I’m also a freelance web-developer, so I sometimes have a need to modify my local “hosts” file on my Mac, in order to get development/local domains to point to my local web server and also to apply DNS changes before they have propagated through the entire Internet.

Usually, you’d do something like that by editing the /etc/hosts file on a Mac and then you’d be good to go.

However, I’m not sure when, if it was with Lion (10.7) or the recent 10.7.2 update, lots of people started to have problems with their hosts file, as if it was being ignored. Myself included.

Going into terminal and typing:

scutil --dns

… would indicate that the first resolver was actually the DNS server, and your local resolver came only 2nd, so Apple might have changed the resolve order, so your hosts file is no longer 1st priority.

One workaround suggested on the Internet was to install DNSMasq and run your own local DNS server based on your hosts file. That’s of course a possibility, but the whole installation process sounds complicated to me, and I’d rather avoid running a local DNS server if possible, considering that it should work with just the hosts file, and it used to.

So, after hours of googling around, searching and reading, I came upon this post, which suggests to run this command in Terminal:

hexdump -C /etc/hosts

… to confirm that your /etc/hosts file has Unix style line endings. I had no idea how to interpret the result, but it gave me an idea: To check if I’m actually saving the file with Unix style line endings or not.

Back into Terminal, I wrote the following:

sudo edit /etc/hosts

Which launched TextWrangler (free text editor for Mac, get if you don’t already have it), and I saw that it had selected “Windows (CRLF)” line endings. I simply changed it to “Unix (LF)”, as you can see on the screenshot below which shows a standard /etc/hosts file. I saved the file – and that’s it, the host files now works again!!

I also tried, in Terminal, to write:

ping google.com

… which of course gave me an IP address belonging to Google. Then I added this line to my hosts file:

127.0.0.1 google.com

And I ran the ping command again, and ping would now tell me that google.com points to 127.0.0.1. It works!

And yes, if you’re one of those bad people who didn’t bother paying for Photoshop, I believe this will also fix your “127.0.0.1 activate.adobe.com” stuff. If this helps, you can repay me by purchasing a legal license for your Photoshop 🙂

26 thoughts on “(Fixed) Mac OS X Lion: /etc/hosts stopped working in 10.7.2, being ignored”

  1. Pingback: rienksmids
  2. This solved my issue! Thanks!
    My filetype was “Classic Mac (CR)” changed it to “Unix (LF)” and hosts is no longer being ignored

    Reply
  3. I’m glad that i stumbled into this post. It’s true that fixing this by installing DNSMasq is quite complicated. Changing the host file is much easier and faster. Thank you for sharing and keep up the good work.

    Reply
  4. You’re a superstar! This had me worried this morning. Already gone into panic mode and started thinking of alternative development paths.

    Reply
  5. I really would like to try this operating system, I have to admit this is my first time to know about this MAC OS lion. I think it has great features.

    Reply
  6. Pingback: Hiroki Ishikura
  7. So managed to solve the issue of accessing the hosts file with textwrangler I used:

    Sudo /Applications/Textwrangler.app/Contents/MacOS/Textwrangler/

    I then opened the hosts file from inside the textwrangler app.

    For anyone who does not know in Lion the host file is hidden, use:

    defaults write com.apple.Finder AppleShowAllFiles YES

    Anyway thanks fo the info above – my hosts file is working again – saved me a lot of headaches…

    Reply
    • The Textwrangler does nor start with
      Sudo /Applications/Textwrangler.app/Contents/MacOS/Textwrangler/
      (there always comes “command not found” in the terminal).

      What can I do (Mac OS 10.7.3)?

      And when it will work: where can I find the host-file? Is it possible to find it in the finder? I tried it with “defaults write com.apple.finder AppleShowAllFiles -boolean true;killall Finder”, but I can not find/ see the file in the finder to open it directly in Textwrangler.

      Reply
  8. I am on the latest 10.6, not 10.7. 10.6 is also now preferring the dns over the hosts, but I have no idea what caused the change. These issues fixed part of it, but some hosts were still borked. If a host could be resolved through an external dns, OR my /etc/hosts, it would prefer the dns one (even in browsers.) I have a lot of hosts that are private (10.*, etc) but some of those are both public for clients and have a local private IP.

    What fixed that was adding “local” to my search domains in network settings, so now it looks like “local, mycorp.com”.

    Also, nslookup will query the dns server directly, so it had better bypass the hosts file 🙂 Type ‘nslookup’ and then type ‘server 8.8.4.4’ and then a host name, and you will query google directly. This is as it should be.

    Reply
  9. Thanks for your post.
    The line ends were already in unix style, but it still did not work.

    For me, replacing the spaces between hostname and ip address by tabs did the trick.

    Reply
  10. No need to download anything, use the command line tool sed:

    $ sed ‘s/^M$//’ input.txt > output.txt

    Reply
  11. I tried all the above, but it didn’t work.
    Then, I found this post http://thecoredump.org/2011/09/editing-the-hosts-file-in-mac-os-x-lion/
    Apparently, in Lion, your entries will be ignored unless you put them FIRST in the file.
    O_o Yeah… right…?
    But it did work! ^_^
    Thanks a lot!

    Reply
  12. Hi Klaus,

    I am new to Lion (Mac) because I have used Windows for the last 10 years, so at the beginning I couldn’t even modify host file on Mac LOL

    Your post is very helpful to me, since I am working with many local wordpress sites, and to transfer them I need to edit host file first to have the webhost’s IP.

    I am now learning with the terminal “Sudo” thing, so thanks for this clear guide.

    Kimi.

    Reply
  13. hi, I tried every method, but my problem is neither localhost, nor 127.0.0.1 seems to work , I have the same configurations for /etc/hosts and /etc/resolv.conf, in my iMac which (isOK,) but on my MBP, not working .
    ..
    if somebody has better ideas , please notify .

    Reply
  14. Helaas werken de commandline utilities anders dan applicaties die de API’s van Apple gebruiken. Met andere woorden, je kunt ping e.d. wel in het gareel krijgen, maar je browser zal altijd eerst naar DNS kijken en dan pas naar je hostfile. Een leuk cadeau van Apple in Lion :s

    Reply
  15. Pingback: Igor Wiedler
  16. Pingback: Josh Wayne
  17. [RESOLVED] In Lion OSX every custom entry on /etc/hosts file MUST have an IPv4 and IPv6 line, so you must have TWO line per domain:

    127.0.0.1 www.facebook.com
    ::1 www.facebook.com

    Reply

Leave a Reply to abdimuna Cancel reply