Skip to content

Instantly share code, notes, and snippets.

@admackin
Last active February 10, 2022 22:06
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save admackin/4507371 to your computer and use it in GitHub Desktop.
Save admackin/4507371 to your computer and use it in GitHub Desktop.
Sane SSH_AUTH_SOCK handling for Screen and Tmux, so that new SSH agents created by subsequent logons are still usable.
_ssh_auth_save() {
ln -sf "$SSH_AUTH_SOCK" "$HOME/.ssh/ssh-auth-sock.$HOSTNAME"
}
alias screen='_ssh_auth_save ; export HOSTNAME=$(hostname) ; screen'
alias tmux='_ssh_auth_save ; export HOSTNAME=$(hostname) ; tmux'
unsetenv SSH_AUTH_SOCK
setenv SSH_AUTH_SOCK $HOME/.ssh/ssh-auth-sock.$HOSTNAME
set -g update-environment "DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY"
set-environment -g SSH_AUTH_SOCK $HOME/.ssh/ssh-auth-sock.$HOSTNAME
@bcomnes
Copy link

bcomnes commented Feb 22, 2015

Solid!

@wilb
Copy link

wilb commented May 7, 2015

Lovely job.

@InfernoZeus
Copy link

Very clever, thanks!

@hginzel
Copy link

hginzel commented Jan 8, 2016

@admackin
Copy link
Author

Unfortunately tmux's update-environment still doesn't suffice here as it doesn't seem to work for already open sessions. Another option is shown at https://babushk.in/posts/renew-environment-tmux.html

@neur0manc
Copy link

This didn't work for me (in ZSH) until I rewrote

alias tmux='_ssh_auth_save ; export HOSTNAME=$(hostname) ; tmux'

to

alias tmux='export HOSTNAME=$(hostname) ; _ssh_auth_save ; tmux'

Thanks for sharing this.

@jribbens
Copy link

The above didn't work for me as I'm not running tmux from a shell, but the following is a way of achieving something very similar and should work the same with tmux/screen/whatever:

~/.ssh/rc:

[ -S "$SSH_AUTH_SOCK" -a -z "${SSH_AUTH_SOCK##/tmp/ssh-*}" ] &&
    ln -fs $SSH_AUTH_SOCK $HOME/.ssh/auth_sock

~/.profile:

[ -S $HOME/.ssh/auth_sock ] && export SSH_AUTH_SOCK=$HOME/.ssh/auth_sock

If your system puts the agent socket somewhere other than /tmp/ssh-* then you'd need to change the pattern after the ## in the rc file. If your home directory is shared across multiple hosts then I guess you'd need to add $HOSTNAME to the .ssh/auth-sock filename as above, but that's not POSIX-compatible so I haven't done it here.

All of these methods presumably have the problem that if you have one ssh connection, then connect another, then close the second one, your auth_sock symlink will no longer be pointing anywhere useful, but I don't think there's any completely-satisfactory solution to this that would cope with all scenarios (e.g. imagine a tmux window is moved between sessions; what would be 'the right thing' to happen?) It works fine for my scenario whereby I'm basically only ever connecting to a host using one ssh connection at once, which is after all pretty much the point of tmux/screen/etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment