3 ms·
The SSH agent symlink tip will not fully work. If you connect with a second SSH session and then disconnect it will point to a stale SSH_AUTH_SOCK that doesn't
by mdeeks 4y ago
The SSH agent symlink tip will not fully work. If you connect with a second SSH session and then disconnect it will point to a stale SSH_AUTH_SOCK that doesn't work. The only way I found to solve this is by having a prompt command that refreshes it every time your prompt is rendered. So worst case is you have a broken ssh-agent for a single command.
Add this function to your zshrc/bashrc:
# This function refreshes some env vars that go stale in old tmux sessions
# It must be run as a preexec function in zsh or a PROMPT_COMMAND in bash
function refresh_env {
local ssh_auth_sock=""
if [[ -v "TMUX" ]]; then
ssh_auth_sock=$(tmux show-environment | grep "^SSH_AUTH_SOCK")
fi
if [[ -n "$ssh_auth_sock" ]]; then
#shellcheck disable=SC2163
export "$ssh_auth_sock"
fi
}
Then for bash, add this:
if ! [[ "$PROMPT_COMMAND" =~ refresh_env ]]; then
PROMPT_COMMAND="refresh_env; $PROMPT_COMMAND";
fi
Or for zsh, add this:
autoload -U add-zsh-hook
add-zsh-hook preexec refresh_env
- commotionfever 4y agonice I have something similar for fish, just if string match -q -r 'tmux\-\d+' "$TMUX" tmux show-env \ | sed -nE 's/^(SSH_[^=]+)=(.\*)/set -gx \1 "\2"/p' \ | source end in my config.fish. sometimes it goes stale but a quick `exec fish` fixes it, I didn't want more stuff to run on every prompt also both of our solutions are nice I think, no need to edit a tmux or ssh config