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 tree Command: Display a Directory Tree

Linux tree Command: Display a Directory Tree

Learn how to use the Linux tree command to display directory contents as a readable hierarchy and control depth and filtering, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.

Linux nmap Command: Scan Authorized Hosts and Ports

Linux nmap Command: Scan Authorized Hosts and Ports

Learn how to scan Authorized Hosts and Ports with the Linux nmap command, including practical examples, key options, and important precautions.

Linux chgrp Command: Change a File's Group

Linux chgrp Command: Change a File's Group

Learn how to change a File's Group with the Linux chgrp command, including practical examples, key options, and important precautions.

Linux fold Command: Wrap Long Lines at a Fixed Width

Linux fold Command: Wrap Long Lines at a Fixed Width

Learn how to wrap Long Lines at a Fixed Width with the Linux fold command, including practical examples, key options, and important precautions.

Linux resolvectl Command: Inspect systemd DNS Resolution

Linux resolvectl Command: Inspect systemd DNS Resolution

Learn how to inspect systemd DNS Resolution with the Linux resolvectl command, including practical examples, key options, and important precautions.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.

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.

Linux watch Command: Run a Command Repeatedly

Linux watch Command: Run a Command Repeatedly

Learn how to use the Linux watch command to rerun a command at regular intervals and highlight changing output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux uname Command: Check Kernel and System Architecture

Linux uname Command: Check Kernel and System Architecture

Learn how to check Kernel and System Architecture with the Linux uname command, including practical examples, key options, and important precautions.