How to Use tmux for Persistent Server Terminal Sessions

Starting a long-running command over SSH, then having your connection drop and losing everything, is one of the more avoidable frustrations in server ad…

Starting a long-running command over SSH, then having your connection drop and losing everything, is one of the more avoidable frustrations in server administration. tmux persistent terminal sessions solve this permanently, keeping your terminal session alive on the server itself regardless of whether your SSH connection stays connected.

What Is tmux?

tmux (terminal multiplexer) lets you create terminal sessions that continue running on the server independently of your SSH connection. Disconnect, close your laptop, or lose your internet connection entirely, and whatever was running inside your tmux session keeps going, ready to be reattached to exactly where you left off the next time you connect.

Why This Matters for Server Work

Beyond just surviving dropped connections, tmux lets you:

  • Start a long-running task (a large file transfer, a lengthy compilation, an interactive script) and safely disconnect without interrupting it
  • Split a single terminal window into multiple panes, monitoring several things simultaneously without needing separate SSH connections
  • Reconnect from a completely different device later and resume exactly where you left off

Installing tmux

sudo apt install tmux -y

Starting a New Session

tmux new -s mysession

The -s flag names the session, making it easier to identify later if you have several running simultaneously.

Detaching from a Session

Press Ctrl+b, then d to detach from the current session, leaving whatever’s running inside it active on the server. Your SSH connection can now be safely closed without interrupting anything.

Reattaching to a Session

After reconnecting via SSH:

tmux attach -t mysession

Everything inside the session — running commands, terminal output, scroll history — appears exactly as you left it.

Listing Active Sessions

If you’ve forgotten a session name or want to see what’s currently running:

tmux ls

Splitting Panes

Within an active tmux session, split the current window into multiple panes:

  • Ctrl+b, then % – split vertically
  • Ctrl+b, then " – split horizontally

Navigate between panes with Ctrl+b, then an arrow key.

Creating Multiple Windows Within a Session

Beyond panes, tmux supports multiple full windows within a single session:

  • Ctrl+b, then c – create a new window
  • Ctrl+b, then a number key (09) – switch to a specific window
  • Ctrl+b, then n or p – move to the next or previous window

A Practical Example: Monitoring a Long Backup

tmux new -s backup-job

Inside the session, start your backup command, then detach with Ctrl+b, d and disconnect entirely. Reconnect hours later:

tmux attach -t backup-job

The backup’s progress output appears exactly as it would have if you’d stayed connected the entire time.

Killing a Session When Finished

Once you’re done with a session entirely:

tmux kill-session -t mysession

tmux vs screen

screen is an older, similar tool accomplishing much the same core goal — persistent terminal sessions surviving disconnection. tmux is generally preferred today for its more intuitive pane-splitting, active development, and more consistent, modern default keybindings, though screen remains available and functional on systems where it’s already the established convention.

Customizing tmux with a Configuration File

For frequent tmux users, a ~/.tmux.conf file lets you customize keybindings, appearance, and default behavior — for example, changing the default prefix key from Ctrl+b to something more comfortable, or enabling mouse support for easier pane navigation and resizing.

Final Thoughts

Using tmux for persistent terminal sessions eliminates one of the most needlessly frustrating aspects of remote server work: losing progress on something running simply because your SSH connection dropped. Whether you’re monitoring a long backup, running a lengthy compilation, or just want the convenience of split-pane monitoring, tmux quickly becomes an essential habit for anyone regularly working on remote servers.

Related Posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *