I have an issue with the localhost alias. It doesn't work. Basically, no program can resolve localhost to The alias is in /etc/hosts:

[root@gabriel-notebook gpoesia]# cat /etc/hosts 
# /etc/hosts: static lookup table for host names
#<ip-address>   <hostname.domain.org>   <hostname>       localhost.localdomain   localhost       gabriel-notebook
::1             localhost ip6-localhost ip6-loopback

# End of file

Also, myhostname is enabled in /etc/nsswitch.conf:

[root@gabriel-notebook gpoesia]# cat /etc/nsswitch.conf 
# Begin /etc/nsswitch.conf

passwd: compat mymachines systemd
group: compat mymachines systemd
shadow: compat

publickey: files

hosts: files mymachines resolve [!UNAVAIL=return] dns myhostname
networks: files

protocols: files
services: files
ethers: files
rpc: files

netgroup: files

# End /etc/nsswitch.conf

And localhost shows up in getent hosts. Also, /etc/hostname is set.

[root@gabriel-notebook gpoesia]# getent hosts       localhost.localdomain localhost gabriel-notebook       localhost ip6-localhost ip6-loopback
[root@gabriel-notebook gpoesia]# getent hosts gabriel-notebook       localhost.localdomain localhost gabriel-notebook
[root@gabriel-notebook gpoesia]# cat /etc/hostname 
[root@gabriel-notebook gpoesia]# hostname

However, ping localhost doesn't work.

[root@gabriel-notebook gpoesia]# ping localhost
ping: localhost: Name or service not known

The main difficulty is I don't know exactly when this started to happen. I remember it working in the past, but I can't tell what caused it to break. Googling around didn't help. I've found people with the same issue caused by reasons that don't hold for me (such as corrupted files, wrong syntax in /etc/hostname, etc).

Any ideas?


  • Output of ls -l /etc/hosts /etc/nsswitch.conf and ip addr show please. (In your question, naturally.) – roaima Jul 1 '17 at 23:02

Firstly I assume that ping and ping ::1 both work fine (or at least one of them works).

Next I would try ping gabriel-notebook and ping ip6-localhost just to rule out the low hanging fruit.

There are a couple nitpicks that could be made with your configuration but nothing that would explain this. For example many argue against putting the name of the local machine as an alias of localhost, but this looks to be a laptop so you probably want it. And the hosts line in your nsswich.conf file looks like the myhostname module will not work right but that is not the issue you are fighting.

The only thing that looks fishy is that getent shows being mapped to both the expected ip4 aliases but also the ip6 aliases. this makes me suspect funky white space in the hosts or nsswitch.conf file (can be checked with hd) or that the mymachines module is doing something funky. I would check that with getent hosts localhost.

Given how everything else looks ok, It is time to reach for the tinfoil beanie, and ask If this is a root kit, and a well made one if you didn't notice it until now, why would they screw around with name resolution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.