Terminal multiplexer minimal tutorial

utils productivity
Written on August 30, 2019

Terminal multiplexer is a software that can multiplex several separate pseudoterminal-based login sessions inside one terminal.

Main features:

  1. Persistence - similar to nohup, permits to disconnect and reconnect without terminating running processes
  2. Multiple windows - split-screens, several windows to have several terminals on one screen.
  3. Session sharing - multiple users can connect to the same session.

Let’s consider one popular Terminal Multeplexer - tmux.

Installation

sudo apt-get install tmux

Basic abstractions

  • Session - aggregate and controls several terminals and processes
  • Window - can be split into several panes, a session can have multiple windows.
  • Pane - window can be divided into panes
  • Control key - every control command will look like *PREFIX KEY* + *COMMAND KEY* where PREFIX KEY is Ctrl+B by default. In this tutorial, I will use C-b instead of Ctrl+B

Basic functionalities

Here are some basic functionalities:

  • when ssh is disconnected session will persist
  • sessions for different tasks
  • connect to the shell of different user

Panes

C-b + % = split left/right

C-b + " = split up / down

C-b + left/right = go to prev/next pane

exit or C-d = close pane

Windows

C-b + c = new window

C-b + p/n = go to prev/next window

C-b + <number> = go to window <number>

Sessions

C-b + d = detach session

tmux ls = list of sessions

tmux attach -t 0 = attach to session 0

tmux new -s database = create session named “database”

Mouse control

Add this to .tmux.conf

# Enable mouse control (clickable windows, panes, resizable panes)
set -g mouse-select-window on
set -g mouse-select-pane on
set -g mouse-resize-pane on

# Enable mouse mode (tmux 2.1 and above)
set -g mouse on

Shared sessions

tmux can also create shared sessions and different users can attach to this session at the same time.

Create

tmux -S /tmp/socket_name new -s session_name

Modify socket permissions to get access for other users in group tmux

chgrp tmux /tmp/socket_name

Attach

tmux -S /tmp/socket_name attach -t session_name

Attach Read-only

tmux -S /tmp/socket_name attach -t session_name -r

? Problem: two users connected to a shared session will use the same session. Active pane and window are connected to the session, so they won’t be able to look at different windows at the same time, and type in different panes simultaneously.

! Solution: grouped sessions

Grouped sessions

grouped sessions allow to share windows and panes between sessions and work in different windows and panes.

tmux new-session -s grouped_session
tmux new-session -t grouped_session -s group_subsession

Usage optimized

Here we will discuss some configurations and best practices to foster usage of tmux and make it more transparent and easier

Some hack to organize multiuser interaction with shared sessions

Shared sessions are implemented with unix sockets, and there is no functionality to look at all shared sessions. To organize shared sessions on the server for a small team we can do the following:

sudo adgroup tmux 

sudo usermod -a -G tmux <USERS>  
sudo mkdir - mode=u+rwx,g+rws,o-rwx /var/tmux_sessions
sudo chown user:tmux /var/tmux_sessions

Where <USERS> means add all necessary users manually or list all users with other utils.

Then we can add some sh-scripts to start, attach and list shared sessions.

#!/bin/sh
SESSION_NAME=$1
tmux -S /var/tmux_sessions/$SESSION_NAME attach -t $SESSION_NAME
#!/bin/bash
ls -l /var/tmux_sessions/ | grep ^s | awk '{print substr($9, 1, length($9)-1) " (" $3 ")" }'
#!/bin/sh
SESSION_NAME=$1
tmux -S /var/tmux_sessions/$SESSION_NAME new -s $SESSION_NAME
chgrp tmux /var/tmux_sessions/$SESSION_NAME

wemux - some wrapper on top of tmux for multiuser access (TODO: check if it works on Ubuntu)

  1. tmux cheatsheet