Backend / DevOps / Architect
Brit by birth,
located worldwide

All content © Alex Shepherd 2008-2024
unless otherwise noted

IPTables port redirection

Published
1 min read
image
Image Credit: Unknown (if this is your work, reach out to me and I'll credit you!)

Hi guys, I was trying to find a way to route connections from my Slackware Internet gateway to a VPS I have running elsewhere on my network, and it took me a little while to work it out, so here's my solution:

PROTOCOL="tcp"
DPORT="8080"
DESTADDR="10.0.0.7"
DESTPORT="80"
iptables -t nat -A PREROUTING -p $PROTOCOL --dport $DPORT -j DNAT --to-destination $DESTADDR:$DESTPORT
iptables -t nat -A POSTROUTING -p tcp --dport $DESTPORT -j MASQUERADE

This basically just sets up a simple nat rule to forward anything coming in to the gateway on port 8080 to the machine with IP address 10.0.0.7 on port 80.

Simples!

Feel free to leave any questions, corrections or comments!

n00b