Strovent SystemsSign in
Reference

SSH agent lost after reattaching tmux or screen: fix SSH_AUTH_SOCK

After you reconnect and reattach tmux, old panes still point SSH_AUTH_SOCK at the last login's agent socket, now gone. Refresh it, or use a fixed link.

You log in with agent forwarding, start tmux, disconnect, then log in again and reattach. In an old pane, ssh-add -l now says:

Error connecting to agent: No such file or directory

and git pull or ssh to the next host asks for a password or fails with "Permission denied (publickey)". In a new window in the same session, it works.

Why it happens

With agent forwarding, sshd creates a socket for each login and puts its path in SSH_AUTH_SOCK. That socket goes away when the login ends. A shell started inside tmux keeps the environment it started with, so an old pane still names the socket of the login that created it.

tmux helps only partly. Its update-environment option lists variables to copy from the attaching client into the session, and in current versions SSH_AUTH_SOCK is on that list by default. But the new value reaches only windows and panes created after the attach. Shells that are already running are not changed.

To see the value tmux now holds for the session, run tmux show-environment SSH_AUTH_SOCK.

Fix one pane now

In the old pane, run:

eval "$(tmux show-environment -s SSH_AUTH_SOCK)"

-s prints the variable as a shell command, so eval sets it in the current shell. Then ssh-add -l should list your keys. Do this in each old pane that needs the agent, or open a new window instead.

Point every shell at a path that never changes, and have each new login update what that path points to.

On the host, create ~/.ssh/rc. sshd runs this file with sh at every SSH login, before your shell starts:

if [ -S "$SSH_AUTH_SOCK" ]; then
    ln -sf "$SSH_AUTH_SOCK" "$HOME/.ssh/ssh_auth_sock"
fi

Then tell tmux to use the link, in ~/.tmux.conf:

set -g update-environment "DISPLAY KRB5CCNAME SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY"
setenv -g SSH_AUTH_SOCK "$HOME/.ssh/ssh_auth_sock"

The first line is tmux's default list without SSH_AUTH_SOCK (check yours with tmux show -g update-environment), so attaching no longer replaces the link. The second gives every new pane the link. A tmux server started after the change uses it at once. In a session that is already running, run tmux source-file ~/.tmux.conf, then tmux set-environment -u SSH_AUTH_SOCK: the session still holds the value from its last attach, and that value wins over the global one until you remove it. Panes that are already open keep their own value; run export SSH_AUTH_SOCK="$HOME/.ssh/ssh_auth_sock" in them once. For GNU screen, start it with the variable already set: SSH_AUTH_SOCK="$HOME/.ssh/ssh_auth_sock" screen -DR.

Things to know about ~/.ssh/rc:

  • It must not print anything to standard output.
  • When the file exists, sshd no longer adds X11 forwarding cookies with xauth itself. If you use X11 forwarding, add the xauth part shown in the sshd manual.
  • The server can turn it off with PermitUserRC no.
  • The link follows the most recent login with agent forwarding. When that login ends, the link points nowhere until the next one.

Mosh

Mosh does not forward the agent at all, so there is no socket to refresh after a Mosh login. See Mosh and SSH agent forwarding: why ssh -A does not carry over.

Sources

Spotted a mistake? Email [email protected].