Skip to content
C Codeloom
Linux

Linux tmux and screen Tutorial: Persistent Terminal Sessions

A practical guide to tmux and GNU screen for persistent shells, splits, and remote workflows, with side-by-side commands and tips for daily use.

·5 min read · By Codeloom
Intermediate 9 min read

What you'll learn

  • What terminal multiplexers are and why they matter
  • Core tmux and screen commands side by side
  • How to manage windows, panes, and detached sessions
  • Patterns for remote work over SSH
  • Production tips and common pitfalls

Prerequisites

  • Familiar with shell

What and Why

A terminal multiplexer runs many shells inside a single terminal window and keeps them alive even when you disconnect. The two veterans are GNU screen, shipped since the 1980s, and tmux, which arrived in 2007 with a cleaner config and saner defaults.

The pitch is simple: SSH into a server, start a long task, close the laptop, reconnect tomorrow, and continue exactly where you left off. Without a multiplexer, closing the SSH connection kills the shell and any process attached to it.

Mental Model

Both tools have the same shape. A session is a collection of windows. A window is a collection of panes. A pane is a shell. You attach a terminal to a session; you can detach and reattach without losing state because the session lives in a long-running daemon.

Every action goes through a prefix key. In screen the default is Ctrl+a. In tmux the default is Ctrl+b. You press the prefix, release, then press the action key. This is why Ctrl+a d detaches in screen and Ctrl+b d detaches in tmux.

If you remember three nouns (session, window, pane) and one verb (detach), eighty percent of daily use falls out naturally.

Hands-on Example

Suppose you SSH into a build server, start a long compile, split the screen to tail logs, then disconnect for the night.


Session: build-server
+--------------------------------------------+
| Window 0: editor                           |
| +-------------------+ +------------------+ |
| |  vim main.c       | |  make -j8        | |
| |                   | |  ...compiling... | |
| |                   | |                  | |
| +-------------------+ +------------------+ |
+--------------------------------------------+
| Window 1: logs                             |
| tail -f /var/log/build.log                 |
+--------------------------------------------+
 status: [build-server] 0:editor* 1:logs
A tmux session with two windows and a split pane

In tmux the flow looks like this:

tmux new -s build-server          # create named session
# inside tmux:
# Ctrl+b c        new window
# Ctrl+b %        split pane vertically
# Ctrl+b "        split pane horizontally
# Ctrl+b o        cycle panes
# Ctrl+b n / p    next / previous window
# Ctrl+b d        detach
tmux ls                           # list sessions
tmux attach -t build-server       # reattach

The screen equivalents use Ctrl+a as the prefix:

screen -S build-server            # create named session
# Ctrl+a c        new window
# Ctrl+a |        split vertically (screen 4+)
# Ctrl+a S        split horizontally
# Ctrl+a Tab      cycle panes
# Ctrl+a n / p    next / previous window
# Ctrl+a d        detach
screen -ls                        # list sessions
screen -r build-server            # reattach

Running make -j8 inside a window and then detaching means you can close the laptop, walk away, and reattach the next morning to see the finished output.

Common Pitfalls

The most common pitfall is forgetting which prefix is active when you mix tools. Many people remap tmux to Ctrl+a to match screen, then SSH into a server that uses default tmux (Ctrl+b) and feel lost for a minute.

Another pitfall is nesting multiplexers. Running tmux inside tmux over SSH leads to prefix collisions. The conventional fix is to send the prefix twice (Ctrl+b Ctrl+b d detaches the inner session) or to remap the inner prefix.

Copy-paste also surprises new users. Default mouse selection often selects across panes and includes pane borders. Enabling set -g mouse on in tmux and using bracketed paste in your terminal emulator fixes this.

Finally, do not assume detached sessions survive reboots. They live in a daemon attached to your user; a reboot kills them. Use systemd-run --user --scope tmux or a cron @reboot job if you need persistence across host restarts.

Production Tips

Pick tmux for new setups. Its config is more readable, plugins like tmux-resurrect can save and restore sessions, and most modern dotfile repos assume it.

Use named sessions. tmux new -s deploy is much easier to find than session 3 six hours later. The same applies to windows: Ctrl+b , renames the current window.

Keep a small ~/.tmux.conf. Set mouse on, bind a sensible split key (bind | split-window -h), and enable a longer history limit (set -g history-limit 50000). Resist the temptation to copy a hundred-line config from the internet; you will forget what each line does.

For remote work, always wrap SSH commands in a multiplexer. A flaky Wi-Fi disconnect should never kill a database migration in progress.

Wrap-up

tmux and screen solve the same core problem: keep shells alive across disconnects and let you split your terminal without leaving the keyboard. The vocabulary of sessions, windows, and panes carries across both tools.

Learn one tool well, configure a minimal prefix and a few splits, and use named sessions for anything that runs longer than five minutes. Once it is a reflex, you stop thinking about it and the productivity gain quietly compounds every workday.