forward outgoing traffic port using iptables

I am trying to forward all outgoing traffic from port 80 to port 8080 using iptables and I tried the following rule, though it did not work:

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

Also, for incoming traffic I need a rule that forward port on the same host not to be as proxy.

Asked By: dana.ba

||

You will need that under nat

e.g

*nat
:PREROUTING ACCEPT
:POSTROUTING ACCEPT 
:OUTPUT ACCEPT

-A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080

you can run this: with example of forwarding 80 to 8080 and so on…

Incoming on 80 to 8080

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

Outgoing on 80 to 8080

iptables -t nat -A POSTROUTING -p tcp --dport 80 -j SNAT --to-ports 8080

Note: I haven’t tested this.

Answered By: tike
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.