Making SSH Fast

In my day to day work, I frequently need to bounce to various SSH servers to see whats happening or put out a fire. Nothing drives me more insane than having to wait 5-10 seconds for SSH. So I put together the various pieces in one nice package for you. (Note: this is for mac/linux only)

First, update your ~/.ssh/config to be sure that your Host * section has at least this in it:

Host *
    ServerAliveInterval 30
    ServerAliveCountMax 2
    ControlPath ~/.ssh/master-%r@%h:%p
    ControlMaster auto

Second, add this shell script to your ~/bin or wherever you keep your shell scripts:




function makefast {

        CMD="autossh -M 0 -f -- $HOST $SSH_OPTIONS $ADDITIONAL_ARGS"

makefast bamboo@bamboo
makefast gerrit
makefast jira

Edit and add new lines for the servers you connect to. In my above example, I have 3 servers defined “jira”, “gerrit”, and “bamboo”.

If you want to provide additional SSH arguments like port forwarding, just add them after the hostname/username.

Finally, make sure you install autossh.

Cool, what does all of this mean?

  • .ssh/config
    • ServerAliveInterval: 30 – Check that the connection is alive every 30 seconds.
    • ServerAliveCountMax: 2 – If there are 2 consecutive keep alive failures, kill the connection.
    • ControlPath: ~/.ssh/master-%r@%h:%p – The location to save persistent connection information.
    • ControlMaster: auto – If there is a persistent connection, use it. If not, create one.
  • autossh – This is a tool, that spawns SSH for you and if SSH quits for an irregular reason, relaunches ssh.

Update: Added explanation of some of the configuration and commands below.

[ Discuss at Hacker News ]