The last versions of OpenSSH brought an interesting feature: sharing multiple sessions over a single connection. When enabled, this is how it works:
- The first time you open an SSH connection to a server (including anything that works on top of SSH, like SCP, SFTP, rsync or Git), it opens a network connection as usual. It also opens a local Unix socket and listens to it for later use.
- The next times you open an SSH connection to that server while the first one is still open, instead of opening a new network connection, it connects to that local Unix socket and lets the first SSH client carry its new session.
Advantages
- By reusing an existing connection, it saves file descriptors, thus power, baby seals and kittens, and most of all it is faster than opening new ones.
- It does not require a new authentication, so it saves you the time to type your password or you key passphrase, and the time for the challenge-response to take place.
- Since you do not have to authenticate again, you can take advantage of you shell's power features to auto-complete remote paths for SCP, SFTP or rsync over SSH. That is, if you use a powerful shell, of course.
Disadvantages
Just like SSH agents, there is one major side-effect to take into account: new remote sessions can be opened without a password. Never use such a feature on a public computer if you are used to temporarily leave running sessions: here, locking them would not protect other people to get a shell access to your remote account!
Configuration
In ~/.ssh/config or /etc/ssh/ssh_config:
ControlPath ~/.ssh/control-%r@%h:%p ControlMaster auto ControlPersist 1
ControlPath
- The name template for the session sharing Unix sockets. See the ssh_config(5) manpage for the syntax.
ControlMaster
- Lets the SSH client use the session sharing socket if it exists or create it and listen otherwise.
ControlPersist
- Usually, the first client to a given server is in charge of the connection sharing. It will then stay in foreground even if you terminate the session it is used for, until every other clients using its socket has finished, and forcefully terminating it will have the effect of terminating these clients! With that option, the master connection is put in background and will only terminate when no client needs it any more (one second after that, in fact), and you will be able to terminate the first session without kicking the other ones.
Try it! Apply this configuration and connect several times to a single host, to see the difference…
10 comments
friday 17 february 2012 à 00:14 Timo Juhani Lindfors said : #1
friday 17 february 2012 à 02:18 Daniel Kahn Gillmor said : #2
friday 17 february 2012 à 03:01 anonymous said : #3
friday 17 february 2012 à 11:49 rjc said : #4
friday 17 february 2012 à 16:38 Tanguy said : #5
friday 17 february 2012 à 16:38 Tanguy said : #6
sunday 19 february 2012 à 00:37 Marius Gedminas said : #7
monday 20 february 2012 à 09:39 rjc said : #8
friday 24 february 2012 à 18:53 Marius Gedminas said : #9
friday 06 july 2012 à 17:01 Tanguy said : #10