I am hosting special HTTP and HTTPS services on the ports 8006 and 8007 respectively. I use IPTABLES to 'active' the server; i.e. to route the incoming HTTP and HTTPS ports:
iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8006 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8007 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8006
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8007
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to-ports 8006
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 443 -j REDIRECT --to-ports 8007
This works like a charm. However I would like to create another script that disables my server again. I.e. restores IPTABLES to the state it was before running the lines above. However I am having a hard time figuring out the syntax to remove these 6 rules. The only thing that seems to work is a complete flush:
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
But that will also delete other iptables rules which is undesired.
-I
instead of-A
forACCEPT
lines. This is because typically, the last line (forINPUT
chain for example) is aDROP
orREJECT
and you want your rule to come before that.-A
puts the new rule after the last rule, while-I
puts it at the start. – Mark Lakata Jul 30 '14 at 20:30