Apple Intelligence is now available on iPhone, iPad, and Mac!

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Cannot resolve localhost

I have a problem on two macbook pros where I cannot resolve localhost:


MattBookPro:~ matt$ ping localhost
ping: cannot resolve localhost: Unknown host
MattBookPro:~ matt$ ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.114 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.143 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.136 ms
^C
--- 127.0.0.1 ping statistics ---


On Linux, there's a nsswitch config file which is where one would look for answers. Doesn't appear to be one on mac.

Any ideas?

MacBook Pro (2010 i7 2.6GHz), Mac OS X (10.6.4), 4GB RAM

Posted on Oct 14, 2010 5:32 AM

Reply
11 replies

Oct 14, 2010 1:19 PM in response to MrHoffman

/etc/hosts is the default:


MattBookPro:etc matt$ 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
MattBookPro:etc matt$ dig localhost
; <<>> DiG 9.6.0-APPLE-P2 <<>> localhost
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51268
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;localhost. IN A
;; ANSWER SECTION:
localhost. 604800 IN A 127.0.0.1
;; AUTHORITY SECTION:
localhost. 604800 IN NS localhost.
;; ADDITIONAL SECTION:
localhost. 604800 IN AAAA ::1
;; Query time: 63 msec
;; SERVER: 192.231.203.132#53(192.231.203.132)
;; WHEN: Fri Oct 15 06:14:25 2010
;; MSG SIZE rcvd: 85
MattBookPro:etc matt$ ping localhost
ping: cannot resolve localhost: Unknown host
MattBookPro:etc matt$ 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.105 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.163 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.092 ms
^C
--- localhost ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.092/0.120/0.163/0.031 ms
MattBookPro:etc matt$



Interesting that appending the . makes it work. Indicates that it's trying to look for "localhost" in some other place and ignoring the /etc/hosts file doesn't it?

Message was edited by: Matthew Connolly3 - formatting

Oct 14, 2010 4:03 PM in response to MrHoffman

I've learnt a few things, but still not got to the bottom of it. This article on afp548 site: http://www.afp548.com/article.php?story=20100329090657793 has some useful info.

In particular, that SL does all name resolving through mDNSResponder. If I enable logging, via:

<pre>Matts-Mac-Pro:~ matt.connolly$ sudo killall -INFO mDNSResponder
</pre>

I can see that mDNSresponder is definitely looking for "localhost" in the domain "home".

I've also just checked on a mac at work:
* ping localhost works (resolves to 127.0.0.1)
* ping localhost prints nothing in the mdnsresponder log.

And here's the `dig localhost` results from the working mac:

$ dig localhost
; <<>> DiG 9.6.0-APPLE-P2 <<>> localhost
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40691
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;localhost. IN A
;; ANSWER SECTION:
localhost. 600 IN A 127.0.0.1
;; Query time: 198 msec
;; SERVER: 10.3.4.10#53(10.3.4.10)
;; WHEN: Fri Oct 15 09:00:03 2010
;; MSG SIZE rcvd: 43
</tt>
Both the work mac and my laptop have exactly the same /etc/hosts file (md5 matches).
So I'm deducing from this that the correctly working machine reads "localhost" from /etc/hosts and my laptop does not. Why? Where would this setting be?
Message was edited by: Matthew Connolly3 -added dig results

Oct 14, 2010 7:23 PM in response to MrHoffman

With respect, mDNS IS involved because in the systems that cannot resolve "localhost", mDNSResponder is clearly trying to resolve "localhost" in the domain "home" which can be seen by enabling debug logging in mDNSResponder as mentioned above. Here's an example of the debug log seen in the Console:

<pre>
15/10/10 6:41:20 AM mDNSResponder[18] 30: Adding FD for uid 501
15/10/10 6:41:20 AM mDNSResponder[18] 30: DNSServiceCreateConnection START
15/10/10 6:41:20 AM mDNSResponder[18] 30: Error socket 41 created 00000000 00000001
15/10/10 6:41:20 AM mDNSResponder[18] 30: DNSServiceQueryRecord(localhost.home., Addr, 5000) START
15/10/10 6:41:20 AM mDNSResponder[18] 30: Error socket 41 closed 00000000 00000001 (0)
15/10/10 6:41:20 AM mDNSResponder[18] 30: DNSServiceQueryRecord(localhost.home., Addr) ADD 0 localhost.home. Addr
15/10/10 6:41:20 AM mDNSResponder[18] 30: Cancel 00000000 00000001
15/10/10 6:41:20 AM mDNSResponder[18] 30: DNSServiceQueryRecord(localhost.home., Addr) STOP
15/10/10 6:41:20 AM mDNSResponder[18] 30: Removing FD
</pre>

If the information in the link above on the afp548 site is correct, then ALL dns querries are handled by mDNSResponder for ALL parts of the system EXCEPT tools like "dig" and "nslookup" which communicate directly with the name lookup services (instead of using common system functionality).

Therefore, I'm not actually convinced that "dig" is telling us anything useful as to why mDNSResponder is trying to lookup "localhost" in the first place (and isn't trying on the systems that can resolve "localhost".

I believe that this is where the problem lies, but don't know how to solve it yet.

When I'm home this evening I'll change the DNS settings as you said and report back.

Oct 16, 2010 12:00 AM in response to etresoft

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


Adding an entry into the plist file like so, fixes the problem.


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


It's got nothing to do with the DNS config. 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.

Oct 16, 2010 5:50 AM in response to Matthew Connolly3

Odd.

Good to hear that this is working.

This stuff should be automatic, so this definitely looks broken.

Have there been direct changes made to /etc/hosts?

Here's the usual query for checking the cache:


$ dscacheutil -q host -a ip_address 127.0.0.1


I have none of the entries added here in any of the local (and working) configurations.

Cannot resolve localhost

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.