Millet Porridge

English version of https://corvo.myseu.cn

0%

OpenSSH Series 4: Reuse ssh connection

multiplexing or keepalive

When we talk about the HTTP protocol, some users could misunderstand multiplexing and keepalive. While the keepalive means you can handle many requests in one connection, and it’s need FIFO. The multiplexing seems an improvement for keepalive, one connection also holds many requests, but don’t need FIFO.

If you connect the remote server frequently, like git push many times, the multiplexing could help to save your time. Because it doesn’t need to establish a new TCP connection every time.

Configuration

Well, the OpenSSH supports multiplexing, and clients could share the connection with the Unix socket. Just add the Controlpath under the old configuration.

1
2
3
4
5
6
7
8
9
Host ali
HostName 1.2.3.4
Port 22
User root
IdentityFile ~/.ssh/id_rsa_test

Controlmaster auto
Controlpath ~/.ssh/ssh-%r@%h:%p.sock
ControlPersist 600

When you firstly connect the server, a Unix socket file is automatically created in your ~/.ssh directory.

1
2
3
~ ❤  ls ~/.ssh
...
[email protected]:22.sock

This file could be used by other ssh clients who want to connect the same server.

ControlPersist: Please be noted that if the initial client closed, other clients will be closed after that time(600s).

Since we use sshfs, we could combine it with OpenSSH multiplexing. By simply mounting a path, we don’t need to open a new console to stay connected.