In this question I see a line like this that will allow me to say "allow these ip addresses to connect"
iptables -A INPUT -m iprange --src-range 10.50.10.20-80 -j ACCEPT
Now, I want to further secure this so that this rule only applies to specific ports. I've been using a command like this for my regular ports:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
Can I combine these two to make a specific port allowed only for a range, like this
iptables -A INPUT -m iprange --src-range 10.50.10.20-80 --dport 12345 -j ACCEPT
Obviously I'm hesitant to just make iptables calls willy-nilly. :) Thanks!
The last line you have in there should work, you just need to make sure you have a -p protocol in there, as --dport doesn't work as a option on its own.
Alternatively, install
ipset
and you will be able to change the list of IP addresses without messing youriptables
rules:Now, if you need to add another allowed source:
Or, you need to 'drop' a host from the allowed sources:
You can save your sets:
Which you can restore during boot, before you implement your
iptables
(or else, iptables will complain!):You can even create an IP set that will match against source IP and destination port, e.g.:
More on
ipset
: http://ipset.netfilter.org/If you are using Ubuntu, you can't install the
ipset
package from its repo. Use my tip: http://pepoluan.posterous.com/powertip-howto-install-ipset-on-ubuntuYou've got the basic idea right, you can combine them into one rule like that.
However, despite what some answers say, you shouldn't use a range like 10.50.10.20-80 (it will expand to 10.50.10.20-80.0.0.0 - use the iptables command to check). You need to use the full IP address in the range e.g. 10.50.10.20-10.50.10.80.
Also, if you specify a port number, you need to state a protocol that supports ports, so the revised rule would be:
Documentaion on iprange: https://www.frozentux.net/iptables-tutorial/chunkyhtml/x2702.html#TABLE.IPRANGEMATCH