NOTE: This is a re-post from the old blog which you can find here
Quick post, this is an ancient method to speed up ssh connections, I’m just leaving it here.
When we use SSH mostly all the times we create a new connection, even if we are connecting with the same host at the same port with the same user. I’ve been using SSH for a long time and to me this was fine, at the end the connection is “pretty” fast and the delay time creating the connection is just ok.
The problem is when you are editing a file that is in the server, whenever I can I use ssh and with that use vim to edit remote files using “scp”, with this I can test the file in the remote server without worrying to sync the file (rsync/scp) manually. At the end I just write back the file in my hard drive if needed.
This is kind of… annoying, vim+scp creates a new connection every time you want to save the file, and it mean a lot of time when editing a file, more if you are like me, saving the freaking file every time I stop writing. So, how can I improve that?.
Well, it turns out its pretty simple. ssh can create a socket (in a predefined place) when connected to a server, with this socket any other ssh instance for the same server+user+port can use this socket and avoid the connection delay. To enable this just add this lines in your ssh config:
Host * ControlMaster auto ControlPath ~/.ssh/socket/ssh_mux_%h_%p_%r
Just make sure that
~/.ssh/socket/ exists. This will create a socket like
~/.ssh/socket/ssh_mux_localhost_22_markuz and all new connections will use it instead of creating a new connection. Of course, there are some downsides, since the first connection is the only one that is connected, if you loose that connection you’ll disconnect all other ssh instances.You’ll probably do this. So, the way I’ve managed to fix it. Well, it’s kind of simple and of course, it is not magic, just create a master connection whenever you can:
ssh -MNn user@host
Put this somewhere to create it magically (let’s say a script that run in the background), with this you’ll have that ssh connection open, you’ll never have it in the terminal and you’ll not close it until you “kill” that ssh connection.
So, the fix is more like a hack, but works, and helps a lot if you are using a lot of connections to a server.
895 total views, 2 views today