Collaborative tmux sessions
Sometimes you want another pair of eyes when troubleshooting or working in production machines. The tools screen and tmux are being used to create collaborative terminal sessions.
tldr;
Create tmux session:
tmux new -s session_name
Join tmux session:
tmux attach -t session_name
You can use the -r flag for read-only connections:
tmux attach -rt session_name
Create sessions with fixed size
It could happen that different terminal sizes are used (old hardware, mobile, big fonts for better readability). By default, tmux will fall back to the window size of the smallest connected client. The behavior can be changed by setting the the window-size option. Possible options are largest, smallest, manual, latest. When creating a new session, this option can be set to manual to prevent unwanted changes of the window size:
tmux new -s session_name \; setw window-size manual \; attach
Join tmux session is the same:
tmux attach -t session_name
Share session between users
It is possible to share tmux session between different users.
Share session using sockets
By default tmux will create a socket in /tmp/tmux-<user_id>/default. It is also possible to explicitly use a different socket using the -S option:
tmux -S /path/to/socket new -s session_name
To connect to this session, grant the user which wants to connect read and write access to the socket and then use:
tmux -S /path/to/socket attach
This method is useful when you don't want to use root (see below). Use this method with caution if you are in a shared environment with different user privileges. Users may use this to escalate privileges.
Note: More recent versions of tmux (available in Debian bookworm and later) include their own access control mechanism. In addition to granting read and write access to socket itself, you will need to use the tmux server-access command to grant access. For example, from within the session you intend to share:
tmux server-access -a $USER_TO_GRANT
You can also use -r to grant read-only access. See man tmux for more details.
"Share" session using root/sudo
It is also possible to create a tmux session as root and let others join by using sudo/root:
sudo -i tmux new -s session_name
Join the tmux session:
sudo -i tmux attach -t session_name
Keep in mind that you are using the root user here. So for read-only operations and debugging you may want to use your personal user and share the session using sockets or su back to a user with less privileges.