Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to allow a few IPs on port 80 and disallow all other from access port 80.

Can someone suggest the iptables rules needed for this to occur?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Try this

/sbin/iptables -A INPUT -s FROMIP.1 -p tcp --dport 80 -j ACCEPT
/sbin/iptables -A INPUT -s FROMIP.2 -p tcp --dport 80 -j ACCEPT
/sbin/iptables -A INPUT -s FROMIP.3 -p tcp --dport 80 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 80 -j DROP

Notes:

  • This assumes you are using a script which calls IPTABLES. (If you are using /etc/sysconfig/iptables, drop the first /sbin/iptables command)

  • This assumes IPTables is running on the webserver. If its running on a firewall, replace the INPUT with FORWARD (and optionally add -d DEST.IP after FROM.IPX if you want to block it to a single host

  • This only deals with traffic on Port 80, I assume you wanted it.

  • the -A is important, as it appends rules (matching order is important). If you use -I to insert rules, reverse the order of the rules.

share|improve this answer
    
Do I have to flush existing rules? I am not quite sure if order matter in this case. –  superuser Feb 18 '13 at 23:21
    
The answer is very much "it depends" entirely on your setup. If you can provide more details on your current rules, how they are loaded and edited I can give you an answer. In general terms, if you put those rules in IN REVERSE ORDER and with the -I flag rather then -A flag they will be inserted into the beginning of the table and will be effective even if you don't reload (until they are wiped at the next reboot) –  davidgo Feb 18 '13 at 23:55
    
You must make sure that the rules appear "soon enough". The iptables rules are executed in order, if some other rule does an ACCEPT, your rules won't be considered. This tutorial might help, also look at the documentation for the tools your distribution favors. –  vonbrand Feb 19 '13 at 3:00

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.