Linux tmux Command: Detach and Reattach Terminal Sessions

tmux is a terminal multiplexer that manages multiple windows and panes in a single terminal. It is useful for continuing shell work even if remote connections are lost, as you can detach a session and then reconnect to it.

tmux commands?

tmux has a hierarchy of sessions, windows, and panes. The default prefix key is Ctrl+b, and you press the next key after pressing the prefix key. Detached sessions are maintained as long as the tmux server is running, but they are not guaranteed to persist through a server reboot or tmux server shutdown.

Basic syntax

tmux [command] [option]

The installed implementation and options may vary depending on the distribution. Check the current system documentation with man tmux.

Examples

Starting a named session

By naming it, it is easier to distinguish what work it is later.

tmux new -s work

Checking the list of sessions

Find currently remaining sessions after detaching.

tmux ls

Reconnect to an existing session

Use the name given when starting.

tmux attach -t work

Detaching and Reattaching a Session

tmux separates the terminal connection from shell tasks. After creating a session, you can press the default prefix Ctrl+B followed by D to exit the screen, and the tasks inside the session can continue running.

tmux new -s demo
tmux ls
tmux attach -t demo

Example output of tmux ls after detaching (dates and status may vary):

demo: 1 windows (detached)

attach returns you to the screen of a detached session. If a session with the same name already exists, creating a new session may fail, so check with tmux ls first. It is useful for maintaining work even if the network to a remote server is disconnected, but if the server itself is rebooted, the standard tmux session will also disappear.

Main Options and Format

Options/Format Description
new -s name Create a new session with a specified name.
ls List sessions of the current tmux server.
attach -t name Connect to the specified session.
detach Detach the current client from the session.
split-window Split the current window into panes.

Precautions when using

In the default key bindings, pressing Ctrl+b followed by d detaches the session, c creates a new window, and % and " split the pane. You press the next key after the prefix key, not simultaneously. Some distributions may not have tmux installed, so first check whether the package is available.

Frequently Asked Questions

Does the tmux session always remain if the SSH connection is disconnected?

As long as the tmux server and remote system keep running, you can usually reconnect. It is not a feature that restores everything after a server reboot or session termination.

Official Documentation

You can check the exact behavior of options and implementation differences in the official tmux documentation.

More in This Category
Linux umask Command: Control Default File Permissions

Linux umask Command: Control Default File Permissions

Learn how to control Default File Permissions with the Linux umask command, including practical examples, key options, and important precautions.

Linux ssh Command: Connect to a Remote Host Securely

Linux ssh Command: Connect to a Remote Host Securely

Learn how to connect to a Remote Host Securely with the Linux ssh command, including practical examples, key options, and important precautions.

Linux mktemp Command: Create Secure Temporary Files and Directories

Linux mktemp Command: Create Secure Temporary Files and Directories

Learn how to create unpredictable temporary files and directories with Linux mktemp, use templates and TMPDIR, clean up with trap, and avoid unsafe dry runs.

Linux readlink Command: Inspect Symbolic Link Targets

Linux readlink Command: Inspect Symbolic Link Targets

Learn how to use the Linux readlink command to display symbolic link targets and optionally resolve canonical paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux basename Command: Extract the Final Component of a Path

Linux basename Command: Extract the Final Component of a Path

Learn how to use the Linux basename command to remove directory prefixes and optional suffixes from path strings, with essential options, practical examples, output interpretation, and common troubleshooting tips.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Understand how the Linux kernel differs from a distribution, what user space and open source mean, and how to check your system's kernel and distribution versions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Use pwd, ls, cd, and tree to find your current location, inspect directories, navigate paths, and check where each command takes you.

Linux rm Command: Remove Files and Directories Safely

Linux rm Command: Remove Files and Directories Safely

Learn how to remove files and directory trees with Linux rm, use interactive safeguards, limit recursive deletion, and avoid destructive path mistakes.