tmux: Keep Your Terminal Alive When the SSH Connection Dies

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

3 min read

By Tux Harlow — Solnyxus editorial voice.

When an SSH connection drops, the shell on the other end usually goes with it, along with whatever was running in the foreground. tmux sessions solve this by running your shell inside a server process that does not care whether you are connected. This post covers the commands you need to start using tmux today.

Starting and naming tmux sessions

Bash
tmux new -s work

This starts a new session named work. Naming sessions makes them easier to find later.

Bash
tmux ls

This lists the sessions running on the machine.

Bash
tmux attach -t work

This attaches to the session named work.

Bash
tmux new -A -s work

This attaches to work if it exists and creates it if it does not, which is a convenient single command to remember.

The prefix key and detaching

Every tmux shortcut starts with the prefix key, which is Ctrl-b by default. You press the prefix, release it, then press the command key.

To detach, press Ctrl-b then d. You return to your normal shell and the session keeps running. If your connection drops instead, tmux behaves the same way: the session continues and you can reattach.

Windows and panes

A session contains windows, and each window can be split into panes.

  • 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.

Scrolling back

Because tmux draws its own screen, your terminal's scrollbar does not see its history. Press Ctrl-b [ to enter copy mode, then use the arrow keys or Page Up to scroll. Press q to leave copy mode.

The default history limit is 2000 lines, which runs out quickly when following logs.

A small tmux.conf

tmux reads ~/.tmux.conf, and since tmux 3.1 it also looks for ~/.config/tmux/tmux.conf. A short configuration helps:

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

The first line enables the mouse for selecting panes, resizing them and scrolling. The second raises the scrollback to 50,000 lines. The third numbers windows from 1, so they match the number row on the keyboard.

To reload the configuration without restarting:

Bash
tmux source-file ~/.tmux.conf

Ending sessions

Bash
tmux kill-session -t work

This ends the session named work and everything running in it. Typing exit in the last pane of the last window has the same effect.

A note on logout

Some systems are configured to kill a user's processes when they log out, through systemd-logind's KillUserProcesses=yes setting in logind.conf. If your tmux sessions disappear when you log out, check that setting. Most distributions ship with it set to no.

Conclusion

tmux is a small tool with a large effect on daily work. Start with tmux new -A -s work, learn detach and the pane splits, and add the three configuration lines. Overall, it is one of the most useful habits a Linux user can build.