Skip to content

Instantly share code, notes, and snippets.

@jvkersch
Last active March 23, 2024 14:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jvkersch/e7ef80dea675524d332f to your computer and use it in GitHub Desktop.
Save jvkersch/e7ef80dea675524d332f 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. In ~/.ssh/rc, add
if test "$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
@koshatul
Copy link

FYI, I found I only needed to do

set -g update-environment -r

in ~/.tmux.conf so everytime I attach it resets the SSH_AUTH_SOCK from the incoming shell.

@socketbox
Copy link

socketbox commented Nov 14, 2019

usage: set-option [-agosquw] [-t target-session|target-window] option [value]

@koshatul what version of tmux are you using?

@koshatul
Copy link

tmux 2.6 on Ubuntu 18.04

$ tmux -V
tmux 2.6
$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.3 LTS
Release:	18.04
Codename:	bionic

@mdeeks
Copy link

mdeeks commented Mar 25, 2022

FYI I believe your ~/.ssh/rc will not work if you open multiple SSH sessions and close the most recent one. Your symlink would point to a path that is no longer valid.

@Sieboldianus
Copy link

This worked for me. Just note that for byobu, the path to the .tmux.conf is ~/.byobu/.tmux.conf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment