I need to block all connection on port 30000. I alredy did that , with command like that

iptables -I INPUT ! -s IP_HERE -p tcp --dport 30001 -j DROP

but now i need 2 ips , how i could do that?

That ips , that i need to allow: localhost, and other ip. Lest say one of google.com ips

  • Use two iptables rules? Aug 17, 2014 at 10:36
  • @CraigWatson that is the same as "go and google that question" Aug 17, 2014 at 10:45
  • Not really, your question was "allow connection from two IP addresses" and I proposed using two separate rules rather than just one. Aug 17, 2014 at 10:48

2 Answers 2


Reverse the logic (accept if.. instead of drop if not..) in your iptables rules and use two rules:

iptables -A INPUT -s FIRST_IP_HERE -p tcp --dport 30001 -j ACCEPT
iptables -A INPUT -s SECOND_IP_HERE -p tcp --dport 30001 -j ACCEPT
iptables -A INPUT -p tcp --dport 30001 -j DROP
  • That did work , ty! Aug 17, 2014 at 10:58
  • Oh and plz , dislike other person... using links on stack / server sites are very bad... what if someone will delete that site? Aug 17, 2014 at 10:58

To drop all traffic but allow two specific IP addresses, you will need to use three rules in this order (iptables rules are processed in order, so the order matters):

  • Allow IP 1
  • Allow IP 2
  • Drop all

If multiple IPs are on the same subnet, you can use either CIDR or IP/mask notation.

You can also do something more fancy like create a chain, see this link.

  • Its localhost ip , and ip that not localhost :P btw , i asked HOW I DO THAT. Idk how to do multiple iptables rules. Aug 17, 2014 at 10:39
  • I don't understand the point - can you edit your question with more detail? Aug 17, 2014 at 10:40
  • I need to allow only 2 ips , but idk how Aug 17, 2014 at 10:43
  • See my answer. You use two rules, one for each IP you want to block. You'll need to remove the ! in your first rule, as it currently reads "drop all traffic on port 3000 apart from this IP" and you want the inverse for two specific IP addresses. Aug 17, 2014 at 10:45
  • Just edited my answer now that you've clarified the question. Aug 17, 2014 at 10:55

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