Guides

How to use tmux — so a command on your server survives a closed laptop

Illustration: a screen split into three panes keeps glowing even though the laptop cable has already been pulled out

You started something long on a server: an install, a database migration, an agent editing code for an hour. You need to leave — you close your laptop. You come back, and the work stopped halfway.

The reason is simple: everything you start over SSH lives inside your connection. The connection dropped — programs got the "hang up" signal and exited.

tmux fixes this in one move. It keeps the terminal on the server itself, and you just connect to it. Disconnect — the terminal keeps running. Reconnect — you see everything exactly where you left it, even from another computer.

Sounds complicated, but five actions cover 90% of tasks. Step by step.

Step 1. Install tmux

On an Ubuntu or Debian server:

sudo apt install tmux

On a Mac — brew install tmux. Check it worked: tmux -V shows the version.

Install it where the work will run. If the command runs on a server, tmux goes on the server, not on your laptop.

Step 2. Create a named session

tmux new -s work

The screen clears and a colored bar appears at the bottom. That means you're inside tmux. The session is called work — you'll need the name to come back later.

Run whatever needs to live long here:

npm run build && npm run migrate

Step 3. Leave without stopping anything

Here's the main trick. Press Ctrl+B, release, then press D.

Ctrl+B is the "prefix": it tells tmux the next key is a command for tmux, not for the program. D is detach.

You're back in your normal terminal. And the work session, with everything running in it, keeps going. Now you can close SSH, the laptop, go to sleep.

Important: don't leave with exit or Ctrl+D. That closes the session itself, along with the work.

Step 4. Come back to the session

SSH into the server again and see which sessions are alive:

tmux ls
# work: 1 windows (created Sat Nov 15 10:02:11 2026)

Attach:

tmux attach -t work

You'll see the screen exactly as it was: the finished output, the cursor, everything. If there's only one session, a short tmux a is enough.

Step 5. Split the screen

A common setup: the server runs on the left, you watch the log on the right. Inside tmux:

  • Ctrl+B, then % — split into left and right halves.
  • Ctrl+B, then " — split into top and bottom.
  • Ctrl+B, then an arrow — move to the neighboring pane.
  • Ctrl+B, then Z — zoom a pane to full screen and back.
  • Ctrl+B, then X — close a pane (tmux asks to confirm).

The right pane is a good home for tail -f — there's a separate walkthrough on how to watch a log in real time.

Step 6. Scroll the output up

The first thing people trip on: the mouse wheel in tmux doesn't scroll history by default. Two ways out.

Quick: Ctrl+B, then [. Scroll mode turns on — use arrows or PageUp. Exit with Q.

Comfortable forever: create ~/.tmux.conf and add one line:

set -g mouse on

Apply it without restarting: tmux source-file ~/.tmux.conf. Now the wheel scrolls, and you can pick a pane with the mouse. To select text the usual way, hold Shift while selecting (in iTerm2, Option).

Step 7. Add windows for different tasks

Panes split one screen. Windows are like browser tabs: each takes the full screen, and you switch between them.

  • Ctrl+B, then C — new window.
  • Ctrl+B, then a number — jump to the window with that number. The numbers are shown in the bottom bar.
  • Ctrl+B, then , (comma) — rename the window, e.g. to server or logs.

A handy layout: a server window with the project running, a logs window with the journal, a shell window for one-off commands. One session holds it all.

When the work is done, close the whole session: tmux kill-session -t work. Otherwise it keeps hanging in the server's memory until a reboot — with its programs still running.

What you get

Your new rhythm for working on a server looks like this:

  1. ssh server
  2. tmux a — or tmux new -s work if there's no session yet.
  3. Work, start the long stuff.
  4. Ctrl+B, D — detached, closed the laptop.
  5. Tomorrow: ssh server, tmux a — and everything is there.

This is especially handy with agents. Claude Code or Codex running in tmux on a server will calmly finish the task while you're on the subway. Later you connect from your phone with any SSH client and see the result.

How is tmux different from nohup?

nohup only protects a command from the disconnect, but you can't come back to it and type anything — you're left reading an output file. With tmux you return to a live terminal: you can answer a program's question, hit Ctrl+C, keep working.

Will a tmux session survive a server reboot?

No. tmux lives in memory, and a reboot closes every session. Anything that should run for weeks and come back by itself should be a systemd daemon.

Why doesn't Ctrl+B work?

Most often you're pressing the keys at the same time. The prefix works in sequence: Ctrl+B, release, then the second key. Another cause — you're already inside tmux and started tmux again; better not to nest sessions.

Learn vibe coding — don’t just read about it

Short story-lessons, an agent simulator and daily practice — in our mobile app. Free.

Open the app
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →