Can’t ping localhost in Linux

By | 2012/06/09

$ ping localhost
ping: unknown host localhost

If you ever find yourself in a situation where you cannot ping localhost, I’ll show you a few things to check!


/etc/hosts

Verify that the /etc/hosts file has entires for localhost which looks like the following:

127.0.0.1 localhost

::1     localhost ip6-localhost ip6-loopback

/etc/nsswitch.conf

The file /etc/nsswitch.conf requires specific permissions. Verify that it is set to 644.

$ sudo chmod 644 /etc/nsswitch.conf

Also verify that /etc/nsswitch.conf has an entry like the following:

hosts:      files dns

Still no luck?

– Temporarily disable iptables, ufw, or any other firewall that may be in place
– Verify you have proper DNS in /etc/resolv.conf
– Restart all network services

– Verify that the output of sudo ifconfig provides something like this:

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:295 errors:0 dropped:0 overruns:0 frame:0
          TX packets:295 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:43479 (42.4 KiB)  TX bytes:43479 (42.4 KiB)

If no local loopback is provided with the above command, inspect the network interface configuration in /etc/network/interfaces (debian/ubuntu) or /etc/sysconfig/network-scripts/ifcfg-lo (red hat/centos).

– Another way to verify the loopback interface is up is with the following command:

$ ip link show lo
1: lo:  mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

Good luck!