SSH session caching with ControlMaster

Today I learned that the OpenSSH client supports maintaining SSH connections in background even after closing the current active terminal session. I found out about this while researching the use of a YubiKey for OpenSSH authentication and private-key management. Every time I connect to a remote host with a key stored on the YubiKey I’m prompted to press the button on the key to authorize the keys usage by the OpenSSH agent. This is inconvenient so I looked into caching opened connections for some time. Here is how to do this.

Configure a persistent session to an SSH remote through the connection control process feature. Configure use of a control master to persist opened connections for a certain amount of time in ~/.ssh/config:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/S.%r@%h:%p
    ControlPersist 5m

New connections will spawn a file in ~/.ssh and kept open for 5 minutes.

Check if there is an active session:

ssh -O check user@remote

Close an active session:

ssh -O exit user@remote

Any thoughts of your own?

Feel free to raise a discussion with me on Mastodon or drop me an email.