How to disable / enable ping response in Linux
Note: all the following steps will be performed in the terminal / command line.
1 Disable / enable persistent ping response (via sysctl)
1.1 Add the following line to “/etc/sysctl.conf” (to enable, change 1 to 0)
net.ipv4.icmp_echo_ignore_all = 1
We can use the following command to achieve this
sudo bash -c 'echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf
1.2 Apply change
sudo sysctl -p
2 Temporarily disable / enable ping response (via sysctl)
2.1 Run the following command to temporarily disable ping response (to enable, change 1 to 0)
echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all
2.2 Apply change
sudo sysctl -p
3 Temporarily disable ping response (through firewall / iptables)
3.1 Run the following command to block / discard ping traffic
iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP
Note. While iptables rules will be in effect after reboot on CentOS and RHEL, they will not be in effect after reboot on Debian / Ubuntu / Kali Linux, etc.
4 Temporarily enable ping response (through firewall / iptables)
4.1 Run the following command to enable / allow ping traffic
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
Note. As in section 3, firewall / iptables rules will be in effect after reboot in CentOS and RHEL, they will not be in effect after reboot in Debian / Ubuntu / Kali Linux, etc.
Bonus
Another way to easily allow the ping response is to just temporarily disable iptables / firewall.
sudo systemctl stop iptables
or
sudo systemctl stop ufw