Many years ago I was told about a way to speed up my SSH connections to servers I have recently made a connection to. The idea was that a session would be kept open for a certain amount of time, even when you had closed out of your sessions. I couldn’t remember the name, so I forgot about it until today when I was trying to find out ways to speed up my Ansible connections.

If you’re like me, you may have many SSH connections opened up to the same server. This trick will make it so your first connection to that server is slow, but any following connections to that server will be sped up because you will have an already opened multiplexed connection.

How to Setup

Setting it up is simple. You can either set this in your system-wide ssh_config file at /etc/ssh/ssh_config, or set this on in your own user’s ssh_config file at ~/.ssh/config.

These are the lines I set:

Host *
	IdentitiesOnly	yes
	ControlPath   	~/.ssh/controlmasters/%C
	ControlMaster 	auto
	ControlPersist	60m

ControlPath is where the control socked will be stored. %C will be a hash and not human readable. If you want it human readable, you could do something like %r@%h:%p.

ControlMaster is what lets you create the sessions and handle the control sockets. auto will do it automatically for all hosts, and you can specify autoask if you want to be asked to confirm it.

ControlPersist says how long that session will stay active in the background when you close your session. So if you disconnect, and remember you wanted to do something on that server, you won’t have to wait to establish an entire new connection, you will connect of the session that is sitting in the background.