Last active
February 10, 2022 22:06
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsetenv SSH_AUTH_SOCK | |
setenv SSH_AUTH_SOCK $HOME/.ssh/ssh-auth-sock.$HOSTNAME |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
~/.profile:
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.