I want to deflect some IP address that we access using any tools (like nmap, medusa, etc) to the another IP Address.

For more details: I have 2 IP Addresses, First IP Address is and the Second is When I want to access the first IP Address using another PC in the same LAN, I want to deflect that to the Second IP Address on port 22, so I using Iptables for that as you can see here, I add some rule on the second IP Address pc:

echo "1" > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination

iptables -t nat -A POSTROUTING -j MASQUERADE

after I tried that, it was work. But, I really need the IP Address information about who was accessing the IP Address because the result show that it's only the first IP Address that was accessing the second IP Address, not the third one. Can you help me please? Because it always converting the IP Address Source and I don't know why.

Oh, I also tried using any tools port forwarder like rinetd, socat, etc. But it doesn't work. Do you have any idea for this? I really need help.

[Update] :

I've tried using SNAT on the rule in iptables but it has same result. I read this post How to do the port forwarding from one ip to another ip in same network? but I don't know what must I do with that


1 Answer 1

Server-A =
Server-B =
Client-PC =

From what you are trying to achieve, the MASQUERADE rule is the culprit and must be removed. Here is a scenario. When Client-PC attempts to connect to Server-A, it must be redirected to Server-B on TCP 22. From Server-B perspective, traffic is coming from

On the host that is acting as a Linux gateway, do the following:

$ sudo echo "1" > /proc/sys/net/ipv4/ip_forward
$ sudo iptables -t nat -A PREROUTING -p tcp -m tcp -d --dport 22 -j DNAT --to-destination
$ sudo iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

From Server-B, make sure it knows how how to send return traffic back to

If not, then add a static route on Server-B

$ sudo ip route add via X.X.X.X # Where X.X.X.X is the IP of the Linux gateway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.