iptables port redirect

Associate
Joined
28 Nov 2005
Posts
431
Location
Scotland
Using iptables I know exactly how to do what I want to do...if that makes sence!

Redirect TCP traffic destined for port 80 to port 2020:

from a shell I issue:
Code:
iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-ports 2020


however, I am not entiry sure EXACTLY what this does, I know it creates a rule in iptables but where is these rule stored? Is in in a config file anywhere?

Also, if I want to undo this rule; stop redirecting TCP traffic destined for port 80 to port 2020 - returning to normal behaviour, how do I do so?

Thanks,
 
however, I am not entiry sure EXACTLY what this does, I know it creates a rule in iptables but where is these rule stored? Is in in a config file anywhere?

Also, if I want to undo this rule; stop redirecting TCP traffic destined for port 80 to port 2020 - returning to normal behaviour, how do I do so?

Thanks,

Once you issue your rule command, it's maintained in memory only. You can see the NAT tables current rule contents with:

iptables -L -t nat

To clear the table (undo the port forward):

iptables -t nat --flush

So essentially you need to store your rules manually and restore them after a reboot. I just do this by putting my rules in a shell script and running the script on boot.

You could also do:

iptables-save > my_rules

combined with:

iptables-restore < my_rules

Hope that helps.
 
If you've got multiple entries in a chain you might want to avoid flushing the whole table for a selective removal:

iptables -t nat -D PREROUTING #

where # is the rule number in that chain.
 
Back
Top Bottom