Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a server running OS X Server 10.5 and it can't resolve localhost to 127.0.0.1.

When I try ping this is what happens:

> ping localhost
ping: cannot resolve localhost: Unknown host

SSH and web browsers get similar results (uknown host). If I try using 127.0.0.1 or the ip address assigned on the LAN all of the above work.

Here's the contents of my /etc/hosts file:

> cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0     localhost

I have no local DNS service running.

Does anyone have any idea why this might be happening or how I can fix it?

share|improve this question
1  
Rename /etc/hosts to /etc/hosts.justworks –  ta.speot.is May 19 '10 at 3:18
    
What are your DNS settings on your server box? Are you running a DNS service? –  Shaggy Frog Dec 10 '10 at 0:59
add comment

7 Answers

I had this same issue with Mountain Lion (10.8.2). I solved it by by disabling "Stealth Mode" in Security & Privacy -> Firewall -> Firewall Options.

share|improve this answer
add comment

This is probably not related to your original question, but there is another issue that can affect DNS lookups in Mac OS X Lion, specifically localhost or any other entries you may have in /etc/hosts.

Apparently the /etc/hosts file no longer supports multiple domains per line in the file, so every record needs its own line:

127.0.0.1 localhost
127.0.0.1 some.site.local
127.0.0.1 some.site.dev

Found this via Justin Carmony's blog. He goes on to describe some other "interesting" behavior if you're using /etc/hosts, so read up if you're still having issues.

share|improve this answer
add comment

I know it's a little late, but in my case, changing the line endings to UNIX, replacing tabs with spaces and saving the file as UTF-8 made it work on Mountain Lion (10.8.2)

Hope this work for someone else.

share|improve this answer
add comment

I have the same problem. I just set up a new mac laptop and it was working until I migrated from my old one. Now ping localhost cannot resolve. Must be some setting I've changes somewhere, but I don't know where...

I've discovered that "ping localhost." works. Appending a trailing dot is meant to bypass searching for the name "localhost" in other search domains. That's cool, but I still don't understand why the /etc/hosts entry isn't working...

edit: Solved.

bash-3.2# dscl . read /Hosts/localhost    
AppleMetaNodeLocation: /Local/Default
RecordName: localhost
RecordType: dsRecTypeStandard:Hosts
bash-3.2# ping localhost
ping: cannot resolve localhost: Unknown host
bash-3.2# dscl . append /Hosts/localhost IPAddress 127.0.0.1
bash-3.2# dscl . read /Hosts/localhost
AppleMetaNodeLocation: /Local/Default
IPAddress: 127.0.0.1
RecordName: localhost
RecordType: dsRecTypeStandard:Hosts
bash-3.2# ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.083 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.140 ms

voila!

Somehow, the directory services record was out of sync with the /etc/hosts file. And when there is no IP address in the directory services configuration, trying to resolve the name then goes to mDNSResponder which asks DNS.

share|improve this answer
add comment
up vote 0 down vote accepted

In the end it just started working again. I found some unorthodox (and unneccessary) DNS configuration on the server and changed that to what it should have been, and perhaps that solved the problem? I can't be sure though because I wasn't trying to resolve the localhost issue at the time and so I didn't test localhost before and after.

share|improve this answer
add comment

Encoding can be a problem, I edited /etc/hosts using Sublime Text which gets me into the problem reediting it using vi fixed the problem. make sure that cat outputs the contents.

share|improve this answer
add comment

Remove your hosts file and see what happens then. Also reboot without connecting to any networks.

share|improve this answer
    
I'm weary of deleting /etc/hosts because it's exactly the same as the /etc/hosts files on my other macs (and those can resolve localhost). Also, this is our production server... I've tried rebooting without connecting to any network but it still could not resolve localhost. –  iainbeeston May 20 '10 at 23:34
    
On a different mac (that doesn't have this problem) I just tried renaming /etc/hosts to something else and rebooted. It didn't make any visible difference, but os x didn't replace /etc/hosts with a new copy. So again, I'm weary to delete the file unless the OS is going to regenerate it. –  iainbeeston May 20 '10 at 23:43
    
If this is a production server then you should have recent backups. Restore from a backup. –  Josh K May 21 '10 at 2:48
    
I'm afraid it's not a recent problem - it's been like that for over a year (for as long as I've been in the job). So restoring from backup would be difficult. In any case I was hoping to find the cause of the problem, not just blat everything in the hope that it starts working again! –  iainbeeston May 24 '10 at 0:05
    
You're not "blat"'ing everything, it's standard procedure, especially with servers. If you notice an issue and can't find the source the easiest and most commonly used method of resolving the issue would be to restore from a backup. The first thing I would have done on the job would have been to fix it. Letting problems fester doesn't make them go away. –  Josh K May 24 '10 at 1:04
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.