- linux
- l1
- topic-pack
- tmux-screen
- linux-fundamentals --- Portal | Level: L1: Foundations | Topics: tmux & screen, Linux Fundamentals | Domain: Linux
tmux & screen - Primer¶
Why This Matters¶
Terminal multiplexers solve a problem every ops engineer hits on day one: SSH disconnects kill your running processes. You are halfway through a database migration over SSH, your Wi-Fi drops for three seconds, and the migration dies mid-transaction. tmux and screen decouple your terminal session from the SSH connection, so processes survive disconnects, reboots of your laptop, and network blips.
Beyond survival, multiplexers give you multi-pane workflows — monitoring logs in one pane, running commands in another, tailing metrics in a third — all within a single SSH connection. During incidents, they enable shared sessions where multiple engineers see the same terminal in real time. They are not optional tooling. They are infrastructure.
tmux Architecture¶
Name origin: tmux stands for "terminal multiplexer." It was written by Nicholas Marriott and first released in 2007 as a BSD-licensed alternative to GNU Screen (which is GPL). The name is literally "t(erminal) mux(iplexer)."
tmux uses a client-server model:
┌─────────────────────────────────────────────┐
│ tmux server │
│ (one per user, persists across detaches) │
│ │
│ ┌─────────── Session: "infra" ───────────┐ │
│ │ │ │
│ │ ┌── Window 0: "logs" ──┐ │ │
│ │ │ ┌──────┬──────┐ │ │ │
│ │ │ │Pane 0│Pane 1│ │ │ │
│ │ │ │ tail │ htop │ │ │ │
│ │ │ └──────┴──────┘ │ │ │
│ │ └──────────────────────┘ │ │
│ │ │ │
│ │ ┌── Window 1: "deploy" ─┐ │ │
│ │ │ ┌──────────────┐ │ │ │
│ │ │ │ Pane 0 │ │ │ │
│ │ │ │ shell │ │ │ │
│ │ │ └──────────────┘ │ │ │
│ │ └───────────────────────┘ │ │
│ └────────────────────────────────────────┘ │
│ │
│ ┌─────────── Session: "dev" ─────────────┐ │
│ │ ... │ │
│ └────────────────────────────────────────┘ │
└─────────────────────────────────────────────┘
│ │ │
Client 1 Client 2 Client 3
(SSH #1) (SSH #2) (local term)
- Server: one per user, runs in the background, owns all sessions
- Session: a collection of windows; what you attach/detach from
- Window: a full-screen tab within a session (like browser tabs)
- Pane: a split within a window (horizontal or vertical)
The server persists even when no clients are attached. Your processes keep running.
Essential tmux Commands¶
Session Management¶
# Start a new named session
tmux new-session -s infra
# Detach from current session
# (inside tmux) Ctrl-b d
# List sessions
tmux list-sessions
# or shorthand:
tmux ls
# Attach to a named session
tmux attach -t infra
# Attach to the most recent session
tmux attach
# Kill a session
tmux kill-session -t infra
# Kill the tmux server (all sessions)
tmux kill-server
# Rename current session
# (inside tmux) Ctrl-b $
Window Management¶
# Create a new window
# Ctrl-b c
# Switch to next/previous window
# Ctrl-b n (next)
# Ctrl-b p (previous)
# Switch to window by number
# Ctrl-b 0 (window 0)
# Ctrl-b 1 (window 1)
# Rename current window
# Ctrl-b ,
# Close current window
# Ctrl-b & (confirms before closing)
# List windows interactively
# Ctrl-b w
Pane Management¶
# Split horizontally (top/bottom)
# Ctrl-b "
# Split vertically (left/right)
# Ctrl-b %
# Navigate between panes
# Ctrl-b <arrow key>
# Resize pane
# Ctrl-b Ctrl-<arrow key> (resize in arrow direction)
# Toggle pane zoom (fullscreen a pane, toggle back)
# Ctrl-b z
# Close current pane
# Ctrl-b x (confirms)
# Swap panes
# Ctrl-b { (swap with previous)
# Ctrl-b } (swap with next)
# Show pane numbers (then press number to jump)
# Ctrl-b q
tmux.conf Configuration¶
tmux is configured via ~/.tmux.conf. Changes take effect on reload:
# Reload config without restarting tmux
tmux source-file ~/.tmux.conf
# or inside tmux:
# Ctrl-b :source-file ~/.tmux.conf
Production-Ready tmux.conf¶
# ~/.tmux.conf
# --- Prefix key ---
# Change prefix from Ctrl-b to Ctrl-a (closer to home row, matches screen)
# Remember: Ctrl-a is "screen muscle memory" — most ops people remap to this
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# --- General settings ---
set -g default-terminal "screen-256color"
set -g history-limit 50000
set -g display-time 4000
set -g status-interval 5
set -g focus-events on
# Start window/pane numbering at 1 (0 is far from prefix key)
set -g base-index 1
setw -g pane-base-index 1
# Renumber windows when one is closed
set -g renumber-windows on
# --- Mouse mode ---
set -g mouse on
# --- Vi mode for copy ---
setw -g mode-keys vi
# Vi-style copy bindings
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"
bind -T copy-mode-vi r send-keys -X rectangle-toggle
# --- Pane splitting (intuitive keys) ---
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %
# New windows open in current directory
bind c new-window -c "#{pane_current_path}"
# --- Pane navigation (vim-style) ---
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# --- Pane resizing ---
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5
# --- Status bar ---
set -g status-position bottom
set -g status-style 'bg=colour234 fg=colour137'
set -g status-left '#[fg=colour233,bg=colour245,bold] #S '
set -g status-right '#[fg=colour233,bg=colour241,bold] %H:%M:%S '
set -g status-left-length 20
set -g status-right-length 50
# Active window
setw -g window-status-current-style 'fg=colour81 bg=colour238 bold'
setw -g window-status-current-format ' #I:#W#F '
# Inactive window
setw -g window-status-style 'fg=colour248 bg=colour235'
setw -g window-status-format ' #I:#W#F '
# Pane borders
set -g pane-border-style 'fg=colour238'
set -g pane-active-border-style 'fg=colour81'
Copy Mode¶
tmux has a built-in copy mode for scrolling and copying text:
# Enter copy mode
# Ctrl-b [
# In copy mode (vi keys):
# h/j/k/l move cursor
# Ctrl-u/d page up/down
# / search forward
# ? search backward
# n/N next/previous search match
# Space start selection
# Enter copy selection and exit
# q exit copy mode
# Paste buffer
# Ctrl-b ]
# List all buffers
tmux list-buffers
# Save buffer to file
tmux save-buffer ~/tmux-capture.txt
tmux send-keys for Automation¶
send-keys lets you script tmux from outside — send keystrokes to panes programmatically:
# Send a command to a specific pane
tmux send-keys -t infra:0.0 "tail -f /var/log/syslog" Enter
# Send Ctrl-C to stop a process
tmux send-keys -t infra:0.1 C-c
# Send to a named session, window, pane
tmux send-keys -t mysession:mywindow.2 "kubectl get pods" Enter
tmux capture-pane for Logging¶
Capture pane contents for postmortems or audit trails:
# Capture visible pane content
tmux capture-pane -t infra:0.0 -p > ~/capture.txt
# Capture entire scrollback history
tmux capture-pane -t infra:0.0 -p -S - > ~/full-scrollback.txt
# Capture with ANSI escape codes (for color)
tmux capture-pane -t infra:0.0 -p -e -S - > ~/capture-color.txt
# Enable automatic logging (pipe pane output to file)
tmux pipe-pane -t infra:0.0 "cat >> ~/pane-log.txt"
# Stop logging
tmux pipe-pane -t infra:0.0 ""
tmux Plugins¶
The tmux Plugin Manager (tpm) provides plugin management:
# Install tpm
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# Add to ~/.tmux.conf:
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect' # Save/restore sessions across reboots
set -g @plugin 'tmux-plugins/tmux-continuum' # Automatic session saving
set -g @plugin 'tmux-plugins/tmux-yank' # Clipboard integration
# tmux-resurrect settings
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-nvim 'session'
# tmux-continuum settings
set -g @continuum-restore 'on' # Auto-restore on tmux start
set -g @continuum-save-interval '15' # Save every 15 minutes
# Initialize tpm (keep at bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
# Install plugins (inside tmux):
# Ctrl-b I (capital I — installs plugins)
# Update plugins:
# Ctrl-b U
screen Basics¶
Gotcha: If you are inside tmux and SSH to a remote host, then start tmux there, your prefix key will be intercepted by the local tmux. To send the prefix to the remote tmux, press the prefix twice (e.g.,
Ctrl-b Ctrl-b dto detach the remote session). Some people remap the remote tmux prefix toCtrl-ato avoid this collision.
screen is the older multiplexer. It is installed on virtually every Linux system, including minimal server images where tmux may not be available. Know the basics for emergencies.
Essential screen Commands¶
# Start a new named session
screen -S infra
# Detach from current session
# Ctrl-a d
# List sessions
screen -ls
# Reattach to a session
screen -r infra
# Force detach remote and reattach here
screen -dr infra
# Create a new window
# Ctrl-a c
# Switch windows
# Ctrl-a n (next)
# Ctrl-a p (previous)
# Ctrl-a 0 (window 0)
# Split horizontally
# Ctrl-a S
# Split vertically
# Ctrl-a |
# Switch between split regions
# Ctrl-a Tab
# Close current region
# Ctrl-a X
# Scrollback mode
# Ctrl-a [ (then use arrow keys or PgUp/PgDn)
# Esc or q to exit scrollback
# Kill current window
# Ctrl-a k
# Quit screen session entirely
# Ctrl-a \
Minimal .screenrc¶
# ~/.screenrc
# Disable splash screen
startup_message off
# Scrollback buffer
defscrollback 10000
# Status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m/%d %{W}%c %{g}]'
# Fix for modern terminals
term screen-256color
# Enable altscreen (clears screen when leaving vim/less)
altscreen on
tmux vs screen Comparison¶
| Feature | tmux | screen |
|---|---|---|
| Pane splits | Native, first-class | Bolt-on, clunky |
| Scripting | Excellent (send-keys, capture-pane) | Limited |
| Config syntax | Clean, modern | Arcane |
| Default availability | Usually installed | Almost always installed |
| Session sharing | Built-in (multiple clients attach) | Requires multiuser mode |
| Copy mode | Vi or Emacs keybindings | Scrollback mode |
| Plugin ecosystem | tpm, resurrect, continuum | Minimal |
| Status bar | Highly customizable | Basic |
| Mouse support | Good (set -g mouse on) | Limited |
| Active development | Yes | Maintenance mode |
Use tmux when you can. Fall back to screen on systems where tmux is not installed and you cannot install it.
One-liner: Quick tmux session that survives disconnect:
tmux new -s work -d && tmux send-keys -t work 'tail -f /var/log/syslog' Enter && tmux attach -t work— creates a detached session, starts a command in it, then attaches you.
Key Takeaway¶
tmux is the standard terminal multiplexer for ops work. It keeps processes alive across SSH disconnects, provides multi-pane layouts for monitoring, and enables shared sessions for pair debugging during incidents. Learn the prefix key (Ctrl-b default, Ctrl-a recommended), pane splits, and session management. Keep screen basics in your back pocket for minimal systems. The configuration in this primer is production-tested — copy it, adjust to taste, and never lose a long-running process to a dropped connection again.
Wiki Navigation¶
Related Content¶
- /proc Filesystem (Topic Pack, L2) — Linux Fundamentals
- Advanced Bash for Ops (Topic Pack, L1) — Linux Fundamentals
- Adversarial Interview Gauntlet (30 sequences) (Scenario, L2) — Linux Fundamentals
- Bash Exercises (Quest Ladder) (CLI) (Exercise Set, L0) — Linux Fundamentals
- Case Study: CI Pipeline Fails — Docker Layer Cache Corruption (Case Study, L2) — Linux Fundamentals
- Case Study: Container Vuln Scanner False Positive Blocks Deploy (Case Study, L2) — Linux Fundamentals
- Case Study: Disk Full Root Services Down (Case Study, L1) — Linux Fundamentals
- Case Study: Disk Full — Runaway Logs, Fix Is Loki Retention (Case Study, L2) — Linux Fundamentals
- Case Study: HPA Flapping — Metrics Server Clock Skew, Fix Is NTP (Case Study, L2) — Linux Fundamentals
- Case Study: Inode Exhaustion (Case Study, L1) — Linux Fundamentals