I want to forward a port from my raspberry Pi to a server reachable through a VPN. I also want to be able to see the client IP from the server.

The setup looks something like this:

                 LAN                                 VPN
Real Client   | <---->  | Raspberry Pi            | <---->    |Server      |   |         |  /  |           |   |
______________|         |_________________________|           |____________|

So the client should be able to e.g. connect through HTTP on port 80 to the raspberry pi on There, the packet should be forwarded through the VPN to the server ( The server should see that the connection comes from

How can I do something like this? I already tried DNAT:

iptables -A PREROUTING -t nat -i eth0 -d -p tcp --dport 80 -j DNAT --to-destination
iptables -A FORWARD -p tcp -d --dport 80 -j ACCEPT

But that doesn't work. I only get Failed to connect to port 80: Connection refused


You need to use normal IP routing on your Raspberry Pi instead of any NAT.

This requires that you set up proper routing tables at gateway devices so that they know where the packets to the other network need to be sent to.

For example, you need to add a route to the server where you tell that network needs to be routed via

On the client side such additions might not be necessary, if Raspberry Pi is the default gateway of Real Client. If the default gateway is something else, then you need to add a route to via

| improve this answer | |

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.