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

2 Answers 2


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.

  • This is a very elegant solution that doesn't leave the firewall open unnecessarily. May 17, 2013 at 22:16
  • 5
    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, 2014 at 9:22
  • @Amor In this example if you used -p all in all --dport rules on the zone_lan_forward chain, that might achieve what you're looking. I am of course assuming there is no other way to get onto that chain with a non-TCP/UDP protocol due to the ACCEPT_TCP_UDP chain. Obviously this is a risky strategy if multiple people have access to modify rules and someone comes along and edits your rules without understanding this subtlety. Nov 24, 2016 at 9:26
  • Ooops. Didn't notice the order of chains. You would need to switch the order of the chains in this example too for what I just said to work properly. So ACCEPT_TCP_UDP jumps to zone_lan_forward which then jumps to ACCEPT. Nov 24, 2016 at 9:29

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.

  • Should I care about ICMP traffic? I am mostly concerned with HTTP access. May 17, 2013 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, 2013 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. May 17, 2013 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, 2013 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. May 17, 2013 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .