Linux: Iptables # 11 How to Block or open http/web service

by LinuxTitli · 7 comments

By default Apache webserver listen on port 80 (http) and port 443 (https i.e. secure http). Apache webserver uses the TCP protocol to transfer information/data between server and browser.

A) Allow incoming http/web traffic at port 80
SERVER_IP=”202.54.10.20”
iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $SERVER_IP --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 80 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

B) Allow incoming https/secure web traffic at port 443
SERVER_IP=”202.54.10.20”
iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $SERVER_IP --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 443 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

C) Allow outgoing http/web service traffic to port 80
SERVER_IP=”202.54.10.20”
iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 1024:65535 -d 0/0 --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -s 0/0 --sport 80 -d $SERVER_IP --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

D) Allow outgoing https/secure web service traffic to port 443
SERVER_IP=”202.54.10.20”
iptables -A OUTPUT -p tcp -s $SERVER_IP --sport 1024:65535 -d 0/0 --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -s 0/0 --sport 443 -d $SERVER_IP --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 7 comments… read them below or add one }

1 Sunil Shrestha 12.29.06 at 7:46 am

grate site

2 Uttam Shrestha Rana 03.01.07 at 9:33 am

How to configure Squid server with bandwidth limitation for particular network ips?
If you response with the configuration, then it will be great help me if not also, from this site i have got lots of information. Thanks. Its a greate knowledge protal.

3 Vasanth kumar 08.02.07 at 9:51 pm

In windows how to block https site like Gmail

4 kunal 02.25.08 at 11:48 am

Script to block incoming HTTP request from an IP say after 20 continue requests.

Thanks in advance
–kunal

5 kunal 02.25.08 at 11:49 am

Just to add one more thing IP blocking should be done for certain period of time say 5 hrs and after unblock that IP.

6 Liju 07.04.08 at 3:39 pm

This would be much simple and better. There is no necessary to permit the oubound traffic to be opend and can be avoided.

# Allow incoming port 80 and 443 (http/s) traffic
/sbin/iptables -A INPUT -p tcp –dport 80 -m state –state NEW -j ACCEPT
/sbin/iptables -A INPUT -p tcp –dport 443 -m state –state NEW -j ACCEPT

7 Well... 05.08.09 at 5:17 am

…you all forgot to mention that conntrack has to be enabled as well. Otherwise only the first packet of the connection is let in, but the “real” data is still blocked… ;-)

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: