Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a number of iptables rules on my firewall that look like this:

iptables -A zone_lan_forward -p tcp -d -j ACCEPT
iptables -A zone_lan_forward -p udp -d -j ACCEPT

Is there a shortcut for having two rules - one for tcp and one for udp - for every address? I mean can I do something like this:

iptables -A zone_lan_forward -p tcp,udp -d -j ACCEPT
share|improve this question
up vote 16 down vote accepted

Create a new chain which will accept any TCP and UDP packets, and jump to that chain from the individual IP/port permissive rules:

iptables -N ACCEPT_TCP_UDP
iptables -A ACCEPT_TCP_UDP -p tcp -j ACCEPT
iptables -A ACCEPT_TCP_UDP -p udp -j ACCEPT

iptables -A zone_lan_forward -d -j ACCEPT_TCP_UDP

This adds the overhead of a few extra lines, but halves the number of TCP / UDP rules.

I would not omit the -p argument, because you're not only opening up the firewall for ICMP, but also any other protocol. From the iptables man page on -p:

The specified protocol can be one of tcp, udp, icmp, or all, or it can be a numeric value, representing one of these protocols or a different one. A protocol name from /etc/protocols is also allowed.

You may not be listening on any protocols except for TCP, UDP, and ICMP right now, but who knows what the future may hold. It would be bad practice to leave the firewall open unnecessarily.

Disclaimer: The iptables commands are off the top of my head; I don't have access to a box on which to test them ATM.

share|improve this answer
This is a very elegant solution that doesn't leave the firewall open unnecessarily. – Dave Kennedy May 17 '13 at 22:16
But this method of creating new chain will failed if the filtering is done through destination port number. Can anyone suggest how to overcome from above mentioned problem? – Amor Oct 9 '14 at 9:22

If you don't really care about ICMP traffic (which you can block globally with a rule anyway), you can just omit the -p flag and it'll cover all the protocols.

share|improve this answer
Should I care about ICMP traffic? I am mostly concerned with HTTP access. – Dave Kennedy May 17 '13 at 16:09
Not really. You can block ICMP (ping) if you want to but since it's serving HTTP traffic anyway there's not much point. – Nathan C May 17 '13 at 16:11
@NathanC, I think the advice to open up ALL ports when the OP is asking how to halve his rules may lead to trouble, now or in the future. – Jed Daniels May 17 '13 at 17:53
@JedDaniels the -p switch specifies the protocols and not ports. The answer below has an alternative if they care to lock down anything other than tcp & udp. – Nathan C May 17 '13 at 17:59
@NathanC Yes, and removing the -p means "open up all protocols, not just tcp or udp", which is reckless if not dangerous. – Jed Daniels May 17 '13 at 18:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.