Server, sockets, clients and groups

tmux · Expert · 12 min

Most 'weird tmux' problems, like stale SSH agents or a config that will not apply, come from the client-server model.

tmux is a client-server program. The first tmux command starts a server daemon that owns all sessions, windows and pane processes. Each tmux invocation, including your attached terminal, is a client talking to it over a Unix socket, by default /tmp/tmux-<uid>/default (directory from $TMUX_TMPDIR).

  • tmux -L name uses a different socket name, giving a completely separate server with its own config load; -S /path gives a full socket path (useful for sharing a session between users via file permissions).
  • $TMUX inside a pane holds the socket path, server pid and session index.
  • kill-server stops everything; the server also exits when its last session closes (option exit-empty).

Several clients can attach to one session; window size follows the window-size option (latest by default since 3.1). A grouped session, tmux new -t main -s side, shares the same windows but keeps its own current window, so two people (or two monitors) can look at different windows of one workspace.

Environment capture: panes inherit the server's environment from when it started. When you reattach from a new SSH login, SSH_AUTH_SOCK and DISPLAY in old panes are stale. tmux copies the variables listed in update-environment into the session environment on attach, but existing shells do not see that; new panes do, and tmux show-environment lets a shell refresh them.

Example

echo "$TMUX"                       # /tmp/tmux-1000/default,12345,0
tmux -L work new -d -s a           # separate server on socket 'work'
tmux -L work ls                    # a: 1 windows ...
tmux ls                            # default server: unaffected
tmux new -d -s main
tmux new -t main -s side           # grouped: same windows, own current window
tmux show-options -g update-environment
# refresh a stale agent socket in an old pane:
eval "$(tmux show-environment -s SSH_AUTH_SOCK)"

Remember

  • One server per socket; -L name or -S path selects another
  • Grouped sessions share windows but not the current window
  • Old panes keep the environment from when they started; use show-environment -s

Watch out for

  • Assuming panes pick up a new SSH_AUTH_SOCK after reattaching; only new panes get the updated value.

Check yourself

  1. After reattaching from a new SSH connection, `git push` in an old pane cannot reach your SSH agent. Why?
  2. What does `tmux -L test` do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: man.openbsd.org/tmux.1#CLIENTS_AND_SESSIONS