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
view raw .sshconfig This Gist brought to you by GitHub.

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

#!/bin/bash

SSH_OPTIONS=" -MNf"

export AUTOSSH_GATETIME=0
export AUTOSSH_PORT=0

function makefast {
        HOST=$1
        shift
ADDITIONAL_ARGS=$@

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

makefast bamboo@bamboo
makefast gerrit
makefast jira
view raw fastssh.sh This Gist brought to you by GitHub.

Edit fastssh.sh 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]