2

After migrating to a new MacBook Pro I can no longer ping localhost.

I already tried to change my /etc/hosts file although it was already right before.

$ host localhost
localhost has address 127.0.0.1
localhost has IPv6 address ::1
$ 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.098 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.081 ms

Pinging localhost. works somehow, can't understand why.

If I restart the system it wont resolve localhost again, but if I ping localhost. then ping localhost starts working. Dunno if this can help anyone with the same problem

What could be wrong?

$ ping localhost
ping: cannot resolve localhost: Unknown host
$ 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.072 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.084 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.077 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.073 ms
^C
--- localhost ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.072/0.076/0.084/0.005 ms
$ 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.070 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.072 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.088 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.082 ms
^C
--- localhost ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss

more results

MBPdeFrancisco:~ francisco$ dscacheutil -q host -a name localhost
name: localhost
ipv6_address: fe80:1::1
MBPdeFrancisco:~ francisco$ LC_ALL=C cat -vet /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 localhostM-bM-^@M-($
255.255.255.255 broadcasthostM-bM-^@M-($
::1 localhostM-bM-^@M-($
fe80::1%lo0 localhost

MBPdeFrancisco:~ francisco$ cat /etc/resolv.conf
#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
domain lan
nameserver 2001:8a0:ddce:7401:9e97:26ff:fedb:6214
nameserver 192.168.1.254
  • what is the output of the "ping localhost" command, please add that to your question. Also under System Preferences > Security & Privacy > Firewall > Firewall options. Is stealth mode selected? – Steve Chambers May 25 '18 at 12:23
  • its working now, i changed absolulety nothing, cant understand – Luce Sky Walker May 25 '18 at 12:25
  • i restarted the system, and localhost was not working again. but if i ping localhost. then ping localhost starts working – Luce Sky Walker May 25 '18 at 12:25
  • None of this makes sense. localhost is mapped to the lo0 interface (127.0.0.1) which is a loopback interface. It can't not work. – Allan May 25 '18 at 16:56
  • 1
    @LuceSkyWalker When it's failing, try running dscacheutil -q host -a name localhost and then add the output (in code format) to the question. Also, what's the output of LC_ALL=C cat -vet /etc/hosts? – Gordon Davisson May 26 '18 at 2:53
1

Your /etc/hosts file is corrupt; for some reason it has unicode LINE SEPARATOR characters added to several lines (the "M-bM-^@M-(" thing in LC_ALL=C cat -vet's output), including one of those for localhost. macOS's resolver will treat that weird character as part of the hostname, and so if you somehow manage to ping localhost<LINE SEPARATOR>, it'll resolve to 127.0.0.1 just fine. Plain localhost? Not so much.

I don't know how those weird characters would've gotten added; did you try to edit the file with some sophisticated editor that thought it would be a good idea to use the very latest trendy formatting characters, rather than just sticking with what'll work? If so, don't use that editor for unix-style config files (or scripts, or...). I recommend BBEdit instead; even if you don't buy it, it'll let you do the basic stuff in its free demo mode.

As for how to fix it... Well, first make a backup copy in case this goes sideways and messes things up even more than they are now. Then run the command:

sudo perl -pi -e 's/[^[:ascii:]]//g' /etc/hosts

That should purge all the weird unicode characters out of the file. Then try the dscacheutil command again; you should get something like this:

$ dscacheutil -q host -a name localhost
name: localhost
ipv6_address: ::1
ipv6_address: fe80:1::1

name: localhost
ip_address: 127.0.0.1

P.s. for a explanation why someone thought LINE SEPARATOR was a good idea, see the ever-relevant XKCD and Jeff Atwood's rant about "The Great Newline Schism".

You must log in to answer this question.

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