How to connect 2 PCs via ssh client using a server

port-forwardingssh

I have 2 PCs that I want to connect over the internet with a ssh port forward. The problem is that both PCs dont have a public IP. My Idea was to use a public server (ps) and use ssh -R <port>:localhost:<port> <ps-user>@<ps-address> on PC1.

For PC2 I tried 2 things to create a Connection to PC1: ssh -p <port> <pc1-user>@<ps-address> and ssh -L <port>:localhost:<port> <pc1-user>@<ps-address>. But both do not result in an ssh connection to PC1.

What do I do wrong here?

Addition: Please note that I can not port forward on both PC1 and PC2

Best Answer

So, if I understood your question, you have:

  • 2 PCs on internal networks.
  • You have no influence on the routers on either network.

And you want to:

  • Set up a public server (PS) hosting an SSH service (I assume port 22).
  • Connect PC1 and PC2 to PS (Outgoing traffic, no router config).
  • Connect a VNC session from PC2 (Windows) to PC1 (Ubuntu) on port 5900.

You can achieve that by creating an SSH tunnel, doing a remote forwarding from PC1 and a local forwarding from PC2.

This would be the architecture:Architecture diagram

So to get this working, from the Ubuntu PC, PC1, you need to connect to PS using:

ssh <ps-user>@<ps-address> -R 5901:localhost:5900

At this point if you logged into the PC1 you may run netstat -latn | grep 5901 and you should see it listening. This means that one half of the process is ready.

Now the client connection. From PC2, if it was a linux box too, you might use

ssh <ps-user>@<ps-address> -L 5902:localhost:5901

if it is Windows, you can use PuTTY, by setting the port forwarding section below Connection / SSH / Tunnels, and add a Local port with Source port 5902 and destination localhost:5901. When clicking add you will see in the list of forwarded ports something like L5902 localhost:5901

Now you have two tunnels connected to a public server. If you run VNC viewer from PC2 and point to localhost:5902, you should connect to the VNC service listening on port 5900 on PC1.

Hope it helps.

Related Question