tmux and screen are terminal multiplexers — they allow multiple terminals in one window and survive SSH disconnections. When an SSH connection drops, processes in a tmux/screen session continue running. After reconnecting, you simply return to your work in progress. For system administrators and developers working with remote servers, this is an indispensable tool.
tmux¶
tmux new -s work # new session named work
tmux attach -t work # attach to existing session
tmux ls # list sessions
# Ctrl+b d=detach, c=new window, %=vertical split, "=horizontal split, z=zoom pane
tmux supports any number of windows and panes within a session. Panes can be moved, resized, and switched between using keyboard shortcuts. Windows work like tabs — switch between them with Ctrl+b number. A key strength of tmux is scriptability — you can automatically create layouts with specific commands in each pane.
Configuration¶
# ~/.tmux.conf
set -g prefix C-a # change prefix from Ctrl+b to Ctrl+a
set -g mouse on # mouse support
set -g base-index 1 # number windows from 1
set -g history-limit 50000 # larger scroll buffer
set -g default-terminal "screen-256color"
bind | split-window -h # | for vertical split
bind - split-window -v # - for horizontal split
screen¶
screen -S work # new session
screen -r work # attach
# Ctrl+a d=detach, c=window, n/p=next/previous
Screen is an older alternative, still present on most systems. It has simpler configuration but fewer features than tmux. For basic session persistence it is fully adequate, but for advanced workflows with panes and scripting, tmux is the better choice.
tmux for everyday¶
Protection against SSH outage is just the beginning. Set up tmux as your default environment on every server — 10 keyboard shortcuts are enough to significantly boost productivity. For an even better experience, try tmux plugin manager (tpm) with plugins like tmux-resurrect for saving and restoring sessions.