Tuesday, March 12, 2013

Redirect to a different port using IPTABLES

 iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 

-t = table 

-A = Appending 

PREROUTING:- Is the chain which is responsible for packets that just arrived at the network interface
So far no routing decision has taken place, therefore it is not yet known whether the packet would be interpreted locally or whether it would be forwarded to another machine located at another network interface. After the packet has passed the PREROUTING chain the routing decision is made.


-p = protocol

--dport = destination port 

-j =  
 -j, --jump target
This specifies the target of the rule; i.e., what to do if the packet matches it.  The  target  can  be  a  user-defined  chain  (other than the one this rule is in).


--to-port
--to-ports port[-port]
              This specifies a destination port or range of ports to use: without this, the destination port is never altered.  This is  only valid if the rule also specifies -p tcp or -p udp.
 

Test it by 
iptables -t nat -L

to check which port is used
netstat -ntl
 

 
Note:-
The chains PREROUTING und POSTROUTING are the most important ones. As the name implies, the PREROUTING chain is responsible for packets that just arrived at the network interface. So far no routing decision has taken place, therefore it is not yet known whether the packet would be interpreted locally or whether it would be forwarded to another machine located at another network interface. After the packet has passed the PREROUTING chain the routing decision is made

No comments: