Getty Images

Tip

How to use tmux sessions to manage remote connections

Learn the basics of using the terminal multiplexer (tmux) for managing remote connections. Walk through the installation process and configuration options for tmux.

Linux administrators often juggle several responsibilities, such as maintaining connections to multiple servers to manage services, maintaining software, updating configurations and reviewing log files. Instead of repeatedly establishing and closing connections, it's easier and more time-efficient to establish multiple terminals from a single workstation's shell.

The terminal multiplexer (tmux) application enables administrators to connect and disconnect from multiple sessions without closing terminals, which would exit the processes running in them.

This article gets administrators started with tmux. It covers the installation process, basic usage, configuration options and the essential key bindings that make tmux such a useful tool.

Primary benefits and features of tmux

There are several primary benefits to integrating tmux into a standard Linux system administration workflow. These benefits include:

  • Maintaining persistent sessions that run after disconnecting, enabling long-running tasks such as software compilation or script execution to continue.
  • Providing vertical and horizontal split-screen panes for viewing multiple sessions in the local shell.
  • Enabling key bindings for efficient task switching.

Most administrators establish an SSH connection and then run tmux. SSH provides security, and tmux offers flexibility.

In addition to persistent sessions, tmux enables many other use cases, such as:

  • Splitting terminal windows into multiple panes to run several applications simultaneously, such as system administration tasks and Python development.
  • Connecting multiple users to one session for collaboration, pair programming or troubleshooting.
  • Conducting continuous monitoring of multiple systems, including various log files and services.

How to install tmux

Tmux offers various installation options for Linux and macOS, since most current distributions do not typically include it. The installation process assumes the OS is using the preferred package manager. Like many Linux applications, tmux is open source. Users can download the source code and compile the program themselves.

Red Hat-based Linux distributions

For distributions such as RHEL, Fedora, AlmaLinux and Rocky Linux, type:

dnf install tmux

Screenshot of tmux installation details on Fedora.
Details of a tmux package installed on Fedora system (version 3.3a, aarch64 architecture).

Debian-based distributions

For Debian-based distributions, including Ubuntu and Linux Mint, type:

apt install tmux

macOS

Mac users frequently rely on the Homebrew package manager. After installing Homebrew, run the following command to add tmux:

brew install tmux

Windows

Windows users must rely on the Windows Subsystem for Linux (WSL). After installing WSL, use the apt package manager to install tmux:

apt install tmux

Users might also run tmux on OpenBSD, FreeBSD and NetBSD.

How to perform basic actions in tmux

Run the application by typing the tmux command. Once it's running, use the Ctrl+B combination to initiate commands. Pressing Ctrl+B on a Linux system alerts tmux that the next key presses are commands for tmux itself.

Screenshot of a new tmux session in the terminal.
A tmux session with a status bar at the bottom that shows the active shell, host name and timestamp.

For example, disconnecting from a tmux session relies on the d command. Make sure to press Ctrl+B first, alerting tmux that the subsequent command -- the D disconnect command -- applies to it.

Start and detach from sessions

Begin by starting a new tmux session. Give it a unique session name -- something descriptive. Once the session is running, it will continue executing scripts and commands after disconnecting from the session.

Create a new session named backup-script by typing this command:

tmux new -s backup-script

Screenshot of starting, detaching from and listing tmux sessions.
Starting a session called backup-script, detaching from that session and listing running tmux sessions, including both the default session (0) and the new backup-script session.

After launching the tmux session, use SSH to connect to a remote system. For example, the command within the tmux window might resemble ssh [email protected].

Use the SSH session to launch backup scripts, conduct sysadmin tasks or update software.

Detach from the tmux session by using the D key. Don't forget to use Ctrl+B to inform tmux that the next command applies to it.

Ctrl+B

D

Screenshot of the user detaching from the default tmux session.
Detaching from the default tmux session after starting it.

Display existing sessions

Display existing sessions by using the tmux ls command, as seen below.

tmux ls

Screenshot of the user listing existing tmux sessions.
Displaying the existing tmux sessions including backup-script and 0.

Reattach to sessions

Reattach to a running session by using the attach subcommand and specifying the session name.

tmux attach -t backup-script

This example shows why descriptive names are important.

Kill sessions

Kill an existing session by using the following command:

tmux kill-session -t backup-script

Screenshot of the user killing the backup-script session, then listing remaining sessions.
Killing the backup-script session, then listing the existing sessions, which now only includes session 0.

How to use copy mode in tmux

The standard local command history feature within a Linux distribution might not work reliably with tmux. Instead, rely on tmux's own copy mode.

Copy mode lets the user select and copy commands from the terminal history and paste them into tmux panes.

The tmux copy mode process follows six steps.

  1. Press Ctrl+B and then the [ key.
  2. Use navigation keys -- typically the arrow keys -- to move to the desired text.
  3. Press Space to begin the text selection process.
  4. Use the navigation keys to select the text.
  5. Press Enter after selecting the text.
  6. Paste the text using the ] key.

Quick reference guide of tmux key bindings

Use the following quick reference list to take advantage of the available key bindings.

  • Ctrl+B C. Create a new window.
  • Ctrl+B N. Switch to the next window.
  • Ctrl+B P. Switch to the previous window.
  • Ctrl+B W. List all windows to select one.
  • Ctrl+B %. Split the current pane vertically.
  • Ctrl+B ". Split the current pane horizontally.
  • Ctrl+B D. Detach from the current session.
Screenshot displaying multiple windows in a tmux session.
Displaying one tmux session split both vertically and horizontally into multiple panes.

Tmux includes plenty of additional key bindings, but these are sufficient to get started.

Basic customization options

Tmux relies on the ~/.tmux.conf file. Because it's stored in the user's home folder, it is not a global configuration, enabling administrators to customize tmux to their own preferences. Tmux doesn't create the file automatically, although it does check for it when launched. Users must create the file and add customizations if they want to personalize tmux.

Some common configuration options include:

  • Adjusting the tmux prefix command from the Ctrl+B default to Ctrl+A or similar.
  • Enabling status bar colors, borders and styles for easier pane identification.
  • Configuring custom key bindings.
  • Automatically running specific commands, scripts or programs upon starting a new tmux session.

Advanced users can add the Tmux Plugin Manager to their system. This utility enables many additional customizations using community-developed plugins.

Using tmux with SSH

SSH provides a critical method of securely connecting to remote systems. Tmux extends its flexibility by enabling multiple running remote sessions. Users can reattach to these sessions later, and they can even reattach from different systems. As an example, this makes it possible to execute a program on a remote server from a workstation at an office and then verify its status from a home computer.

Damon Garn owns Cogspinner Coaction and provides freelance IT writing and editing services. He has written multiple CompTIA study guides, including the Linux+, Cloud Essentials+ and Server+ guides, and contributes extensively to Informa TechTarget, The New Stack and CompTIA Blogs.

Dig Deeper on Software testing tools and techniques