Fix SSH agent in reattached tmux session shells
A new remote logon session creates a different SSHAUTHSOCK and friends. This can be a problem if existing shells in a detached tmux session retain the old ssh-agent environment variables; github pushes and pulls won't work, ssh to other machines will have trouble, etc
Add this function to your shell (works for zsh, I think it will work in bash), and run "fixssh" in each shell that needs to communicate with ssh-agent:
fixssh() {
for key in SSH_AUTH_SOCK SSH_CONNECTION SSH_CLIENT; do
if (tmux show-environment | grep "^${key}" > /dev/null); then
value=`tmux show-environment | grep "^${key}" | sed -e "s/^[A-Z_]*=//"`
export ${key}="${value}"
fi
done
}
EDIT, much later: The following gist lays out a more automatic way of solving this problem: https://gist.github.com/martijnvermaat/8070533
Written by Mike Bloy
Related protips
1 Response
Thank you Mike, you kinda solved my problem, now the last part to solve is how to auto-repair this without changing anything on the remote host?
On my computer I customized SSH so it does always try to restore the last tmux session when I ssh a machine - works pretty well but as you noted bad things happen.
I do not want to have to "prepare" all machines so I need a solution from the client SSH client... it seems that the connect code will become more complex now..