MacOS – ‘localhost’ stoped working as an alias for ‘127.0.0.1’ for SSHing into the VirtualBox VM. Why, and how to fix

macosNetworkterminalvirtualbox

The question explains the problem. For months I've never had any problems along these lines. Then, yesterday morning, I couldn't get into my VM! Took a long time to troubleshoot but here was the solution:

Brendans-Mac-mini:~ Brendan$ ssh -p 3022 user@localhost
ssh: Could not resolve hostname localhost: nodename nor servname provided, or not known
Brendans-Mac-mini:~ Brendan$ ssh -p 3022 user@127.0.0.1
user@127.0.0.1's password: 
Last login: Tue Jul  1 22:37:59 2014 from 10.0.2.2

So, why would this have happened, and more importantly, how do I fix it?

I tried dig localhost, which seems to tell me that there's still some kind of connection between localhost and 127.0.0.1:

Brendans-Mac-mini:~ Brendan$ dig localhost

; <<>> DiG 9.8.3-P1 <<>> localhost
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28989
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;localhost.         IN  A

;; ANSWER SECTION:
localhost.      655360  IN  A   127.0.0.1

;; Query time: 7 msec
;; SERVER: 10.0.1.1#53(10.0.1.1)
;; WHEN: Wed Jul  2 09:44:44 2014
;; MSG SIZE  rcvd: 43

Best Answer

[This is copied & slightly expanded from my answer on Superuser.]

localhost is normally defined in the /etc/hosts file, not in DNS (although your DNS has an emergency backup entry for it). Here's the normal contents of /etc/hosts in OS X (as of Mavericks):

##
# 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

Note that if you need to edit/recreate this file, it must be in plain text format (no formatting at all), with unix-style line endings (linefeeds). You can use a command line editor like vi or nano; if you want a GUI editor, I tend to recommend TextWrangler or its big brother BBEdit for editing files like this, since they're good about not adding spurious "helpful" things to the files you edit, and also can use admin rights to edit system files (like /etc/hosts).

(Also, it's not really visible or important, but those are tab characters between the addresses and names.)

For testing lookups like this, use dscacheutil (dscacheutil -q host -a name localhost) instead of something like dig. The reason to use dscacheutil is that dig (and host and nsloogup) bypasses the system lookup policy and just checks DNS directly, while dscacheutil uses the full system lookup policy. As a result, dig won't see entries in /etc/hosts, or do mDNS (Bonjour) lookups, or look for Computer entries in directory services, or see cached entries from previous lookups, or...

When you have the hosts file set up properly, here's what dscacheutil's output should look like:

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

name: localhost
ip_address: 127.0.0.1