Tmux Client‑Server Socket Architecture: Requirements, Boundaries, and Failure Modes
An architecture note explaining how tmux uses a per‑user Unix domain socket to enable multiple clients to share a session while preserving isolation and describing what can go wrong.
21 Jul 2025, 04:19 UTC

Problem: Sharing a tmux session safely
Users often need more than one terminal attached to the same tmux session—for example, a developer debugging in one pane while a colleague watches the output in another. The requirement is that each attachment sees independent input and output, can detach and re‑attach later, and that no other user can inject keystrokes or read the session data.
Requirements
- Multiple clients (processes) must be able to connect to the same session simultaneously.
- Each client must have its own input stream (keystrokes) and output stream (pane rendering).
- Detached clients must keep the session alive; re‑attachment must restore the exact state.
- The mechanism must work without network configuration, relying only on local system primitives.
- Security: only the owning user may attach or interfere with the session.
Smallest suitable design
The tmux implementation satisfies the above with a single Unix domain socket per user. When tmux starts, it creates a server process that binds to a socket file located in $TMUX_TMPDIR (default /tmp/tmux-<UID>). The socket name incorporates the server‑instance identifier (e.g., default) so a user can run multiple independent tmux servers if desired.
Each tmux client (the command you run in a terminal) connects to that socket, sends a request to attach to a session, and then engages in a bidirectional protocol:
- The server serializes the current session state (window layout, pane contents, options) and streams it to the newly attached client.
- Keystrokes from the client are forwarded to the server, which injects them into the appropriate pane’s input queue.
- Screen updates generated by the server are sent back to the client for rendering.
Because the socket is a file system object, the kernel enforces access based on the file’s owner, group, and mode.
Trust and data boundary
The trust boundary is the socket file’s filesystem permissions. The server creates the socket with mode 600 (read/write for owner only) or, if the user sets set -g socket-group to a specific group, mode 660. Consequently:
- Only processes running with the same UID (or a permitted GID) can open the socket for read/write.
- The server does not perform any additional authentication; it trusts that any entity able to open the socket is the legitimate user.
- All data exchanged over the socket—session state, keystrokes, and screen updates—is considered confidential to that user. No encryption is applied because the socket resides in a directory only the user can access.
Operational checks
Administrators and users can verify the health of the socket model with built‑in tmux commands:
tmux list-socketsshows each socket file tmux has created, its path, and the server’s PID.tmux list-clients -t <session>lists attached clients (their TTY and PID) for a given session.- The server logs attachment and detachment events to
~/.tmux/logwhen logging is enabled (set -g debug-level on).
These commands require no special privileges; they merely read the socket directory and query the server via the same socket mechanism.
Failure modes
- Socket file corruption or deletion – If the socket file is removed while a session is active, the server detects the loss of its listening endpoint, terminates, and all attached clients receive a "socket closed" error. The session state is lost unless
remain-on-exitis enabled, in which casetmux rescue-sessioncan recover the session from the server’s internal state file. - Server crash – A segmentation fault or kill signal ends the server process, detaching every client. Because the socket disappears, clients cannot reconnect until a new server is started.
- Stale socket on network filesystems – When the socket directory resides on NFS with delayed attribute caching, a removed socket may still appear present. A subsequent tmux startup can fail with "address already in use" even though the original server is gone. The workaround is to place the socket on a local filesystem (e.g.,
/tmp) or to clear NFS caches. - Permission drift – If the socket’s mode is changed to something more permissive (e.g.,
666) or its ownership altered, other users could attach and inject keystrokes, violating the trust boundary.
When the design would need to change
The current design assumes a single‑user trust model based on filesystem permissions. Two scenarios would motivate a redesign:
- Multi‑user shared sessions – If administrators wanted to allow different UIDs to attach to the same tmux session (e.g., for pair programming across accounts), the socket‑based UID check would be insufficient. The design would need an explicit authentication mechanism, such as a token passed in the environment or a TLS layer, and the server would have to validate that token before granting access.
- Transport encryption – Should the socket be exposed over a network (e.g., forwarding tmux via SSH tunnels), the lack of encryption would be a concern. Replacing Unix domain sockets with a TCP‑based transport secured by TLS would protect session data in transit, but would also introduce connection‑management complexity and require handling of network partitions.
Practical verification steps
To confirm that the socket model works as described, perform the following checks on a Linux workstation where you have a regular user account (no root needed).
- Identify the socket
tmux show -v socket-path
This prints something like/tmp/tmux-1000/default. Verify the file’s ownership and mode:ls -l $(tmux show -v socket-path)
Expected output:-rw------- 1 youruser yourgroup 0 Sep 26 10:12 /tmp/tmux-1000/default(or-rw-rw----if a group is set). - Attach two clients
Open two terminal windows. In the first, start a session if none exists:
tmux new -s demo
In the second, attach to the same session:tmux attach -t demo
Now type a few characters in either pane; you should see them appear in both panes because the server broadcasts keystrokes to all attached windows. - Detach and re‑attach
In one client, press
Ctrl‑b dto detach. The session remains running on the server. In the other client (or a new terminal), re‑attach:tmux attach -t demo
You should see the session exactly as you left it, confirming that detachment does not destroy state. - Simulate socket loss
While a session is attached in both terminals, open a third terminal and run:
rm $(tmux show -v socket-path)
Observe that both attached clients immediately display an error like "socket closed" and the tmux server process disappears (check withps -ef | grep tmux). This demonstrates the server’s liveness dependency on the socket.
Limitations
- The model is inherently local; sharing a session across machines requires forwarding the socket (e.g., via SSH
-R) which adds latency and depends on the reliability of the forwarded channel. - Because authentication relies solely on filesystem permissions, any compromise of the user’s account (or accidental permission loosening) can lead to session hijacking.
- The server does not encrypt data on the socket; if the socket directory were exposed (e.g., via a misconfigured bind mount), an attacker with read access could sniff keystrokes.
Conclusion
Tmux’s client‑server architecture satisfies the core need for multi‑client session sharing through a minimal, per‑user Unix domain socket. The socket’s ownership and mode form the trust boundary, enabling the server to rely on the kernel for access control while keeping the implementation simple. Operational visibility is provided by built‑in list commands, and failure modes are well understood: socket loss or server crash detaches all clients, and network‑filesystem quirks can produce spurious errors. Should the requirement evolve to support cross‑user sharing or encrypted transport, the design would need to replace the permission‑based trust model with explicit authentication and possibly a different transport layer. Until then, the socket‑based approach remains a robust, low‑overhead solution for local session multiplexing.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.