Mastering Tmux - The Essential Guide for Persistent Cloud Sessions
Stop losing work! Learn how to use tmux to keep long-running processes alive on a cloud server, even after your SSH connection drops. Complete beginner’s guide to sessions, windows, and panes.
Part 1: Tmux Fundamentals and Session Persistence
1. What is Tmux?
Tmux is a terminal multiplexer. It creates a persistent shell that runs on your cloud server, independent of your local SSH connection.
| Feature | Benefit |
|---|---|
| Persistence | Processes continue to run after you disconnect. |
| Resilience | Your work is safe from unstable Wi-Fi or accidental shutdowns. |
| Organization | Manage multiple terminal windows and splits within a single session. |
2. Installation
Install tmux on your cloud server using your distribution’s package manager:
# For Debian/Ubuntu
sudo apt update && sudo apt install tmux
# For CentOS/RHEL
sudo yum install tmux
3. The Prefix Key
All tmux keyboard shortcuts start with the Prefix Key: Ctrl + b. Press and release this combination before any command key.
4. Session Management: Named vs. Unnamed
Tmux sessions can be handled in two ways: named (recommended for clarity) or unnamed (automatically numbered).
| Action | Named Session (e.g., myproject) | Unnamed Session (e.g., 0, 1) |
|---|---|---|
| Create New | tmux new -s myproject (Long) / tmux new -s myproject (Short) | tmux new (Long) / tmux (Shortest) |
| Detach | Ctrl + b then d | Ctrl + b then d |
| List All | tmux list-sessions (Long) / tmux ls (Short) | tmux list-sessions (Long) / tmux ls (Short) |
| Reattach | tmux attach -t myproject (Long) / tmux a -t myproject (Short) | tmux attach -t 0 (Long) / tmux a -t 0 (Short) |
| Reattach (First Available) | N/A | tmux attach (Long) / tmux a (Short) |
Note on Unnamed Reattach: If you have multiple unnamed sessions (e.g., 0, 1, 2), you must use the -t flag with the specific number (tmux a -t 2). If you only use tmux a with multiple sessions, it will usually connect to the oldest or numerically lowest one.
5. Running Your Process
Once you are inside a session (named or unnamed), you can safely run any long command:
- Example: Start a training script
python3 train_model.py # This process will continue after you detach!
6. Killing a Session
When your process is complete, you should terminate the session to free up resources.
| Method | Command |
|---|---|
| From Outside | tmux kill-session -t [name or number] |
| From Inside (If only one pane exists) | Ctrl + b then x (then confirm with y) |
Part 2: Advanced Organization with Windows and Panes
Tmux allows you to organize your terminal space using two main levels of hierarchy: Windows (like tabs) and Panes (like split screens).
1. Window Management (The Tabs)
| Task | Shortcut (After Ctrl + b) | Description |
|---|---|---|
| Create New Window | c | Creates a new, empty window and switches you to it. |
| Switch to Next Window | n | Moves to the window with the next sequential number. |
| Switch by Index | 0 to 9 | Directly switches to the window by its number. |
| List Windows | w | Opens an interactive list of all windows in the session. |
| Rename Window | , (Comma) | Opens the command prompt to type a new name. |
| Close (Kill) Window | & (Ampersand) | Kills the current window and all panes inside it (Requires confirmation: y). |
2. Pane Management (The Splits)
Panes split a single window into multiple independent terminal views (e.g., viewing logs while editing code).
| Task | Shortcut (After Ctrl + b) | Description |
|---|---|---|
| Split Vertically | % (Percent) | Splits the current pane into two side-by-side (left/right). An older way |
| Split Vertically | v (v) | Divides the current pane into two panes side-by-side. (left/right). |
| Split Horizontally | " (Double Quote) | Splits the current pane into a top and bottom pane. |
| Switch Panes | <Arrow Key> | Changes focus to the adjacent pane. |
| Zoom Pane | z | Toggles the active pane to fill the entire window temporarily. |
| Close (Kill) Pane | x (Letter X) | Kills the currently active pane (Requires confirmation: y). |
Summary: Why Tmux is Essential
Tmux is the ultimate safety net for every remote professional. Using a named session for a specific project allows you to guarantee that your long-running script, training job, or migration will never be terminated by an unstable network connection. It transforms your server connection into a reliable, multi-tasking workspace.