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

by Klaus on October 25, 2011

in Guides/How-To, Mac OS X

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 :)

Related articles you might find interesting:

Comments & Leave a Comment

comments

{ 22 comments… read them below or add one }

Rienk October 25, 2011 at 21:53

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

Reply

Rob Aldred December 15, 2012 at 17:31

Mine too, probably my mac book was originally leopard, Apple must have forgotten to update this during an upgrade.

Reply

Veronica Cervera October 26, 2011 at 05:27

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

Morgan Leek October 26, 2011 at 08:41

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

Reply

Marty October 27, 2011 at 13:44

Thanks for this fix, these small problems can always be a huge headache
Marty recently posted… 10+ Best Security & Anti-Virus Apps for Android

Reply

Kate Brown Wilson November 1, 2011 at 14:06

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

Alan Smith November 4, 2011 at 07:45

It is a handy features of Mac which gives little bit comfort to me.

Reply

Chay November 15, 2011 at 23:44

Installed textwrangler and tried

sudo edit /etc/hosts

got an error “bash edit command not found” any ideas?

Reply

Chay November 17, 2011 at 22:32

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

Markus Mueller March 26, 2012 at 21:11

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

Robert November 29, 2011 at 23:12

Thanks a lot !! it was the only solution the actually worked. !

Reply

Pekka December 1, 2011 at 20:53

Interestingly ssh, and ping work, but nslookup still resolves to real addresses (please try)

Reply

Andrew December 7, 2011 at 11:42

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

Jan Schutte March 11, 2012 at 22:12

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

Martin Stabenfeldt March 17, 2012 at 14:11

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

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

Reply

Martin Stabenfeldt March 31, 2012 at 21:24

Gikk visst galt i cut´n paste-operasjonen.

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

Reply

Martin Stabenfeldt March 31, 2012 at 21:25

I all verden, hvorfor blir sed, altå S E D byttet ut med tørste?

Reply

Marco March 24, 2012 at 13:23

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

Kimi March 26, 2012 at 09:23

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.
Kimi recently posted… Apple iPad MD328LL/A iPad 3 16GB White

Reply

abdimuna April 5, 2012 at 23:05

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

Jeroen April 9, 2012 at 19:24

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

Claudio September 22, 2012 at 09:10

[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 http://www.facebook.com
::1 http://www.facebook.com

Reply

 

Leave a Comment

CommentLuv badge

{ 4 trackbacks }

Previous post:

Next post: