tmux Sessions: Keep Your Terminal Alive When SSH Drops

A dropped SSH connection takes your shell, your half-finished upgrade and your scrollback with it. tmux sessions keep running on the server so you can detach, reconnect and pick up where you left off.

Tux Harlow

4 min read

By Tux Harlow — Solnyxus editorial voice. Drafted with AI assistance; reviewed by the team.

You know the moment. The upgrade is forty minutes in, the progress bar has been at 87% for a while, and then the cursor stops blinking. The Wi-Fi hiccuped. The SSH connection is gone, and the shell on the far end went with it, taking the job you started and the scrollback you were going to read. tmux sessions fix this, and they fix it so plainly that you'll be a little annoyed nobody made you learn them sooner.

The trick is simple. tmux runs your shell inside its own server process on the machine, and that process doesn't care whether you're connected. You leave. It keeps going. You come back.

Starting and naming tmux sessions

Bash
tmux new -s work

That starts a session called work. Name your sessions. Always. An unnamed session gets a number, and three days later "0", "1" and "4" mean nothing to anyone, least of all you.

Bash
tmux ls

That lists every session on the machine.

Bash
tmux attach -t work

And that gets you back into work.

Bash
tmux new -A -s work

This is the one I'd actually put in muscle memory. It attaches to work if it exists and creates it if it doesn't, so you never have to remember which state you left things in.

The prefix key, and walking away

Every tmux shortcut starts with the prefix, Ctrl-b out of the box. Press it, let go, then press the command key. Not together. Everyone gets this wrong for about a day.

To detach on purpose: Ctrl-b then d. You drop back to your ordinary shell, and the session carries on without you. Here's the lovely part: if the connection dies instead, tmux treats it exactly the same. Nothing is lost. You reconnect, run tmux attach -t work, and the progress bar is right where it was (or, with any luck, finished).

Windows and panes

A session holds windows. A window can be cut into panes. That's the whole model, and these are the keys that drive it:

  • Ctrl-b c creates a new window.
  • Ctrl-b n and Ctrl-b p move to the next and previous window.
  • Ctrl-b % splits the current pane left and right.
  • Ctrl-b " splits the current pane top and bottom.
  • Ctrl-b followed by an arrow key moves between panes.
  • Ctrl-b z zooms the current pane to fill the window, and pressing it again restores the layout.

Yes, % for a side-by-side split and " for a stacked one is arbitrary. You'll stop noticing within a week. Zoom is the sleeper hit: tail a log in one pane, edit in the other, and blow either one up to full size when you need to actually read it.

Scrolling back

Side note, because this one confuses people: your terminal's scrollbar is useless inside tmux. tmux draws its own screen, so the terminal never sees the history. Scroll with the wheel and you'll get your shell's command history, or nothing at all.

Press Ctrl-b [ to enter copy mode, then use the arrow keys or Page Up. Press q to get out.

And the default history limit is 2000 lines. That's nothing. Follow a busy log for a minute and the interesting part is already gone.

A small tmux.conf

tmux reads ~/.tmux.conf, and since tmux 3.1 it also looks in ~/.config/tmux/tmux.conf if you like your home directory tidy. Three lines go a long way:

Bash
set -g mouse on
set -g history-limit 50000
set -g base-index 1

The first turns on the mouse: click a pane to select it, drag a border to resize, scroll with the wheel. The second raises the scrollback to 50,000 lines, which is what that 2000 should have been. The third numbers windows from 1, so they line up with the number row instead of starting over by the backtick.

Reload it without restarting anything:

Bash
tmux source-file ~/.tmux.conf

Ending sessions

Bash
tmux kill-session -t work

That ends work and everything running inside it, so make sure you mean it. Typing exit in the last pane of the last window does the same thing, more politely.

When sessions vanish at logout

One gotcha. Some systems are set to kill a user's processes at logout, through systemd-logind's KillUserProcesses=yes setting in logind.conf. If your tmux sessions disappear every time you log out, that's the first place to look. Most distributions ship it set to no, so if you're hitting this, someone chose it.

So: next time you SSH somewhere to run anything that takes longer than a coffee, type tmux new -A -s work first. The Wi-Fi will still hiccup. It just won't matter anymore.