One old weird trick to speed SSH

SSH connection multiplexing allows many clients to share the same control connection to a server, speeding the connection start and providing some cool features.


SSH is awesome, it’s perhaps into the top 10 most important tools for the sysadmin job and still surprises me sometimes with cool features.

Doing some reading about cluster administration using SSH I stumbled into this:

Host *
  ControlMaster auto
  ControlPath /tmp/ssh_mux_%h_%p_%r
  ControlPersist 4h

What witchery is that, you ask? Has science gone too far? Well, no, that’s actually pretty old and I should be probably ashamed of only discovering it so recently.

Those options cause your client to create a master connection to the server you are connecting, with all SSH overhead (key exchange, session setup, authentication, etc) on your first connection. The real magic occurs when you open more connections to the same server: they piggyback on the master, and skip all the long ass initialization phase.

That’s a good thing if you (like me) spend most of your day logged into many servers at the same time, and even multiple times to the same server (because you forgot to run tmux).

Another neat thing is that when a master connection is open, you get a bonus tab-completion feature on scp for remote directories/files. You have to try it to see how much difference it makes.

The ControlPersist option is kinda of a controversy IMO because it means (on the above case) that the master connection will remain open for that amount of time after you disconnect the server. That helps a lot when you are going in and out of servers, but still a connection dangling around the network… so, YMMV.

To read the full description and caveats of each option, check the ssh_config man page, as always.

cya!

 
comments powered by Disqus