How to Use the ‘tmux’ Terminal Multiplexer to Keep SSH Sessions Alive

The Danger of Broken SSH Connections

If you have ever connected to a remote Linux server via SSH to compile software or run a massive database migration, you know the anxiety of a dropped Wi-Fi signal. If your laptop loses its connection for even a second, the SSH tunnel collapses. Any command that was running on the remote server is instantly terminated. A process that was 90% complete is destroyed, and you must start over from the beginning.

To solve this, Linux professionals use a Terminal Multiplexer. A multiplexer is a program that runs on the remote server and acts as a protective shell around your terminal session. If your SSH connection drops, the multiplexer keeps the session running safely in the background. When you reconnect, you simply “re-attach” to the multiplexer and pick up exactly where you left off.

While GNU Screen is the legacy choice, tmux is the modern, vastly superior standard.

Installing and Starting tmux

If tmux is not already installed on your server, you can grab it from your package manager (e.g., sudo apt install tmux or sudo yum install tmux).

To start a protected session, SSH into your server and simply type:

tmux

Your screen will clear, and a green status bar will appear at the very bottom of your terminal window. You are now inside a tmux session. Any command you run now is protected from network failures.

The Prefix Key (Ctrl+B)

To talk to tmux (instead of typing commands into bash), you must use the Prefix Key. By default, this is Ctrl + B.

You press and release Ctrl+B, and then you press a command key.

Detaching and Re-attaching (The Lifesaver)

Assume you start a massive Python script that will take 5 hours to run. You don’t want to leave your laptop open for 5 hours.

  1. Press Ctrl+B, then press D (for Detach).
  2. You will be kicked out of the tmux environment back to your standard SSH prompt, with a message like: [detached (from session 0)].
  3. The Python script is still running perfectly inside the detached tmux shell. You can safely type exit to close your SSH connection and shut your laptop.

When you want to check on the script 5 hours later:

  1. SSH back into the server.
  2. Type the re-attach command:
tmux attach

You will instantly be dropped back into your session, watching the Python script finish.

Managing Multiple Windows and Panes

tmux is called a multiplexer because it can multiplex (split) a single SSH connection into multiple virtual windows and panes.

Creating Windows (Tabs)

You can create multiple full-screen terminal windows within a single tmux session (similar to browser tabs).

  • Create a new window: Ctrl+B, then C
  • Switch to the next window: Ctrl+B, then N
  • Switch to the previous window: Ctrl+B, then P

Look at the green status bar at the bottom; you will see an asterisk (*) next to the window you are currently viewing.

Creating Panes (Split Screen)

You can split your current screen to view two terminal outputs simultaneously (e.g., watching a log file tail in the bottom pane while typing commands in the top pane).

  • Split the screen horizontally (top/bottom): Ctrl+B, then " (double quote)
  • Split the screen vertically (left/right): Ctrl+B, then %
  • Switch between panes: Ctrl+B, then use the Arrow Keys
  • Close a pane: Type exit or press Ctrl+D in the pane you want to close.

Conclusion

Using SSH without tmux is a dangerous gamble. By spending five minutes learning the basic tmux commands-specifically how to detach and re-attach-you insulate your critical server processes from the unpredictable nature of local network connections.

Get the best tech tips delivered straight to your inbox.

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