(howto) run multiple ssh session through the one forwarded port (port redirection, tunnelling)

1.1k Views Asked by At

Sorry, if it is a trivial question: I use the port forwarding the port 22 on remote computer is redirected to port 2222 on my local computer. The tunnel is created with the following azure command:

az network bastion tunnel

I can start the first session with ssh

ssh seva@localhost -p 2222

and it works fine However, when I'm starting from another terminal window another ssh session with the same command

ssh seva@localhost -p 2222

the connection hangs and goes through only when the first connection is terminated I'm aware, that I can run multiple sessions with azure native client:

az network bastion ssh

But I need multiple sessions through the same port 'classical way' because it is obviously the way, the Visual Studio Code uses when I trying to connect with it the remote computer. One session is obviously for the terminal window and another one for data transfer.

Many thanks in advance.

=Seva

1

There are 1 best solutions below

0
On

You can work around this limitation by enabling SSH multiplexing. The first session will setup a control connection, and any subsequent session will simply re-use this. This removes the need for a second connection which, weirdly enough, az network bastion does not seem to support.

To do this for all of your connections, add the following to your SSH client config (ie. ~/.ssh/config):

Host *
  # Connection Multiplexing
  ControlMaster auto
  ControlPersist 600
  ControlPath ~/.ssh/ctrl/%C

This should be all you need. If you want/need even more information though check out https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing.