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«.
Introduction
-
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 toconnectToHost
, and forward all connection attempts to the localsourcePort
to portonPort
on the machine calledforwardToHost
, which can be reached from theconnectToHost
machine. -
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 toconnectToHost
, and forward all connection attempts to the remotesourcePort
to portonPort
on the machine calledforwardToHost
, 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