tmux and screen: Persistent Terminal Sessions

Intermediate
11 min

tmux and screen: Persistent Terminal Sessions

An SSH session dies when your network does, and with it every program you started. A terminal multiplexer keeps a shell running on the server independently of your connection: you detach, go home, reattach, and the build is still scrolling. It also splits one terminal into several panes — logs on one side, an editor on the other — which is how most administrators work on remote machines. This lesson teaches tmux, the modern standard, and enough screen to cope on systems that only have that.

Sessions, Windows and Panes

tmux has three levels. A session is a workspace that survives disconnects. A session holds windows (like tabs), and each window can be split into panes. You interact through a prefix key, Ctrl+b by default, followed by a command key.

bash
sudo apt install tmux tmux new -s work # create a session named "work" tmux ls # list sessions tmux attach -t work # reattach (or: tmux a) tmux kill-session -t work tmux new -s work -d # create detached, for scripts

Detach and Reattach: The Core Workflow

bash
ssh web tmux new -s migrate ./run-migration.sh # takes two hours # Ctrl+b, then d -> "[detached (from session migrate)]" exit # close SSH; the migration keeps running # Later, from anywhere ssh web tmux attach -t migrate

If your connection drops without detaching, the session simply stays attached from tmux's point of view; tmux attach -d -t migrate detaches the old client and takes over.

Essential Key Bindings

All keys are pressed after the Ctrl+b prefix.

| Key | Action | |---|---| | d | detach | | c | new window | | n / p / 0-9 | next / previous / numbered window | | , | rename window | | w | interactive window list | | % | split pane left/right | | " | split pane top/bottom | | arrow keys | move between panes | | z | zoom the current pane to full size (toggle) | | x | close the current pane | | o | cycle panes | | Space | cycle layouts | | { / } | swap pane with previous / next | | [ | enter copy (scroll) mode; q exits | | ? | list all bindings | | : | tmux command prompt |

Scrolling is the first thing that surprises new users: the mouse wheel does nothing until you enter copy mode with [ (or enable mouse support in the config below).

Copy Mode

Inside copy mode, navigate with arrows or PgUp/PgDn, search backwards with ?, start a selection with Space, and copy with Enter. Paste with Ctrl+b ]. With setw -g mode-keys vi in your config, the keys become h j k l, /, v and y.

A Practical Configuration

Settings live in ~/.tmux.conf. This one enables the mouse, moves the prefix to Ctrl+a (easier to reach and matches screen), starts numbering at 1 and makes splits keep the current directory.

bash
# ~/.tmux.conf set -g prefix C-a unbind C-b bind C-a send-prefix set -g mouse on set -g base-index 1 setw -g pane-base-index 1 set -g history-limit 50000 setw -g mode-keys vi set -g default-terminal "tmux-256color" bind | split-window -h -c "#{pane_current_path}" bind - split-window -v -c "#{pane_current_path}" bind r source-file ~/.tmux.conf \; display "reloaded"

Apply it without restarting: tmux source-file ~/.tmux.conf. The status bar at the bottom shows the session name, windows and time, and is fully customisable with set -g status-left and status-right.

Scripting tmux

Because everything is a command, a layout can be built from a script and a command can be sent into a running pane — handy for launching a development environment or running a job under supervision.

bash
tmux new -d -s dev -n editor 'vim' tmux split-window -t dev -h 'npm run dev' tmux split-window -t dev -v 'tail -f logs/app.log' tmux select-pane -t dev:1.1 tmux attach -t dev tmux send-keys -t migrate "./run-migration.sh" Enter # type into a session tmux capture-pane -t migrate -p | tail -20 # read its screen

GNU screen

screen predates tmux and is still the only multiplexer on some minimal or older systems. The concepts are the same; the prefix is Ctrl+a.

| Task | screen | |---|---| | start named session | screen -S work | | detach | Ctrl+a d | | list | screen -ls | | reattach | screen -r work (-d -r to steal an attached one) | | new window | Ctrl+a c | | next / previous window | Ctrl+a n / Ctrl+a p | | split horizontal / vertical | Ctrl+a S / Ctrl+a \| | | scroll mode | Ctrl+a [ | | kill window | Ctrl+a k |

screen also has a niche use tmux lacks: connecting to serial consoles (screen /dev/ttyUSB0 115200).

Common Mistakes

  • Nesting tmux inside tmux over SSH and sending the prefix to the wrong one; press the prefix twice to pass it through, or detach the inner one.
  • Forgetting the session name and creating a new one each login; tmux attach with no -t attaches to the most recent.
  • Assuming a detached session survives a reboot; it does not. Use systemd for that.
  • Trying to scroll with the wheel before enabling mouse on or entering copy mode.
Quick Quiz
Question 1 of 3

Which key sequence detaches from a tmux session with the default prefix?

Key Takeaways

  • tmux sessions survive disconnects: tmux new -s name, prefix d to detach, tmux attach -t name to return.
  • Sessions contain windows (c, n, p) which contain panes (%, ", arrows, z).
  • Scroll with copy mode ([) or enable set -g mouse on in ~/.tmux.conf.
  • Everything is scriptable: split-window, send-keys and capture-pane build and drive layouts.
  • screen uses Ctrl+a and the same ideas; use systemd, not a multiplexer, for services that must survive reboots.

Next lesson: Security Basics: ufw Firewall, fail2ban and Server Hardening — close every port you do not need and stop brute-force attacks automatically.

tmux and screen: Persistent Terminal Sessions - Linux & Command Line | CodeYourCraft | CodeYourCraft