User Tools

Site Tools


docu:csheet:net:iptables:port_redirection

How to redirect ports using Iptables

(It may not work depending on more iptables rules or firewalls)


Make the actual redirection from port 80 to 8080

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

For debugging purposes or for loopback access of this rule, you need to enable loopback redirection too from your machine.

# syntax
iptables -t nat -A PREROUTING -d [IN_IP] -p tcp --dport 3456 -j DNAT --to-destination 2.2.2.2:1234
iptables -t nat -A OUTPUT -d [IN_IP] -p tcp --dport [PORT] -j DNAT --to-destination [TO_IP]:[TOPORT]
 
# example:
iptables -t nat -A OUTPUT -d 127.0.0.1 -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:8080
 
# nat 1:1 port from public to private IP space
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -t nat -A PREROUTING -d [A_PUBLIC_IP] -p tcp --dport 222 -j DNAT --to-destination [LAN_IP]:22
docu/csheet/net/iptables/port_redirection.txt · Last modified: 2020/07/19 22:32 by admin