tmux is a terminal multiplexer: a program that runs on the server itself and holds on to your terminals, even when your SSH connection drops. If your internet cuts out in the middle of a long upgrade, a database dump or a build, the work carries on on the server, and tmux attach puts you back on the same screen, with the same output and the same command still running. tmux also lets you split one terminal window into several panes and keep several windows open over a single connection.
The essentials: tmux new -s work creates a session, Ctrl+b followed by d detaches from it (without closing anything), and tmux attach -t work brings you back. Below we first look at why disconnects kill processes, then explain sessions, windows and panes, write a small config file, and finish with a cheat sheet. If you are not yet comfortable with SSH itself, read connecting to a server with SSH keys first.
Short answer: tmux is a terminal multiplexer that runs on the server and keeps terminal sessions alive even after your SSH connection drops. Create a session with
tmux new -s work, detach withCtrl+bthend, and return to the same screen and running command withtmux attach -t work.
Why a dropped SSH connection kills your processes
When you log in over SSH, your shell (usually bash) is a child of that connection, and every command you run is a child of the shell. When the connection drops, the system sends the shell a SIGHUP ("hang up") signal, and the shell passes it on to its children. Most programs respond to that signal by exiting. The result: a half-finished apt upgrade, a migration stopped midway, or a truncated archive.
There are older workarounds, such as nohup or sending a job to the background with & and disown. These keep the process alive, but you can no longer watch its live output or answer it if it asks something (like "Do you want to continue?"). tmux keeps the terminal itself on the server, not just the process.
For services that must always be running (a queue worker or an API), tmux is the wrong tool; make them a systemd service. tmux is for your own hands-on work.
Installation
On Ubuntu 24.04 tmux is usually preinstalled. If not:
sudo apt update && sudo apt install -y tmux
tmux -V
The same command works on Debian; on Rocky/AlmaLinux use sudo dnf install tmux. Install tmux on the server, not on your laptop; the whole point is that it stays on the server.
Three concepts: sessions, windows, panes
- Session: the top level. A named workspace, such as
deployordb. A session lives until you close it or the server reboots. - Window: each session has several windows, like browser tabs. They are listed in the green status bar at the bottom of the screen.
- Pane: each window can be split horizontally or vertically into panes; for example,
tail -fon a log on one side and a shell on the other.
A good habit: create one session with a meaningful name per task, rather than one unnamed session where everything piles up.
The prefix key
tmux needs to know which keystrokes are meant for it and which for the program inside the terminal. So every shortcut starts with a prefix key, Ctrl+b by default. Press Ctrl and b together, release, then press the command key. For example, Ctrl+b then c means "new window".
In this article, prefix means Ctrl+b (or whatever you later replace it with).
Detach and attach
This is the core tmux workflow:
# Create a named session
tmux new -s deploy
# ... do some work, e.g. start a long-running command
# Press prefix then d to detach; the command keeps running
# List sessions
tmux ls
# Reattach to the session
tmux attach -t deploy
# Shorter, if you only have one session:
tmux a
If your SSH connection drops unexpectedly, exactly the same thing happens as with prefix d: the session stays in the background. Log back in and run tmux attach.
A handy trick: the command below attaches to a session named main if it exists and creates it if it does not. You can make a habit of running it every time you log in:
tmux new -A -s main
To kill a session from outside:
tmux kill-session -t deploy
From inside, just type exit in the last pane; when the last pane closes, the session closes too.
Two people on one session
If two people are logged in as the same user and both run tmux attach -t deploy, they both see the same screen and can both type. For showing a colleague a problem or pairing on a server, it is simpler than screen sharing.
Working with windows and panes
Windows
prefix ccreates a new window.prefix nandprefix pgo to the next and previous window.prefix 0toprefix 9jump straight to that window number.prefix ,renames the window. Naming windows (logs,shell,db) makes them much easier to find.prefix wshows a list of all windows and sessions to pick from.
Panes
prefix %splits the window vertically (two panes side by side).prefix "splits it horizontally (two panes stacked).prefixplus an arrow key moves between panes.prefix zzooms the current pane to full screen; press it again to restore.prefix xcloses the current pane (after confirmation).
Scrolling and copying
By default, the mouse wheel does not scroll back through output in tmux. To see earlier output, press prefix [ to enter copy mode; move up and down with the arrow keys or PgUp/PgDn, and press q to leave. Once you enable the mouse in the next section, wheel scrolling works too.
A small ~/.tmux.conf
The defaults are usable, but a few small changes make life easier. Create this file on the server:
# Use Ctrl+a as the prefix; easier to reach with one hand
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# Mouse: select panes, resize them and scroll with the wheel
set -g mouse on
# Number windows and panes from 1, which sits next to 2 on the keyboard
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on
# More scrollback history
set -g history-limit 50000
# Split panes with memorable keys, keeping the current directory
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# Reload this file with prefix r
bind r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"
Sessions that are already open do not pick up new settings automatically. Run this once from inside tmux (after that, prefix r is enough):
tmux source-file ~/.tmux.conf
A few notes on these settings:
- In bash,
Ctrl+ais the "go to start of line" shortcut.bind C-a send-prefixmeans pressingCtrl+atwice still does that. If you rely on that shortcut, keep the prefix asCtrl+b. - With
mouse on, tmux takes over mouse text selection. To select and copy with your own terminal instead, holdShiftwhile dragging (in most terminals; in iTerm2 it isOption). - If you work on several servers, keep this file in a dotfiles repository so it is the same everywhere.
Common mistakes
- tmux inside tmux. If you are inside tmux on server A, hop to server B and open tmux there too, the prefix goes to the outer tmux. Either do not run tmux on the intermediate server, or send the prefix twice for the inner one.
- Forgetting old sessions. Every open session with a running command uses memory. Run
tmux lsnow and then and close the ones you no longer need. - Expecting sessions to survive a reboot. tmux keeps sessions in memory, not on disk. A server reboot closes them all.
- Running a permanent service in tmux. If the server reboots or the process dies, nobody brings it back. That is systemd's job.
tmux cheat sheet
| Action | Command or key |
|---|---|
| New named session | tmux new -s name |
| Attach or create | tmux new -A -s name |
| List sessions | tmux ls |
| Reattach to a session | tmux attach -t name or tmux a |
| Kill a session from outside | tmux kill-session -t name |
| Detach from session | prefix d |
| Choose a session from a list | prefix s |
| Rename session | prefix $ |
| New window | prefix c |
| Next / previous window | prefix n / prefix p |
| Go to window N | prefix N |
| Rename window | prefix , |
| Split vertically (side by side) | prefix % |
| Split horizontally (stacked) | prefix " |
| Move between panes | prefix + arrow key |
| Zoom pane | prefix z |
| Close pane | prefix x |
| Copy mode and scrolling | prefix [ (exit with q) |
| List all key bindings | prefix ? |
Frequently asked questions
Do tmux sessions survive a server reboot?
No. tmux keeps sessions in memory, not on disk, so a server reboot closes every session and the commands running in them. tmux only protects your work from a dropped SSH connection.
What is the difference between tmux and nohup?
nohup only keeps the process alive after you disconnect; you can no longer watch its live output or answer its prompts. tmux keeps the terminal itself on the server, and when you come back you get the same screen with the full output and the ability to type.
Should I install tmux on my laptop or on the server?
On the server. The whole point of tmux is to run on the machine where the work happens, so that when your laptop's connection drops the session stays alive on the server. On Ubuntu, install it with sudo apt install tmux.
How do I scroll up in tmux?
Press prefix [ to enter copy mode, move up and down with the arrow keys or PgUp and PgDn, and press q to leave. If you add set -g mouse on to your config file, mouse-wheel scrolling works too.
How do I list and kill tmux sessions?
tmux ls lists all sessions and tmux kill-session -t name kills a given session from outside. From inside a session, just type exit in the last pane.
Wrap-up
tmux is one of those tools you never log into a server without again once you have lost unfinished work. Learn the minimum: tmux new -A -s main after logging in, prefix d to detach and tmux a to come back. Add windows, panes and a config file gradually. Hand permanent jobs to systemd and keep tmux for your own hands-on work. For other everyday commands, see essential Linux commands for server management.