Started a large database import or migration over SSH and your Wi-Fi dropped? Without precautions the command dies with the connection. tmux keeps a terminal session running on the server, so you can disconnect and pick up exactly where you left off. This tmux tutorial covers everything you need for server work.
Before you start
- SSH access to a Linux server.
- Install tmux:
apt install tmuxordnf install tmux.
Start a named session
tmux new -s migrateYou are now inside tmux; a green status bar appears at the bottom. Run your long command as usual, for example a large import.
Detach and reattach
All tmux shortcuts start with the prefix Ctrl+b: press it, release, then press the next key.
- Detach (leave it running): Ctrl+b then d.
- List sessions:
tmux ls - Reattach:
tmux attach -t migrate(or justtmux afor the last one)
If your connection drops, simply SSH back in and reattach; the command kept running.
Essential shortcuts
| Keys (after Ctrl+b) | Action |
|---|---|
d | Detach |
c | New window |
n / p | Next / previous window |
% | Split pane left/right |
" | Split pane top/bottom |
| Arrow keys | Move between panes |
[ | Scroll mode (arrows/PgUp, q to exit) |
x | Close the current pane |
$ | Rename the session |
A typical layout: one pane running a deployment, another tailing logs with journalctl -f, a third with htop.
Make tmux friendlier
Create ~/.tmux.conf:
set -g mouse on
set -g history-limit 50000
set -g base-index 1Mouse mode lets you scroll, select panes and resize them with the mouse. Reload with tmux source-file ~/.tmux.conf.
End a session
Type exit in every pane, or kill the session from outside:
tmux kill-session -t migrateThe screen equivalents
Some older servers only have GNU screen. The ideas are the same; the prefix is Ctrl+a:
| Task | screen |
|---|---|
| New named session | screen -S migrate |
| Detach | Ctrl+a then d |
| List | screen -ls |
| Reattach | screen -r migrate |
Alternatives for one-off commands
nohup long-command > output.log 2>&1 &runs a command in the background, immune to hang-ups. You cannot interact with it later, but the log captures output.- For services that should always run, create a systemd unit instead.
Troubleshooting
- sessions should be nested with care: you ran tmux inside tmux. Detach from the inner one, or use the existing session.
- No sessions after a reboot: tmux sessions live in memory and end when the server restarts.
- Strange colours or keys: set your terminal type, e.g.
set -g default-terminal "tmux-256color". - Cannot scroll with the mouse wheel: enable
set -g mouse onor use Ctrl+b [.
tmux FAQ
tmux or screen?
tmux is more actively developed and easier to configure. Use screen only where tmux is unavailable.
Can several people share a tmux session?
Yes, two users attached to the same session (as the same user) see and type into the same terminal, handy for pair debugging.
Is tmux available on cPanel hosting?
It depends on the host; jailed shells often do not include it. On your own VPS you can install it freely.
