, 3 min read

Sharing Terminal Screen Using Tmux

Original post is here eklausmeier.goip.de/blog/2020/03-26-sharing-terminal-screen-using-tmux.

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.

# The default key is C-b because the prototype of tmux was originally
# developed inside screen and C-b was chosen not to clash with
# the screen meta key.
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Set a  scrollback buffer limit
set -g history-limit 100000

# Enable mouse control (clickable windows, panes, resizable panes)
set -g mouse on

# Enable vi keys for working with buffers
setw -g mode-keys vi
# Constrain window only if another session is looking at it
setw -g aggressive-resize on
# Always resize windows to the larger/smallest session containing the window
#setw -g window-size largest
setw -g window-size smallest

new-session -s klm -n gen zsh
#new-window -t0 -n gen zsh
new-window -t1 -n bin zsh
new-window -t2 -n etc zsh
new-window -t3 -n man zsh
new-window -t4 -n root zsh
new-window -t5 -n log zsh
new-window -t6 -n ssh zsh
new-window -t7 -n music zsh
new-window -t8 -n chrome zsh
new-window -t9 -n Dwn zsh
split-window -h -t2
split-window -h -t8
select-window -t0

Starting tmux is now

tmux start-server \; attach-session

or in abbreviated form

tmux 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:

setfacl -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.