Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 created a ipset with a bunch of IPs that I want to block access to dovecot and exim.

The ipset is called "bannedIPs" and have been added to iptables using this

iptables -A INPUT -p tcp --dport 25 -m set --set bannedIPs src -j DROP
iptables -A INPUT -p tcp --dport 587 -m set --set bannedIPs src -j DROP
iptables -A INPUT -p tcp --dport 465 -m set --set bannedIPs src -j DROP
iptables -A INPUT -p tcp --dport 110 -m set --set bannedIPs src -j DROP
iptables -A INPUT -p tcp --dport 143 -m set --set bannedIPs src -j DROP
iptables -A INPUT -p tcp --dport 993 -m set --set bannedIPs src -j DROP
iptables -A INPUT -p tcp --dport 995 -m set --set bannedIPs src -j DROP

my question is:

  • Are these rules correct? Will they block IPs on the bannedIPs ipset from accessing exim and dovecots on all ports of these services?
share|improve this question
Yes, why you suspect that? – warl0ck Mar 13 '13 at 7:47
because I see the same IPs trying to access dovecot and/or exim on /var/log/maillog and/or /var/log/exim/mainlog after they have been added to the set. Once I have a new IP I just add it to the ipset using ipset -A bannedIPS #.#.#.#. Is adding an IP to ipset all I have to do to block that IP or do I have to reload the ipset or whatever after adding a new IP? – SpaceDog Mar 13 '13 at 7:52
Nope, it's loaded automatically ;-) – warl0ck Mar 13 '13 at 7:52
thanks for the help. – SpaceDog Mar 13 '13 at 7:58
up vote 1 down vote accepted

Yes, and no.

You didn't tell us what service you are running, imap(s) or pop, smtp(s) etc., and if it's all using the default port.

But, to verify that you've collected all necessary ports, run i.e netstat -luantp to get a list of listening ports. Then compare the list of ports against it.

Also, consider put these rules into a single one,

iptables -A INPUT -p tcp -m multiport --dports 25,587,465,110,143,993,995 -m set --match-set bannedIPs src -j DROP

To save / restore ipset lists, try ipset save > ipset.rules and ipset restore < ipset.rules

share|improve this answer
yes, dovecot and exim are using default ports. I will try your multiple rule – SpaceDog Mar 13 '13 at 7:55
ah, just to clear one thing. As far as I know these ipset/iptables rules are cleared if you shutdown/restart a machine. Is there a way to save all these rules and ipset and reload them automatically after a restart? – SpaceDog Mar 13 '13 at 8:15
@DigitalRobot updated answer – warl0ck Mar 13 '13 at 8:23
thanks. So I guess all saving/restore are always manual, unless I add a script to automate that, right? I thought there was a way to automatically load iptables with all ipsets defined after a restart. Thanks – SpaceDog Mar 13 '13 at 8:26
@DigitalRobot nope, prepare your own init script – warl0ck Mar 13 '13 at 8:30

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.