-
-
Save admackin/4507371 to your computer and use it in GitHub Desktop.
_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 |
Lovely job.
Very clever, thanks!
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
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.
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.
Solid!