Troubleshooting scenarios when ping localhost fails
Troubleshooting scenarios when ping localhost fails
In this article, we will explore different scenarios where and all the ‘ping localhost’ fails
Scene 1: invalid entries in /etc/hosts
To explain this scenario, I voluntarily made incorrect entry in the ‘/etc/hosts’ file as below,
[root@ centos7-test-01 ~]# cat /etc/hosts
127.0.0.1 localhost_error
[root@ centos7-test-01 ~]#
And I have modified ‘/etc/nsswitch.conf’ file for ‘hosts’ entry as below
[root@ centos7-test-01 ~]# cat /etc/nsswitch.conf | grep -i hosts | grep -v ^#
hosts: files
[root@ centos7-test-01 ~]#
Now we are getting “Name or service not known” error message when we ‘ping localhost’ as below,
[root@ centos7-test-01 ~]# ping localhost
ping: localhost: Name or service not known
[root@ centos7-test-01 ~]#
Scene 2: net.ipv4.icmp_echo_ignore_all = 1 in /etc/sysctl.conf
When we set the value ‘net.ipv4.icmp_echo_ignore_all’ to 1 in sysctl, server will disable all ping replies.
Current value:
[root@centos7-test-01 ~]# sysctl -a | grep -i icmp_echo_ignore_all
net.ipv4.icmp_echo_ignore_all = 0
[root@centos7-test-01 ~]#
To set it to 1, [root@centos7-test-01 sysctl.d]# vi /etc/sysctl.conf
To make it persistent and effect, execute ‘sysctl -p’
[root@centos7-test-01 sysctl.d]# sysctl -p
net.ipv4.icmp_echo_ignore_all = 1
[root@centos7-test-01 sysctl.d]# sysctl -a | grep -i icmp_echo_ignore_all
net.ipv4.icmp_echo_ignore_all = 1
[root@centos7-test-01 sysctl.d]#
Now, we will try ping localhost and see the output,
[root@centos7-test-01 sysctl.d]# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
^C
— localhost ping statistics —
6 packets transmitted, 0 received, 100% packet loss, time 4999ms
[root@centos7-test-01 sysctl.d]#
Scene 3: Checking iptables firewall rules
With below, iptables rules, we can block icmp traffic
[root@centos7-test-01 ~]# iptables -A INPUT –proto icmp -j DROP
[root@centos7-test-01 ~]#
[root@centos7-test-01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP icmp — anywhere anywhere
Now we can test ‘ping localhost’
[root@centos7-test-01 ~]# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
^C
— localhost ping statistics —
4 packets transmitted, 0 received, 100% packet loss, time 3000ms
[root@centos7-test-01 ~]#