How to Use Ubuntu Server tmux for Persistent Terminal Sessions

If you connect to an Ubuntu Server via SSH and start a script that takes three hours to compile, you are tied to that specific network connection. If your laptop loses Wi-Fi, or if your SSH client crashes, the session terminates immediately, taking your compilation process down with it. To prevent this, system administrators use terminal multiplexers. While screen is the legacy choice, tmux (Terminal Multiplexer) is the modern standard for creating persistent, detachable terminal sessions that survive SSH disconnects.

Step 1: Install tmux

Tmux is incredibly lightweight and available in the default Ubuntu repositories.

sudo apt update && sudo apt install tmux -y

Step 2: Start a New Session

To start a new, persistent terminal session, simply type:

tmux

Your screen will clear, and you will see a green status bar at the bottom. You are now inside the tmux environment. Any command you run here will continue running even if you close your SSH window.

Pro Tip: It is best practice to name your sessions so you can easily identify them later.

tmux new -s compilation

Step 3: Detach from the Session

Once you start your long-running script (like a database backup or a kernel compile), you can safely “detach” from the tmux session, leaving it running in the background while returning you to your normal SSH prompt.

To detach, you must use the tmux prefix key. By default, the prefix is Ctrl+B.

  1. Press and release Ctrl+B.
  2. Press the D key (for detach).

You will see a message saying [detached (from session compilation)]. You can now safely close your SSH connection and go home.

Step 4: Reattach to the Session

When you reconnect to the server the next morning, you can seamlessly re-enter your running session.

First, list the active sessions to find the name:

tmux ls

You will see output like: compilation: 1 windows (created Thu Aug 24 10:00:00 2026).

To reattach to it, use the attach command (or a for short) with the -t flag (for target):

tmux a -t compilation

You will instantly be dropped exactly where you left off, viewing the output of your finished script.

Step 5: Managing Windows and Panes

Tmux isn’t just for backgrounding processes; it allows you to split your screen into multiple panes and tabs (windows), entirely through keyboard shortcuts, without needing a graphical interface.

Remember, always press Ctrl+B before pressing the following command keys:

  • Ctrl+B, C: Create a new window (tab).
  • Ctrl+B, N: Move to the Next window.
  • Ctrl+B, P: Move to the Previous window.
  • Ctrl+B, %: Split the current screen vertically.
  • Ctrl+B, “: Split the current screen horizontally.
  • Ctrl+B, [Arrow Keys]: Move your cursor between the split panes.

By mastering tmux, you can transform a single, fragile SSH connection into a robust, multi-tasking workspace that never drops a running job.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.