Environment and Path Issues
Tmux sessions can have out-of-date or wrong environment variables, especially for SSH agents, PATH, and display variables. This lesson covers the root causes and fixes.
When you detach and reattach to a tmux session hours later, the environment variables captured at session creation may be stale. A new login may have a different SSH_AUTH_SOCK, DISPLAY, or PATH.
Diagnosing the Problem
# Inside tmux pane — check current environment
env | grep SSH
env | grep DISPLAY
echo $PATH
# Compare with outside tmux
tmux show-environment
Issue: SSH Agent Not Working (ssh-add fails)
Symptom: ssh commands fail with no auth socket or Could not open connection to authentication agent.
Root cause: The SSH_AUTH_SOCK variable in the tmux session points to an old socket from a previous login.
Fix 1: Update environment on reattach
# Before attaching, force update the environment
tmux attach -t work
# then inside the session:
eval $(tmux show-environment -s SSH_AUTH_SOCK)
Fix 2: Automatic via tmux config
# Update SSH_AUTH_SOCK on each new pane
set -g update-environment "SSH_AUTH_SOCK SSH_CONNECTION SSH_CLIENT"
Fix 3: Symlink the agent socket (most robust)
# Create a stable symlink that always points to the current agent socket
if [[ -n "$SSH_AUTH_SOCK" ]]; then
ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
fi
Then in ~/.tmux.conf:
set-environment -g SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
Issue: Wrong PATH Inside Tmux
Symptom: Commands work in a normal shell but not in a tmux pane.
Root cause: Tmux captures PATH at session creation, not at window/pane creation. If you modify PATH in a shell profile that runs for interactive shells only, it may not apply correctly.
Fix:
# Explicitly set PATH in tmux sessions
set-environment -g PATH "/usr/local/bin:/usr/bin:/bin:/usr/local/sbin"
Or ensure your PATH is set in ~/.bashrc (not just ~/.bash_profile), since tmux creates interactive shells.
Issue: DISPLAY Variable Missing (X11 Forwarding)
Symptom: X11 apps fail inside tmux even though SSH forwarding is on.
Root cause: DISPLAY was set when you first connected, but the tmux session doesn't update it on reattach.
Fix:
set -g update-environment "DISPLAY SSH_AUTH_SOCK XAUTHORITY"
Then update the variable after reattach:
eval $(tmux show-environment -s DISPLAY)
Issue: Tmux Default Shell is Wrong
Symptom: New windows open in /bin/sh instead of your preferred shell.
Fix:
# Explicitly set the shell
set -g default-shell /bin/zsh
# or
set -g default-shell /bin/bash
Checking Tmux Environment Variables
# Show all tmux environment variables
tmux show-environment
# Show a specific variable
tmux show-environment SSH_AUTH_SOCK
# Set a variable in the running session
tmux set-environment SSH_AUTH_SOCK "$SSH_AUTH_SOCK"
# Remove a variable
tmux set-environment -u OLDVAR
Quick Checklist After Reattach
#!/bin/bash
# Run this inside a tmux pane after reattaching to refresh key variables
eval $(tmux show-environment -s SSH_AUTH_SOCK)
eval $(tmux show-environment -s DISPLAY 2>/dev/null)
echo "Environment refreshed"