11

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?

2
  • 2
    Rename /etc/hosts to /etc/hosts.justworks May 19, 2010 at 3:18
  • What are your DNS settings on your server box? Are you running a DNS service? Dec 10, 2010 at 0:59

10 Answers 10

6

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.

1
  • Also fixed issue with El Capitan 10.11
    – suspectus
    Dec 21, 2015 at 14:08
3

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.

2

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.

1

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.

0
1

On my computer (same symptoms) my /etc/hosts file had been munged to use Macintosh line endings (CR—0x13) instead of Unix line endings (LF—0x10). The file was otherwise correct.

My solution was:

brew install dos2unix
sudo dos2unix -c mac /etc/hosts
0

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.

0

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.

0

I edited my /etc/hosts with Sublime Text and had the same issue, try to edit and save your /etc/hosts file with vi or nano, that fixed it for me ...

0

Ever make sure to read the Apache error log, because not all errors are thrown in the console when starting the service. There may be problems, even if the syntax is ok.

By default they should be stored in /var/log/apache2/ and the log file is error.log.

I had exactly the same problem, that the host could be resolved and apache did not tell any issue in the console. The simple "problem" was, that the error.log defined my vhost-configuration could not be created after swapping an external drive.

-1

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

6
  • 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. May 20, 2010 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. May 20, 2010 at 23:43
  • If this is a production server then you should have recent backups. Restore from a backup.
    – Josh K
    May 21, 2010 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! May 24, 2010 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, 2010 at 1:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .