How to Use tmux and screen for Persistent Terminal Sessions

tmux and screen let a terminal session keep running even after you disconnect — essential for long-running commands, or simply avoiding losing your work if your SSH connection drops unexpectedly.

The Problem They Solve

Without a session multiplexer, closing your SSH connection (deliberately or accidentally) terminates any running foreground processes — tmux/screen keep the session alive on the server, letting you reattach later exactly where you left off.

Installing tmux

sudo apt install tmux -y

Starting a New tmux Session

tmux new -s mysession

Detaching from a Session (Without Ending It)

Ctrl+b, then d

The process(es) running inside continue running on the server even after you disconnect.

Reattaching to a Session

tmux attach -t mysession

Listing Active Sessions

tmux ls

Killing a Session

tmux kill-session -t mysession

Splitting Panes Within tmux

Ctrl+b, then %   # split vertically
Ctrl+b, then "   # split horizontally
Ctrl+b, then arrow keys  # navigate between panes

Creating Multiple Windows Within One Session

Ctrl+b, then c   # new window
Ctrl+b, then n   # next window
Ctrl+b, then p   # previous window

Installing screen (Alternative)

sudo apt install screen -y

Basic screen Usage

screen -S mysession       # start a named session
Ctrl+a, then d             # detach
screen -r mysession        # reattach
screen -ls                 # list sessions

tmux vs screen: Which to Choose

Both accomplish the same core goal — tmux is generally considered more modern with more flexible pane/window management and better default configurability; screen is older but still perfectly functional and pre-installed on some systems. Either is a reasonable choice; consistency (picking one and learning it well) matters more than which specific tool.

Practical Use Case: Running a Long Installation

tmux new -s install
./long-running-install.sh
# Ctrl+b, d to detach and check back later
tmux attach -t install

Practical Use Case: Monitoring Logs While Working Elsewhere

tmux new -s logs
tail -f /var/log/app.log
# Ctrl+b, d, then create another session/window for actual work

Common Errors

"session not found" when trying to reattach — verify the exact session name with tmux ls; a typo in the session name is the most common cause.

Session ended unexpectedly — if the server itself rebooted, tmux/screen sessions don't survive a reboot; only SSH disconnections, not server restarts.

Continue Reading

Browse more articles in Linux Server Administration.

  • tmux tutorial, screen command linux, persistent ssh session, tmux vs screen
  • 0 Користувачі, які знайшли це корисним
Ця відповідь Вам допомогла?

Схожі статті

How to Update and Upgrade Your Ubuntu or Debian VPS

Keeping your VPS updated is one of the most important and simplest maintenance tasks — it...

How to Enable and Configure Swap on a Linux VPS (Ubuntu & Debian)

Swap is disk space Linux can use as overflow memory when physical RAM is exhausted. It won't make...

How to Manage Services with systemd and systemctl

systemd is the service manager used by Ubuntu, Debian, and most modern Linux distributions to...

How to Read and Analyze Linux Logs with journalctl

On modern Ubuntu and Debian systems, journalctl is the central tool for viewing system and...

How to Schedule Tasks with Cron on a Linux VPS

Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log...