I am trying to resolve localhost which right now is "not found"

if I ping localhost - I am getting

ping: cannot resolve localhost: Unknown host

I have made sure that the /etc/hosts file has the below entries that are necessary to have the resolution for localhost -   localhost broadcasthost
::1             localhost

However, when I ping it is successful -

PING ( 56 data bytes
64 bytes from icmp_seq=0 ttl=64 time=0.052 ms
64 bytes from icmp_seq=1 ttl=64 time=0.066 ms
64 bytes from icmp_seq=2 ttl=64 time=0.054 ms
--- ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.052/0.057/0.066/0.006 ms

In doubts of this getting blocked by my antivirus - I have also disabled my "web root secure anywhere" anti-virus... and it did not solve anything.

Also, I have noticed that when I restarted my machine - for the first few mins the ping localhost worked and stopped working after few mins. not sure how to track what stopped it from working.

Based on the answer at DNS not resolving on Mac OS X - tried "dig @ localhost" only to find that there was no "answer" section at all.

localhost:~ $ dig @ localhost

; <<>> DiG 9.8.3-P1 <<>> @ localhost
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 3535
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;localhost.         IN  A

.           805 IN  SOA a.root-servers.net. nstld.verisign-grs.com. 2016081801 1800 900 604800 86400

;; Query time: 20 msec
;; WHEN: Thu Aug 18 17:15:07 2016
;; MSG SIZE  rcvd: 102

As requested, the output for "ls -le@ /etc/hosts" -

$ ls -le@ /etc/hosts
-rw-r--r--  1 root  wheel  6126 Aug 18 17:09 /etc/hosts
  • 1
    localhost is a special host name. It's never resolved by a dns server. The only localhost the dns server @ knows of is the server itself! The server @ doesn't even know what the IP of google is because google is no proper host name. Check dns resolution by using a proper fqdn. Please add the output of ls -le@ /etc/hosts. – klanomath Aug 18 '16 at 23:01
  • 1
    Your hosts file has a size of >6kb. That's the ~ thirtyfold of the default size. Probably it contains (a) bogus line(s). Please either post the complete content of the hosts file (to pastebin) or back up your current file, reinstate the default one and try again to ping localhost. – klanomath Aug 18 '16 at 23:21
  • You may delete the gist and your comment. The hosts file seems fine. – klanomath Aug 19 '16 at 1:19
  • What kind of "antivir web root secure anywhere"-ware did you install? – klanomath Aug 19 '16 at 1:31
  • @klanomath, That's a good question. I just tried pinging localhost and, neither worked. I had my Firewall hardened in Stealth Mode. So I modified some settings and I could ping both, then reenabled Stealth Mode. :) – user3439894 Aug 19 '16 at 1:35

I have solved the problem by restoring my Hosts file (it was empty):

But before, you have to check your hosts file.

cat /etc/hosts

If nothing is shown, that means your hosts file is empty.

Then just copy and paste this over the existing hosts file then save it as plain text to restore it.

# Host Database
# localhost is used to configure the loopback interface 
# when the system is booting. Do not change this entry.
##   localhost broadcasthost
::1 localhost
fe80::1%lo0 localhost

If your hosts file is not empty, your can try to delete it and add it again with this lines.


After living with this for a long time, I figured out (by checking from the Sublime editor) that the issue with my /etc/hosts file was that the "Line Endings" on the file were set to "Mac OS". When I changed the line endings to "Unix", I am able to ping localhost in the terminal and stuff's working as expected.

enter image description here

You must log in to answer this question.

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