How can I get ssh-agent working over ssh and in tmux (on OS X)?

An elegant solution, picked up from dagit.o:

Create ~/.ssh/rc

#!/bin/bash
if [ -S "$SSH_AUTH_SOCK" ]; then
    ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
fi

Add to ~/.tmux.conf

set -g update-environment "DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY"
set-environment -g 'SSH_AUTH_SOCK' ~/.ssh/ssh_auth_sock

In your .tmux.conf configuration file, add this line:

set -g update-environment "SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION"

This causes these environment variables to be copied from your main shell to any shells opened within tmux, which then allows ssh-agent to work properly within those tmux shells.


My colleague created some bash functions to assist with finding a live agent: https://github.com/wwalker/ssh-find-agent

He uses it mainly for connecting between systems (laptop to desktop, etc), but I use it most often for local tmux sessions where you logout/in from your window manager (OS X for myself).

Usage

  1. Download ssh-find-agent.bash (git clone git://github.com/wwalker/ssh-find-agent.git works).

  2. Add the following to ~/.bashrc:

    . /path/to/ssh-find-agent.bash
    
  3. Then you can type the following to set SSH_AUTH_SOCK in your current shell:

    set_ssh_agent_socket