I am trying to block the entire Internet on all ports, but allow a multiple ranges to connect on specific ports. I want this ruleset to apply to outbound connections as well (using REJECT instead of DROP) as to prevent identification attacks.


# Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT

# Accept all established inbound connections

# Allow all network traffic

# Allow all SSH
-A INPUT -p tcp --dport 22 -j ACCEPT

# Permitted web connections
-A INPUT -s -p tcp --dport http  -j ACCEPT
-A INPUT -s -p tcp --dport http  -j ACCEPT
-A INPUT -s -p tcp --dport https -j ACCEPT
-A INPUT -s -p tcp --dport https -j ACCEPT

-A OUTPUT -s -p tcp --dport http  -j ACCEPT
-A OUTPUT -s -p tcp --dport http  -j ACCEPT
-A OUTPUT -s -p tcp --dport https -j ACCEPT
-A OUTPUT -s -p tcp --dport https -j ACCEPT

# Reject all web not to or from a safe server
-A INPUT  -p tcp --dport http  -j DROP
-A INPUT  -p tcp --dport https -j DROP
-A OUTPUT -p tcp --dport http  -j REJECT
-A OUTPUT -p tcp --dport https -j REJECT

# Drop all other inbound - default deny unless explicitly allowed


The drawback with my very strict policy is the verbosity of adding an additional range. Each range takes four lines of policy to work. Inbound/HTTP, Inbound/HTTPS, Outbound/HTTP, Outbound/HTTP. It's a huge pain to keep straight.

What I would like it something like this:

# Pseucode
*group webservers

-A INPUT -s webservers -p tcp --dport http  -j ACCEPT
-A INPUT -s webservers -p tcp --dport http  -j ACCEPT
-A OUTPUT -s webservers -p tcp --dport http  -j ACCEPT
-A OUTPUT -s webservers -p tcp --dport http  -j ACCEPT

Is there an easy way to accomplish this?


2 Answers 2


You might want to try using ipsets

If you want to

  • store multiple IP addresses or port numbers and match against the collection by iptables at one swoop;
  • dynamically update iptables rules against IP addresses or ports without performance penalty;
  • express complex IP address and ports based rulesets with one single iptables rule and benefit from the speed of IP sets

then ipset may be the proper tool for you.

Worth noting that:

IP sets are a framework inside the Linux kernel, which can be administered by the ipset utility

And yes, the website looks like it hasn't been updated since 1996. For some reason computer security news sources and low-level software / firmware project webpages look like that a lot.


You can also try sending all the traffic from certain ranges to a custom chain, then each port-related accept becomes a single line:

iptables -N CUSTOM

iptables -I INPUT 1 -s -j CUSTOM
iptables -I INPUT 2 -s -j CUSTOM

iptables -A CUSTOM - p tcp --dport 80 -j ACCEPT
iptables -A CUSTOM - p tcp --dport 443 -j ACCEPT

Packets from the source range(s) sent to the CUSTOM chain that don't match an ACCEPT there will come back to the INPUT chain for handling (ie, DROPping) with all the rest of the unwanted traffic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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