Linux Iptables allow or block ICMP ping request

The Internet Control Message Protocol (ICMP) has many messages that are identified by a "type" field. You need to use 0 and 8 ICMP code types.

=> Zero (0) is for echo-reply

=> Eight (8) is for echo-request.

To enable ICMP ping incoming client request use following iptables rule (you need to add following rules to script).

My default firewall policy is blocking everything.

Task: Enable or allow ICMP ping incoming client request

Rule to enable ICMP ping incoming client request ( assuming that default iptables policy is to drop all INPUT and OUTPUT packets)

SERVER_IP="202.54.10.20"
iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -d $SERVER_IP -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

iptables -A OUTPUT -p icmp --icmp-type 0 -s $SERVER_IP -d 0/0 -m state --state ESTABLISHED,RELATED -j ACCEPT

Task: Allow or enable outgoing ping request

To enable ICMP ping outgoing request use following iptables rule:

SERVER_IP="202.54.10.20"
iptables -A OUTPUT -p icmp --icmp-type 8 -s $SERVER_IP -d 0/0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

iptables -A INPUT -p icmp --icmp-type 0 -s 0/0 -d $SERVER_IP -m state --state ESTABLISHED,RELATED -j ACCEPT

How do I disable outgoing ICMP request?

Use the following rules:

iptables -A OUTPUT -p icmp --icmp-type echo-request -j DROP

OR

iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP

ICMP echo-request type will be block by above rule.

See ICMP TYPE NUMBERS (type fields). You can also get list of ICMP types, just type following command at shell prompt:
# /sbin/iptables -p icmp -h

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 15 comments… read them below or add one }

1 Anonymous 08.01.05 at 4:19 am

Thank you for the above post. I find what i was looking for about 15min googling.
Thanks.

2 polarizers 2cent 10.05.05 at 3:25 pm

This seems to be incomplete. An ICMP ping is an “icmp echo request” that is followed up by an “icmp echo reply”. So you need to specify the appropriate “–icmp-type” in your incoming and outgoing chains.

Possible values for –icmp-type are listed by “iptables -p icmp -h”. There are a icmp packets you dont want to recieve or reply to.

polarizers 2cent
http://www.codixx.de/polarizer.html

3 LinuxTitli 10.05.05 at 11:29 pm

>This seems to be incomplete
Noop, this is not incomplete.

>Possible values for –icmp-type are listed by “iptables -p icmp -h”. There are a icmp packets you dont want to recieve or reply to.

Yup, but you don’t have to use them. I just prefer to keep it simple aka KISS. My Default firewall policy is block everything, so this works w/o problem.

4 fsckyou 11.17.05 at 4:39 am

I beg to differ. IT is incomplete. You block everything, then you open all ICMP traffic. How does this block other types of ICMP traffic?

5 Anonymous 01.31.06 at 7:45 pm

i need to disable ougoing ICMP from my server with iptables, how do I do that?

6 LinuxTitli 01.31.06 at 9:51 pm

I have updated post as per your request see above.

7 LinuxTitli 01.31.06 at 9:52 pm

fsckyou, you are right i have updated the entire rules, thanks :D

8 OSCAR 06.28.07 at 4:58 pm

Hi,
How block ping request with ENDIAN firewall?
Best regards

9 China Landscape 10.08.07 at 4:48 am

Hi,

Thanks for your tip, it’s work perfectly.
Just one question :
Is the options –state NEW,ESTABLISHED,RELATED are mandatory ?

China Landscape

10 vivek 10.08.07 at 5:02 am

–state will improve security and it is one of the best features of Iptables. I recommend keeping it..

11 Hide IP 12.02.07 at 7:51 am

Thanks for info! We’ll use this info in our script firewall.

12 ak 11.03.08 at 3:13 am

iptables -A INPUT -p icmp –icmp-type 8 -s 0/0 -d $SERVER_IP -m state –state NEW,ESTABLISHED,RELATED -j ACCEPT

Error: Bad argument `state’

13 Bill 12.08.08 at 6:48 pm

AK, you have a typo. Should be “–state”

14 Vivek Gite 12.08.08 at 8:20 pm

@ak,

You must replace $SERVER_IP with actual IP address or create a variable itself.

15 ashwani 04.24.09 at 8:27 pm

Nice…how about i can ping anyone but none cant ping me?…i mad lil rule

iptables -A OUTPUT -p icmp –icmp-type echo-reply -s 192.168.1.50 -d 192.168.1.0/24 -j REJECT

this for an single subnet :-)

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , ,

Previous post: Linux Iptables Avoid IP Spoofing And Bad Addresses Attacks

Next post: FreeBSD: How to write protect important file ( even root can NOT modify / delete file )