iptables: direct traffic from one port to an alternate port

Requirement was to run a process on a server on a new port but the original port was hard-coded into the client so we needed to redirect all traffic bound for the original port to the new port.

First check the IP Forwarding is enabled.

sysctl net.ipv4.ip_forward

if IP forwarding is not enabled then:

echo "net.ipv4.ip_forward = 1"|sudo tee /etc/sysctl.d/99-ipforward.conf
sysctl -p /etc/sysctl.d/99-ipforward.conf

Then add an iptables REDIRCT from the old port 1111 to the new port 2222

iptables -t nat -A PREROUTING -p tcp --dport 1111 -j REDIRECT --to-port 2222

Check it with:

iptables -t nat -L -n

Leave a Reply

  • (will not be published)

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