ssh connection sharing

Another relatively unknown feature of openssh is connection sharing. This enables you to reuse an existing ssh connection for another one. The second (and subsequent) connections(s) (called slaves) will use the same TCP connection as the first one (called master). The advantages are that the slave connections are initiated faster and that there is no password needed. And both these improvements make bash tab completion very fast. For example if you have an ssh session already running to a remote host you can use tab completion from another shell for scp, and it finds the files on the remote host.

Setting it up is simple: in your .ssh/config, add these lines:

Host *
ControlMaster auto
ControlPath /home/oku/.ssh/%r@%h:%p

Replace ‘oku’ with your user name.

The ‘ControlMaster auto’ option tells ssh to check if there is already a master, and if not set itself to the master. Otherwise, be a slave and use the master’s socket. The next line tells ssh how to name the unix socket it needs to create. In this example, it is composed of the remote user name, the host name and the port number, and will be created in the user directory in the subdirectory ‘.ssh’.

That’s all.

I use this for years now. It usually works fine, but there are a few disadvantages:

  • when the ssh connection unexpectedly dies (for example, a cold hardware reset), it leaves the socket behind. When you then try to login again, ssh complains that it cannot create the socket. You need to manually delete it.
  • when there are slaves, you cannot terminate the master session without terminating the slave connections.
  • if you do port forwarding, this works only for the master – if you try it with a slave, you don’t see an error message though. This is pretty annoying if you don’t know it. I think this also applies to ssh tunneling. This is reported as a bug in Debian.

Despite these flaws, I still find it very useful.

You will also find information here, and of course in the man pages.


Filed under debian, linux, openssh