Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

iptables -A zone_lan_forward -p tcp -d 1.2.3.0/24 -j ACCEPT
iptables -A zone_lan_forward -p udp -d 1.2.3.0/24 -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 1.2.3.0/24 -j ACCEPT
share|improve this question

2 Answers 2

up vote 10 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 1.2.3.0/24 -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. –  Koveras May 17 '13 at 22:16

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. –  Koveras 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

 
discard

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.