Linux VPS

Keep commands running after SSH disconnects with tmux

Use tmux (or screen) to run long tasks that survive a dropped SSH connection, detach and reattach sessions, split panes and learn the few shortcuts that matter.

BeginnerLinux VPS3 min read

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 tmux or dnf install tmux.

Start a named session

tmux new -s migrate

You 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 just tmux a for the last one)

If your connection drops, simply SSH back in and reattach; the command kept running.

Essential shortcuts

Keys (after Ctrl+b)Action
dDetach
cNew window
n / pNext / previous window
%Split pane left/right
"Split pane top/bottom
Arrow keysMove between panes
[Scroll mode (arrows/PgUp, q to exit)
xClose 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 1

Mouse 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 migrate

The screen equivalents

Some older servers only have GNU screen. The ideas are the same; the prefix is Ctrl+a:

Taskscreen
New named sessionscreen -S migrate
DetachCtrl+a then d
Listscreen -ls
Reattachscreen -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 on or 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.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.