15

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 -

127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

However, when I ping 127.0.0.1 it is successful -

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.052 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.066 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.054 ms
^C
--- 127.0.0.1 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 @8.8.8.8 localhost" only to find that there was no "answer" section at all.

localhost:~ $ dig @8.8.8.8 localhost

; <<>> DiG 9.8.3-P1 <<>> @8.8.8.8 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

;; QUESTION SECTION:
;localhost.         IN  A

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

;; Query time: 20 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; 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
8
  • 1
    localhost is a special host name. It's never resolved by a dns server. The only localhost the dns server @ 8.8.8.8 knows of is the server itself! The server @ 8.8.8.8 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
    Commented Aug 18, 2016 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
    Commented Aug 18, 2016 at 23:21
  • You may delete the gist and your comment. The hosts file seems fine.
    – klanomath
    Commented Aug 19, 2016 at 1:19
  • What kind of "antivir web root secure anywhere"-ware did you install?
    – klanomath
    Commented Aug 19, 2016 at 1:31
  • @klanomath, That's a good question. I just tried pinging localhost and 127.0.0.1, neither worked. I had my Firewall hardened in Stealth Mode. So I modified some settings and I could ping both, then reenabled Stealth Mode. :) Commented Aug 19, 2016 at 1:35

3 Answers 3

11

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.
##
127.0.0.1   localhost
255.255.255.255 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.

4

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

0

Anyone to the rescue? dig and nslookup work fine and return ip when I query hostname, but ping, ssh and others are able to connect only through ip. When I try using hostname always get "Unknown host"

luciano@MacBook-Air-de-Luciano ~ % dig srv1.grch.home

; <<>> DiG 9.10.6 <<>> srv1.grch.home ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31298 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ;; QUESTION SECTION: ;srv1.grch.home. IN A

;; ANSWER SECTION: srv1.grch.home. 0 IN A 192.168.31.254

;; Query time: 15 msec ;; SERVER: 192.168.31.233#53(192.168.31.233) ;; WHEN: Sun Jun 25 16:50:51 -03 2023 ;; MSG SIZE rcvd: 59

luciano@MacBook-Air-de-Luciano ~ % nslookup srv1.grch.home Server: 192.168.31.233 Address: 192.168.31.233#53

Name: srv1.grch.home Address: 192.168.31.254

luciano@MacBook-Air-de-Luciano ~ % ping -c 4 srv.grch.home ping: cannot resolve srv.grch.home: Unknown host

luciano@MacBook-Air-de-Luciano ~ % ping -c 4 192.168.31.254 PING 192.168.31.254 (192.168.31.254): 56 data bytes 64 bytes from 192.168.31.254: icmp_seq=0 ttl=64 time=14.095 ms 64 bytes from 192.168.31.254: icmp_seq=1 ttl=64 time=12.171 ms 64 bytes from 192.168.31.254: icmp_seq=2 ttl=64 time=13.771 ms 64 bytes from 192.168.31.254: icmp_seq=3 ttl=64 time=7.437 ms

--- 192.168.31.254 ping statistics --- 4 packets transmitted, 4 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 7.437/11.868/14.095/2.660 ms luciano@MacBook-Air-de-Luciano ~ %

3
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Thinkr
    Commented Jun 25, 2023 at 20:12
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
    – Allan
    Commented Jun 25, 2023 at 20:35
  • Your ping has srv while your nslpokup and dig is srv1 ?
    – Satya
    Commented Jun 26, 2023 at 22:35

You must log in to answer this question.

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