How to Use tmux: Sessions That Survive Disconnects
tmux keeps terminal sessions alive on the server: run tmux new -s work, start a long task, press Ctrl+b then d, and close your laptop. Later, SSH back in, run tmux attach -t work, and you are exactly where you left off. This guide shows how to use tmux on a server: sessions, windows, panes, scrolling, config and a cheat sheet.
The killer use case: an AI agent such as Claude Code, or a long build, that must keep running when your Wi-Fi drops or you go to bed.
Install tmux
On Ubuntu 24.04 and Debian 12:
sudo apt update && sudo apt install -y tmux
# check the version
tmux -V
On 0x.cloud, tmux is preinstalled in the "For AI agents" image along with Node, Python, Git and Docker.
The Prefix Key
Every tmux shortcut starts with the prefix Ctrl+b: press it, release, then press the next key. This guide writes that as Ctrl+b d.
How to Use tmux Sessions
# create a session named work
tmux new -s work
# list sessions
tmux ls
# attach to the work session
tmux attach -t work
# attach to the most recent session
tmux attach
# kill the work session and everything in it
tmux kill-session -t work
| Keys | Action |
|---|---|
Ctrl+b d |
detach; the session keeps running |
Ctrl+b s |
list and switch sessions |
Ctrl+b $ |
rename the session |
Windows: Several Terminals in One Session
| Keys | Action |
|---|---|
Ctrl+b c |
new window |
Ctrl+b n / Ctrl+b p |
next / previous window |
Ctrl+b 0…9 |
jump to a window by number |
Ctrl+b , |
rename the window |
Ctrl+b & |
close the window |
Panes: Split the Screen
| Keys | Action |
|---|---|
Ctrl+b % |
split left/right |
Ctrl+b " |
split top/bottom |
Ctrl+b + arrow |
move to the neighboring pane |
Ctrl+b o |
next pane |
Ctrl+b z |
zoom a pane to full screen and back |
Ctrl+b x |
close the pane |
Example layout: the agent on the left, htop on the right watching memory, tail -f on the bottom following logs.
Scrolling and Copying
The mouse wheel does not scroll tmux history by default. Two fixes:
- Copy mode:
Ctrl+b [, then arrows or PgUp/PgDn;qto exit. - Turn on the mouse: add
set -g mouse onto your config (below).
A Starter ~/.tmux.conf
nano ~/.tmux.conf
# scroll and select panes with the mouse
set -g mouse on
# longer scrollback
set -g history-limit 50000
# number windows from 1, easier to reach
set -g base-index 1
Apply without restarting: Ctrl+b :, then source-file ~/.tmux.conf. Every command and option is in the official tmux wiki.
How to Use tmux with Claude Code
Claude Code and Codex can run for hours. In a plain SSH session, a dropped connection stops the agent. With tmux:
# create a session for the agent and start it inside
tmux new -s agent
claude
# detach with Ctrl+b d; the agent keeps working
# come back in the morning
tmux attach -t agent
Full setup in deploy Claude Code on a server, and the always-on workflow in run Claude Code 24/7. Claude Code needs 4 GB+ RAM, so s2-4 (2 vCPU, 4 GB) fits an agent at $15.99 a month, or about $0.109520 an hour on hourly billing. Get a server with tmux preinstalled on the Claude Code VPS page.
Connect to my server, create a tmux session named agent, start the build for the project in /root/app inside it, then detach. Send me the command I can use to check progress.
tmux or systemd?
| Job | Use |
|---|---|
| Interactive work, a supervised agent, a long build | tmux |
| A bot or service that must always run and start after reboot | systemd |
tmux does not survive a reboot. For permanent services, set up a systemd service.
Errors and Fixes
| Symptom | Cause | Fix |
|---|---|---|
sessions should be nested with care, unset $TMUX to force |
you are already inside tmux | detach with Ctrl+b d or switch with Ctrl+b s |
can't find session: work |
the session is gone (closed or server rebooted) | tmux ls, then tmux new -s work |
no server running on /tmp/tmux-0/default |
there are no sessions at all | create one: tmux new -s work |
| Mouse wheel does not scroll | mouse mode is off | set -g mouse on in ~/.tmux.conf |
| Shortcuts do nothing | prefix held down together with the key | press Ctrl+b, release, then the key |
| Odd colors | wrong TERM |
in ~/.tmux.conf: set -g default-terminal "tmux-256color" |
tmux Cheat Sheet
| Task | Command or keys |
|---|---|
| New session | tmux new -s name |
| List sessions | tmux ls |
| Attach | tmux attach -t name |
| Detach | Ctrl+b d |
| New window | Ctrl+b c |
| Split right / below | Ctrl+b % / Ctrl+b " |
| Scroll | Ctrl+b [ |
| tmux command prompt | Ctrl+b : |
| Kill session | tmux kill-session -t name |
FAQ
What is tmux? tmux is a terminal multiplexer: it keeps terminal sessions running on the server independently of your SSH connection and lets you split the screen into windows and panes.
How do I leave tmux without closing the session? Press Ctrl+b, release, then press d (detach). The session keeps running on the server; return with tmux attach.
How do I close a tmux session completely? Exit every shell with exit, or run tmux kill-session -t name. Everything running inside the session is stopped.
tmux or screen? Both keep sessions alive after SSH drops. tmux has better pane splitting, simpler configuration and more active development, so most people pick it today.
Does a tmux session survive a server reboot? No. tmux lives as long as the server is up. For programs that must start after a reboot, use a systemd service.