Quick Tip: Keeping SSH and SCP sessions active with one login

Feb 5, 2022

Don’t you hate it when you have to reach for 2FA or the password manager again and again when transferring multiple files over scp or opening an extra ssh session because your cluster’s ls command froze again?

Turns out that ssh can keep it alive for you! A ControlMaster feature can be configured in ~/.ssh/config:

Host my.cluster.address
  HostName my.cluster.address
  User myusername
  ControlPath ~/.ssh/controlmasters/%r@%h:%p
  ControlMaster auto
  ControlPersist 20m

Then, mkdir ~/.ssh/controlmasters. You should be able to reuse ssh and scp active sessions within the ControlPersist timeframe.

Hey, my X server forwarding isn’t working now!

The first connect needs to have ssh -X option. Manually delete the file in ~/.ssh/controlmasters and connect again.

Doesn’t this make things less secure?

You already have an open ssh session in your computer to the target machine. If someone has console access to run ssh you have more than this to worry about.

For sanity you can always set ControlPersist to a shorter time so the session is kept alive shorter than the last disconnect.

This is a quick tip post. They are written in a way to help users quickly resolve a problem that I ran into in my daily work and usually don’t go in depth about the details of ‘why’. Please feel free to comment or contribute to the knowledge.