Skip to content

Instantly share code, notes, and snippets.

@tongphe
Forked from jvkersch/ssh_auth_sock.md
Last active August 20, 2018 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tongphe/23f3f9f9ae896cdeca8ecdf5dc179e20 to your computer and use it in GitHub Desktop.
Save tongphe/23f3f9f9ae896cdeca8ecdf5dc179e20 to your computer and use it in GitHub Desktop.
Updating SSH_AUTH_SOCK in an already established tmux session

I found this information somewhere on StackOverflow but I forgot exactly where. I'm paraphrasing what I learned here, for future reference.

SSH uses a Unix socket to communicate with other processes. The socket's path can be found by looking at the environment variable $SSH_AUTH_SOCK. When you re-connect to a tmux session that was started during a previous SSH session, this variable will contain the path of the previous SSH auth socket, and this will cause processes that try to connect to your authentication agent to fail. To fix this, we have to

  1. Create a symlink from the auth socket to a fixed path somewhere, so that we can refer to it later on, and not to modify the symlink if current is still alive. In ~/.ssh/rc, add
if [ ! -S ~/.ssh/ssh_auth_sock ] && [ -S "$SSH_AUTH_SOCK" ]; then
    ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
fi
  1. On attaching to a tmux session, set SSH_AUTH_SOCK to the path we just created. In ~/.tmux.conf, add
setenv -g SSH_AUTH_SOCK $HOME/.ssh/ssh_auth_sock
set -g update-environment -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment