Keep your SSH agent socket working in long-running tmux sessions.
When using SSH agent forwarding with tmux:
- You SSH into a machine with agent forwarding (creates
/tmp/ssh-XXXX/agent.YYYY) - Start a tmux session
- Disconnect and reconnect later (new socket created, old one gone)
- tmux still has the old
SSH_AUTH_SOCK, SSH operations fail
ssh-agent-monitor maintains a stable symlink at $XDG_RUNTIME_DIR/ssh-auth-sock that always points to the newest working SSH agent socket. Your shell and tmux sessions reference this symlink, which stays valid across reconnects.
- Linux with systemd
inotify-tools(forinotifywait)openssh-client(forssh-add)
# Clone to recommended location
git clone git@github.com:msch/ssh-agent-monitor.git ~/.local/share/ssh-agent-monitor
cd ~/.local/share/ssh-agent-monitor
# Install (sets up systemd service, ~/.ssh/rc, and shell config)
make installThis will:
- Check that
inotifywaitandssh-addare available - Install and start the systemd user service
- Set up
~/.ssh/rc(creates a symlink to our script, or adds a line to run it if file exists) - Detect your shell and add
SSH_AUTH_SOCKexport to.bashrcor.zshrc(.zshrc.localtakes precedence if it exists)
Then restart your shell or run source ~/.bashrc (or ~/.zshrc, etc.).
- The monitor scans
/tmp/ssh-*directories for agent sockets - Tests each socket with
ssh-add -lto find a working one - Updates the symlink
$XDG_RUNTIME_DIR/ssh-auth-sockatomically - Uses
inotifywaitto watch for socket changes - On new SSH connection,
~/.ssh/rctriggers a rescan by touching the symlink
make uninstallThis stops and disables the systemd service. If ~/.ssh/rc was a symlink to our script, it will be removed. If it was an existing file with a source line added, you'll be prompted to remove that line manually. The shell config modification is left in place for manual cleanup.
MIT