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

Solution 1:

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
    

Solution 2:

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

Solution 3:

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.

Solution 4:

It happened to me that panes created when connecting via ssh from OS X started asking my passphrase after a while of working ok. I found a way to fix that stealing this line from http://santini.di.unimi.it/extras/ph/my-tmux-setup.html

eval $(tmux show-environment -t [YOUR-SESSION] | grep '^SSH_AUTH_SOCK')

Just run it from the pane that's complaining.