Sharing Terminal Screen Using Tmux

· klm's blog


Original post is here: eklausmeier.goip.de

Sharing your terminal input and output can be done in different ways.

  1. kibitz, see Linux commands: expect and kibitz
  2. VNC
  3. Skype / Teams / Zoom, etc.
  4. tmux

1. tmux essentials. tmux (tmux wiki) uses the following vocabulary and hierarchy.

  1. server
  2. session-group
  3. session
  4. client
  5. window
  6. pane

The tmux-server listens on a socket. The tmux-client talks to this socket. A tmux-server may have 0, 1, ..., n tmux-sessions. A tmux-client connects to a tmux-session. Two or more different tmux-clients may connect to the same tmux-session (that's what this post will lead to). Each tmux-session has 1, ..., n tmux-windows (so at least one). Each tmux-window has 1, ..., n tmux-panes (so at least one). Sessions in the same group share the exact same set of windows, except the current window, i.e., closing or creating new windows in this group affect all sessions in this group.

Below .tmux.conf is my preferred configuration and is in no way required for sharing terminal screens. Your file .tmux.conf can be empty or absent at all.

 1# The default key is C-b because the prototype of tmux was originally
 2# developed inside screen and C-b was chosen not to clash with
 3# the screen meta key.
 4set -g prefix C-a
 5unbind C-b
 6bind C-a send-prefix
 7
 8# Set a  scrollback buffer limit
 9set -g history-limit 100000
10
11# Enable mouse control (clickable windows, panes, resizable panes)
12set -g mouse on
13
14# Enable vi keys for working with buffers
15setw -g mode-keys vi
16# Constrain window only if another session is looking at it
17setw -g aggressive-resize on
18# Always resize windows to the larger/smallest session containing the window
19#setw -g window-size largest
20setw -g window-size smallest
21
22
23new-session -s klm -n gen zsh
24#new-window -t0 -n gen zsh
25new-window -t1 -n bin zsh
26new-window -t2 -n etc zsh
27new-window -t3 -n man zsh
28new-window -t4 -n root zsh
29new-window -t5 -n log zsh
30new-window -t6 -n ssh zsh
31new-window -t7 -n music zsh
32new-window -t8 -n chrome zsh
33new-window -t9 -n Dwn zsh
34split-window -h -t2
35split-window -h -t8
36select-window -t0

Starting tmux is now

1tmux start-server \; attach-session

or in abbreviated form

1tmux start \; a

Below screenshot shows all those 10 windows in action. One of those windows, here number 8, has two panes.

2. Screen sharing. If two different users A and B on a single Linux machine want to share their terminal screen, they have to do the following:

  1. User A: open a tmux using a "special" socket: tmux -S /tmp/userA_socket new-session -s userA_session
  2. User A: making socket accessible to user B: chmod 777 /tmp/userA_socket
  3. User B: access tmux from user A: tmux -S /tmp/userA_socket attach -t userA_session

So two users spawn two clients which connect to the same session managed by one server.

Instead of creating a new socket, /tmp/userA_socket, one can also use the default socket used by tmux, which is /tmp/tmux-/default. If chmod 777 is too risky, then use ACL:

1setfacl -m u:UserB:rwx /tmp/userA_socket

You can achieve a similar effect with GNU screen, although one of the participants needs to setuid-root the screen program. If you do not have root access to the machine in question, then GNU screen is not an option.