{"id":238,"date":"2020-10-06T02:29:02","date_gmt":"2020-10-06T02:29:02","guid":{"rendered":"https:\/\/linuxways.net\/?p=238"},"modified":"2020-12-08T12:55:05","modified_gmt":"2020-12-08T12:55:05","slug":"how-to-block-unblock-ping-requests-to-ubuntu-server-20-04-lts","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-block-unblock-ping-requests-to-ubuntu-server-20-04-lts\/","title":{"rendered":"How to block or unblock ping requests on Ubuntu Server 20.04 LTS"},"content":{"rendered":"<p>Ping is a network administration utility that is used to test the availability of a system on an IP network. Ping is also used to test the quality of the network connection by monitoring the round trip time and packet losses. On the other hand, network intruders and hackers also use ping to identify network subnets to find potential hosts or to perform ICMP flood attacks. Therefore, it is a good practice to block ping requests to your servers to prevent any kind of attack.<\/p>\n<p>This article is about how to block ping requests to Linux Server. We will also describe how to unblock the ping requests in case you need to use ping for system administration and troubleshooting.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 20.04 LTS<\/li>\n<li>User with sudo privileges<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> The commands discussed here have been tested on Ubuntu 20.04 LTS.<\/p>\n<h2>Block\/unblock ping requests to Linux Server<\/h2>\n<p>Ping works by sending an ICMP packet (Echo request) to the destination system and then receives a response ICMP packet (Echo reply). In Linux, the ping command continues sending ICMP packets until you stop it using Ctrl+C.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"179\" class=\"wp-image-239\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-98.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-98.png 678w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-98-300x79.png 300w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/p>\n<p>In order to block ping requests, you will need to ignore\/block the ICMP echo requests that are sent to your server. There are following two ways through which you can block\/unblock ICMP echo requests to the Linux server.<\/p>\n<ul>\n<li>Through Kernel parameters<\/li>\n<li>Through iptables<\/li>\n<\/ul>\n<p>Let\u2019s get started.<\/p>\n<h3>Block\/unblock ping requests through kernel parameters<\/h3>\n<p>Through kernel parameters, you can block ping requests either temporarily or permanently. Kernel parameters can be modified through <strong>sysctl<\/strong> command, <strong>\/sys\/proc<\/strong> directory, and <strong>\/etc\/sysctl.conf file.<\/strong><\/p>\n<h4>Temporary block\/unblock ping requests<\/h4>\n<p>The sysctl command in Linux is used to read and write kernel parameters in the <strong>\/proc\/sys<\/strong> directory. Using this command, we can set up kernel parameters to block\/unblock ping requests. The kernel parameter <strong>net.ipv4.icmp_echo_ignore_all <\/strong>controls whether the system should respond to the ICMP echo request. The default value of it is \u2018<strong>0\u2019<\/strong> which means to respond to the ICMP request.<\/p>\n<h5>Block Ping Request<\/h5>\n<p>In order to block ping request, issue the following command in Terminal:<\/p>\n<pre>$ sudo sysctl -w net.ipv4.icmp_echo_ignore_all=1<\/pre>\n<p>This command sets the kernel parameter to &#8216;1&#8217; which means to ignore all the ICMP requests.<\/p>\n<h5><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"52\" class=\"wp-image-240\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-99.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-99.png 716w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-99-300x22.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/h5>\n<p>Now all the ping requests to your system will be blocked and the sender will receive no response as shown in the below screenshot.<\/p>\n<h5><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"159\" class=\"wp-image-241\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-100.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-100.png 678w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-100-300x70.png 300w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/h5>\n<h5>Unblock Ping Request<\/h5>\n<p>To unblock the ping requests, again run the same command by changing the parameter value to default \u20180\u2019.<\/p>\n<pre>$ sudo sysctl -w net.ipv4.icmp_echo_ignore_all=0<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"69\" class=\"wp-image-242\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-101.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-101.png 716w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-101-300x29.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/p>\n<p>Alternatively, you can block the ping requests by changing the kernel parameter value in the <strong>\/proc\/sys<\/strong> directory using the echo command. However, to use this method, you will need to run the command as root.<\/p>\n<p>In order to block ping request, first switch to root account using the following command in Terminal:<\/p>\n<pre>$ su root<\/pre>\n<p>When prompted for the password, enter the password for root.<\/p>\n<p>Then issue the following command in Terminal:<\/p>\n<pre>$ echo 1 &gt; \/proc\/sys\/net\/ipv4\/icmp_echo_ignore_all<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"828\" height=\"97\" class=\"wp-image-243\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-102.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-102.png 828w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-102-300x35.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-102-768x90.png 768w\" sizes=\"auto, (max-width: 828px) 100vw, 828px\" \/><\/p>\n<p>To unblock the ping requests, the command would be:<\/p>\n<pre>$ echo 0 &gt; \/proc\/sys\/net\/ipv4\/icmp_echo_ignore_all<\/pre>\n<h2>Permanently block ping requests<\/h2>\n<p>Kernel parameters can also be modified through the <strong>\/etc\/sysctl.conf<\/strong> file. This file will allow you to permanently block ping requests to your server.<\/p>\n<h5>Block Ping Request<\/h5>\n<p>In order to block ping request to your system, edit <strong>\/etc\/sysctl.conf<\/strong> file:<\/p>\n<pre>$ sudo nano \/etc\/sysctl.conf<\/pre>\n<p>Then append the following line in the file:<\/p>\n<pre>net.ipv4.icmp_echo_ignore_all = 1<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"347\" class=\"wp-image-244\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-103.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-103.png 786w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-103-300x132.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-103-768x339.png 768w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/p>\n<p>Save and close the file.<\/p>\n<p>Then issue the following command in Terminal to apply this configuration without reboot:<\/p>\n<pre>$ sysctl -p<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"394\" height=\"52\" class=\"wp-image-245\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-104.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-104.png 394w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-104-300x40.png 300w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/p>\n<h5>Unblock Ping Request<\/h5>\n<p>To unblock ping requests, edit the <strong>\/etc\/sysctl.conf<\/strong> file:<\/p>\n<pre>$ sudo nano \/etc\/sysctl.conf<\/pre>\n<p>Then modify the value of <strong>net.ipv4.icmp_echo_ignore_all<\/strong> to \u2018<strong>0\u2019<\/strong>:<\/p>\n<pre>net.ipv4.icmp_echo_ignore_all = 0<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"793\" height=\"347\" class=\"wp-image-246\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-105.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-105.png 793w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-105-300x131.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-105-768x336.png 768w\" sizes=\"auto, (max-width: 793px) 100vw, 793px\" \/><\/p>\n<p>Save and close the file.<\/p>\n<p>Then issue the following command in Terminal to apply this configuration without reboot:<\/p>\n<pre>$ sysctl -p<\/pre>\n<h2>Block\/unblock ping requests Using iptables<\/h2>\n<p>Iptables is a firewall utility in Linux that controls incoming and outgoing traffic based on certain rules. It comes preinstalled in the Ubuntu system. In case, it is missing from the system, you can install it using the following command in Terminal:<\/p>\n<pre>$ sudo apt install iptables<\/pre>\n<h5>Block Ping Request<\/h5>\n<p>To block ping requests to your system, type following command in Terminal:<\/p>\n<pre>$ sudo iptables -A INPUT -p icmp --icmp-type 8 -j REJECT<\/pre>\n<p>Where the <strong>A<\/strong> flag is used to add a rule in iptables and <strong>icmp-type 8 <\/strong>is the ICMP type number used for echo request<strong>.<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"50\" class=\"wp-image-247\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-106.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-106.png 802w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-106-300x19.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-106-768x48.png 768w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/strong><\/p>\n<p>The above command will add a rule in the firewall that will block any incoming ping requests to your system. By adding this rule, anyone sending the ping request to your system will see the &#8220;<strong>Destination Port Unreachable<\/strong>&#8221; message as shown in the below screenshot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"186\" class=\"wp-image-248\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-107.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-107.png 760w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-107-300x73.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/p>\n<p>If you do not want this message to appear, use the following command replacing <strong>REJECT<\/strong> with <strong>DROP<\/strong>:<\/p>\n<pre>$ sudo iptables -A INPUT -p icmp --icmp-type 8 -j DROP<\/pre>\n<p>Now anyone sending the ping request to your system will see the following similar output:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"722\" height=\"148\" class=\"wp-image-249\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-108.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-108.png 722w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-108-300x61.png 300w\" sizes=\"auto, (max-width: 722px) 100vw, 722px\" \/><\/strong><\/p>\n<h5>Unblock Ping Request<\/h5>\n<p>In order to unblock ping requests to your server, type the following command in Terminal:<\/p>\n<pre>$ sudo iptables -D INPUT -p icmp --icmp-type 8 -j REJECT<\/pre>\n<p>Where the <strong>D<\/strong> flag is used to delete a rule in iptables and <strong>icmp-type 8 <\/strong>is the ICMP type number used for an echo request<strong>.<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"69\" class=\"wp-image-250\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-109.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-109.png 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-109-300x28.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/strong><\/p>\n<p>In order to make these rules persistent after a system reboot, you will need <strong>iptables-persistent <\/strong>package. Issue the below command in Terminal to install iptables-persistent:<\/p>\n<pre>$ sudo apt install iptables-persistent<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"755\" height=\"279\" class=\"wp-image-251\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-110.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-110.png 755w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-110-300x111.png 300w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/p>\n<p>You will be asked to confirm whether you want to proceed with the installation or not. Hit <strong>y<\/strong> to proceed, after which the system will start the installation and once completed, it will be ready to use.<\/p>\n<p>After adding or deleting any rule, issue the following commands in Terminal to make them survive the system reboot.<\/p>\n<pre>$ sudo netfilter-persistent save<\/pre>\n<pre>$ sudo netfilter-persistent reload<\/pre>\n<p>In order to view all the rules added to your iptables, issue the following command in Terminal:<\/p>\n<pre>$ sudo iptables -L<\/pre>\n<p>That is all there is to it! In this article, we have discussed how to block\/unblock ping requests to Linux Server either through the kernel parameters or through iptables utility. Hope this helps!<\/p>\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Ping is a network administration utility that is used to test the availability of a system on an IP network. Ping is also used to test the quality&hellip;<\/p>","protected":false},"author":1,"featured_media":255,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[14,15],"class_list":["post-238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-ip-tables","tag-ping"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/255"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}