How to Manage Terminal Sessions in Linux Using Tmux

If you regularly manage remote Linux servers via SSH, you have undoubtedly experienced the terror of a dropped connection. You might be in the middle of a massive database migration, compiling a massive software package, or downloading a gigantic file, and suddenly, your Wi-Fi flickers. The SSH connection breaks, your terminal session dies, and your critical background task is forcefully terminated.

To completely eliminate this risk and radically improve your command-line workflow, you must learn how to use a terminal multiplexer. The undisputed king of Linux multiplexers is Tmux (Terminal Multiplexer).

What is Tmux and Why is it Essential?

Tmux sits between your SSH connection and your Linux shell. When you start a Tmux session, it runs continuously in the background on the server. If your SSH connection drops, your laptop dies, or you simply close your terminal window on purpose, the Tmux session remains perfectly intact and active.

Beyond session persistence, Tmux allows you to visually split your single terminal window into multiple horizontal and vertical panes. Instead of opening five separate SSH windows to monitor logs, run top, and edit configuration files, you can tile them perfectly within a single monitor.

How to Install Tmux on Ubuntu

Tmux is incredibly lightweight and is available in the default software repositories for virtually every major Linux distribution.

To install Tmux on Ubuntu, Debian, or Linux Mint, open your terminal and run:

sudo apt update
sudo apt install tmux

To verify the installation was successful, check the version by typing:

tmux -V

Starting and Detaching from Sessions

The core concept of Tmux revolves around “sessions”.

  1. To start a brand-new session, simply type tmux and press Enter. You will notice a green status bar appear at the bottom of your screen, indicating you are now inside the multiplexer.
  2. To create a named session (which is highly recommended for organisation), use the new-session command: tmux new -s MyDatabaseTask

Once you are inside the session, you can start a long-running process (like downloading a large file with wget). To safely exit the session while leaving your process running in the background, you must “detach”.

Tmux relies on a specific keyboard prefix to listen for commands. By default, this prefix is Ctrl+b.

  • To detach, press and release Ctrl+b, then press the d key.

Your screen will return to your standard terminal prompt, and you will see a message confirming that you have detached from the session. The session is now safely running in the background.

Reattaching to a Background Session

If your SSH connection drops and you reconnect to the server, you will want to jump straight back into your running session.

  1. To view a list of all active background sessions, run: tmux ls
  2. To reconnect to the last active session, type: tmux attach
  3. To reconnect to a specific named session, use the target flag: tmux attach -t MyDatabaseTask

How to Split Your Terminal into Multiple Panes

The most visually impressive feature of Tmux is window splitting. Remember, all of these commands require you to press and release the Ctrl+b prefix first.

  • Split Horizontally (Top/Bottom): Press Ctrl+b, then press the (double quote) key.
  • Split Vertically (Left/Right): Press Ctrl+b, then press the % (percent) key.
  • Navigate Between Panes: Press Ctrl+b, then use your keyboard’s directional Arrow Keys to move up, down, left, or right.
  • Close a Pane: Navigate to the pane you wish to close, type exit, and press Enter (or use the shortcut Ctrl+d).

Killing a Frozen Session

If a program completely freezes inside a Tmux session, you may need to force quit the entire session from the outside.

To completely terminate a session named “MyDatabaseTask”, run the following command from your standard terminal (not inside Tmux):

tmux kill-session -t MyDatabaseTask

By mastering these basic commands, Tmux will transform your terminal experience from fragile and chaotic into a highly resilient, professional workspace.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

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

Receive our best articles and tips delivered straight to your inbox.