Can't resolve localhost on Mac OS X Server

32,500

Solution 1

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.

Solution 2

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.

Solution 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.

Solution 4

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.

Solution 5

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
Share:
32,500

Related videos on Youtube

iainbeeston
Author by

iainbeeston

Updated on September 17, 2022

Comments

  • iainbeeston
    iainbeeston almost 2 years

    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?

    • ta.speot.is
      ta.speot.is about 14 years
      Rename /etc/hosts to /etc/hosts.justworks
    • Shaggy Frog
      Shaggy Frog over 13 years
      What are your DNS settings on your server box? Are you running a DNS service?
  • iainbeeston
    iainbeeston about 14 years
    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
    iainbeeston about 14 years
    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.
  • Josh K
    Josh K about 14 years
    If this is a production server then you should have recent backups. Restore from a backup.
  • iainbeeston
    iainbeeston about 14 years
    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!
  • Josh K
    Josh K about 14 years
    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.
  • iainbeeston
    iainbeeston about 14 years
    That's true if I was a dedicated systems admin but I'm not (I work for an SMB and systems admin is only a peripheral part of my job description). Also, the problem is so minor (it's not affecting any current process on the server - only future changes that we'd like to make) that the disruption and time spent on a system-restore would far outweigh the benefit to the business... Anyway, waaaay too much information, I'm sure it can be fixed by a one-line config change if I can find the right file!
  • suspectus
    suspectus over 8 years
    Also fixed issue with El Capitan 10.11