SSH port forwarding

(λx.x)eranga
Effectz.AI
Published in
2 min readApr 1, 2016

Scenario

“I have a remote host machine with IP 10.4.1.41 address. Inside this machine there are some running docker containers. I want to connects to this remote host’s docker containers from my local machine with IP 10.4.1.35 address”

Port forwarding

I can use local port forwarding in here. Local port forwarding allows to access services on the remote network from your local host via a local listener.

There are two ways to do local port forwarding,

  1. Via command line
  2. Via ssh config

1. Command line

In here you can execute ssh local port forwarding command from your local machine. Following is the syntax of the command.

Following is the example command that I have used to access postgres container running on remote host.

Now I can connect to the postgres instance running on remote host docker container from my local machine

2. SSH config

We can define all of the port forwarding details/rules in ~/.ssh/config file instead of manually entering via command line.

Then we can directly ssh to the remote host in order to apply the port forwarding.

Now I can connect to the postgres instance(which running on remote host docker container) as same as previously.

Reference

  1. http://pentestmonkey.net/cheat-sheet/ssh-cheat-sheet
  2. http://www.mpcdf.mpg.de/services/network/secure-shell/ssh-tunnelling-port-forwarding
  3. https://chamibuddhika.wordpress.com/2012/03/21/ssh-tunnelling-explained/
  4. http://ubuntuguide.org/wiki/Using_SSH_to_Port_Forward
  5. http://unix.stackexchange.com/questions/115897/whats-ssh-port-forwarding-and-whats-the-difference-between-ssh-local-and-remot

--

--