Search code examples
linuxnetwork-programmingfirewalliptables

How to redirect a request to localhost?


I created an iptables-rule:

-A PREROUTING -s 192.168.XXX.XX/32 -d 192.168.YYY.YY/32 -p tcp -m tcp --dport 65430 -j DNAT --to-destination 127.0.0.1:65435

I even used sysctl -w net.ipv4.ip_forward=1, but it still doesn't work. But it worked to redirect the request to an other port via

-A PREROUTING -s 192.168.XXX.XX/32 -d 192.168.YYY.YY/32 -p tcp -m tcp --dport 65430 -j DNAT --to-destination 192.168.YYY.YY:65435

Solution

  • you just need REDIRECT

    iptables -t nat -I PREROUTING -s 192.168.XXX.XX/32 -d 192.168.YYY.YY/32 -p tcp --dport 65430 -j REDIRECT --to-ports 65435