How do I ssh tunnel port forward?

Solution 1:

I have drawn some sketches

The machine, where the ssh tunnel command is typed is called »your host«.

ssh tunnel starting from local


ssh tunnel starting from remote

Introduction

  1. local: -L Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.

    ssh -L sourcePort:forwardToHost:onPort connectToHost means: connect with ssh to connectToHost, and forward all connection attempts to the local sourcePort to port onPort on the machine called forwardToHost, which can be reached from the connectToHost machine.

  2. remote: -R Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.

    ssh -R sourcePort:forwardToHost:onPort connectToHost means: connect with ssh to connectToHost, and forward all connection attempts to the remote sourcePort to port onPort on the machine called forwardToHost, which can be reached from your local machine.

Your example

You want to connect to a remote server through the EC2 machine, right? Then the second image represents your scenario. The remotehost is your EC2 machine. The farawayhost is your remote server with its pink port of interest (in the drawing port 456, but in your case port 22). So the command has to look like this:

ssh -L 22528:REMOTE-IP:22 EC2-MACHINE-IP

The port number of the green port is 22528. Now you can access your remote server with ssh via

ssh -p 22528 localhost