Install and Use tmux on Linux | Best Terminal Multiplexing

Posted on

Install and Use tmux on Linux | Best Terminal Multiplexing

Install and Use tmux on Linux | Best Terminal Multiplexing

In this guide from the Linux Tutorials, we intend to teach you How To Install and Use tmux on Linux. The screen package is deprecated and not included in RHEL 8. But you can use the tmux package instead of the screen.

Tmux is a Linux application that allows multitasking in a terminal window. It stands for Terminal Multiplexing and is based around sessions. Users can start a process, switch to a new one, detach from a running process, and reattach to a running process.

Some users like Tmux better and some are more comfortable with Screen. Tmux, however, is deemed more user-friendly than the other. On the other hand, Screen allows a user to share a session with other users but this feature is missing in Tmux.

To complete this guide, you must log in to your server as a non-root user with sudo privileges. You can use any Linux distro.

1. Install the tmux Command on Linux

The tmux packages are available in the default repositories. To install tmux on Centos 7 and RHEL 7, you can use the command below:

sudo yum install tmux

You can use the command below to install tmux on Debian and Ubuntu:

sudo apt install tmux

To install tmux on RHEL 8, you can use the command below:

sudo dnf -y install tmux

When your installation is completed, you can verify it by checking the tmux version:

tmux -V
tmux Command on Linux

2. Work with tmux on Linux | Terminal Multiplexing

In Tmux, you will be working with sessions, windows, and panes.

Use tmux panes, windows, and sessions to organize your tasks logically.

3. Start a New Session in tmux

To start a new session in tmux, simply type the following command in your Linux terminal:

tmux

Once you are in tmux, the only thing that will be visibly different is the ever-present green bar at the bottom.

tmux terminal

Now you can run your tmux commands in your session. For example, use the following command to get a list of all commands:

Ctrl+b ?

Note: tmux command usage may differ from version to version. Your Linux tmux version might not support all of the commands listed below. For more information see the official tmux changelog.

4. Name Linux tmux Sessions

To start a new session with a specific name in tmux, you can use the following command in your Linux terminal:

tmux new -s [<mark>name of session</mark>]

5. Split Pane tmux

In a GUI desktop environment, you have Windows. In tmux, you have panes. Like windows in a GUI, these panes allow you to interact with multiple applications and similarly can be opened, closed, resized, and moved.

Tmux allows splitting the screen both horizontally and vertically.

Split the screen horizontally: CTRL+b+"

Split the screen vertically: CTRL+b+%

Resize Tmux Panes

You can change the size of each pane. To do so, press CTRL+b+:.

The bottom bar will change from green to yellow. Now you can type a command to resize the Linux tmux pane:

  1. resize-pane -D – Moves the boundary line for the pane downward.

  2. resize-pane -U – Moves the boundary line for the pane upward.

  3. resize-pane -R – Moves the boundary line for the pane right.

  4. resize-pane -L – Moves the boundary line for the pane left.

You may also specify a specific number of cells to move the boundary line. For example:

resize-pane -U 10 – Moves the boundary line up 10 cells.

You can specify a different cell than the one you’re working in. To resize Cell 2 (lower right):

resize-pane –t 2 --R 5 – Moves the boundary line 5 cells to the right.

6. Detach and Reattach tmux Sessions

With tmux, you can keep a process working in the background.

To detach from the current session, you can use:

tmux detach

Your system will drop to a normal command line. There should be an output that reads [detached (from session X)].

To reattach the session, you can use:

tmux attach

The system will re-enter the live Linux tmux session, and pick up just where you left off.

To attach to a specifically named session, use:

tmux a -t <mark>session_name</mark>

7. List Active tmux Sessions

You can easily list your tmux active session by using the command below:

tmux ls
**<mark>Example Output
</mark>**0: 1 windows (created Sat Nov 12 03:55:44 2022) [80x23] (attached)

8. Work with tmux Window Screens

Your screen can become cluttered if you have too many panes open. You can create a new full-screen window by entering CTRL+b+c.

To rename a window, switch to it and use the comma key: CTRL+b+,

You can simply switch to the next window by pressing: CTRL+b+n

To switch to the previous window press: CTRL+b+p

You can display an interactive list of windows with CTRL+b+w.

Use the up/down arrow keys to select the window you want to use, then press enter.

Also, you can close a tmux window, by using the: CTRL+b+&

9. Configure tmux on Linux

The tmux is so highly configurable. You can edit the tmux.conf file to make changes.

To create custom changes for a single user, create the file in the user’s home directory ~/.tmux.conf. To create system-wide changes, create the file in the system directory /etc/tmux.conf.

Change Activation Key

By default, tmux uses the CTRL+b combination to activate functions. To do this, open the file with your favorite text editor, here we use vi:

sudo vi /etc/tmux.conf

Add the following lines to the file:

unbind C-b
set –g prefix C-a

Save the changes and exit. Now, whenever you use tmux, you’ll use CTRL+a to activate functions.

Note: If you have any active tmux sessions, the changes won’t take effect until they are closed and restarted. Detaching will not work.

Change Status Bar Appearance

Open the configuration file for editing:

sudo vi /etc/tmux.conf

Add the following lines to the file:

# Status bar colors
set –g status-bg blue
set –g status-fg black

# highlight and display
setw –g monitor-activity on
setw –g visual-activity on

Save the changes and exit the file.

Conclusion

At this point, you have learned to Install and Use tmux on Linux. The tmux command in Linux is used for terminal multiplexing, allowing users to manage multiple terminal sessions within a single window.

Hope you enjoy it. You may also like these articles:

Disable NetworkManager on AlmaLinux 8

How To Install GitLab on Debian 11

Check Linux System and Hardware Information

Find the PID of a Process in Linux Terminal

Alternative Solutions to Terminal Multiplexing

While tmux is a powerful and widely used terminal multiplexer, there are alternative approaches to achieving similar functionalities. Here are two different ways to solve the problem of managing multiple terminal sessions:

1. Using GNU Screen

GNU Screen is another popular terminal multiplexer. It is similar to tmux in functionality but has a different command structure and configuration. Screen is older than tmux and pre-installed on many Linux distributions. It allows users to create, access, and manage multiple terminal sessions inside a single window.

Explanation:

Instead of relying on tmux, you can use GNU Screen to manage multiple terminal sessions. Screen provides the ability to detach and reattach to sessions, split the screen into regions, and switch between windows. While tmux is often preferred for its modern features and configurability, Screen is a viable alternative, especially if it’s already available on your system or if you prefer its command syntax.

Installation (if not already installed):

sudo apt install screen  # Debian/Ubuntu
sudo yum install screen  # CentOS/RHEL
sudo dnf install screen  # Fedora

Basic Usage:

  • Start a new screen session: screen
  • Detach from the current session: Ctrl+a d
  • List active screen sessions: screen -ls
  • Reattach to a detached session: screen -r <session_id> (e.g., screen -r 12345)
  • Create a new window: Ctrl+a c
  • Switch to the next window: Ctrl+a n
  • Switch to the previous window: Ctrl+a p
  • Split the screen horizontally: Ctrl+a S
  • Switch between regions: Ctrl+a Tab
  • Close a window: type exit in the window.

Example Scenario:

Let’s say you’re running a long-running process and you want to disconnect without interrupting it.

  1. Start a screen session: screen
  2. Run your long-running process (e.g., a script or server).
  3. Detach from the session: Ctrl+a d
  4. Later, reattach to the session: screen -r (if only one session is active) or screen -r <session_id>.

2. Using Multiple Terminal Tabs/Windows within a Terminal Emulator

Most modern terminal emulators (like GNOME Terminal, Konsole, iTerm2) support multiple tabs or windows within a single application instance. While this approach doesn’t offer the detach/reattach functionality of tmux or Screen, it can be a simpler solution for basic multitasking within a single desktop session.

Explanation:

Instead of using a dedicated terminal multiplexer, you can leverage the built-in tab or window management features of your terminal emulator. This is especially useful for simple tasks where you don’t need to preserve sessions across disconnections or share sessions. Each tab or window acts as an independent terminal, allowing you to run different commands or applications concurrently.

Usage:

  • Open a new tab: Typically Ctrl+Shift+T or Ctrl+T (check your terminal emulator’s settings).
  • Open a new window: Typically Ctrl+Shift+N or Ctrl+N (check your terminal emulator’s settings).
  • Switch between tabs/windows: Typically Ctrl+Page Up/Page Down or Ctrl+Tab/Ctrl+Shift+Tab (check your terminal emulator’s settings).

Example Scenario:

Suppose you are working on a programming project.

  1. Open your terminal emulator.
  2. Open one tab to edit your code.
  3. Open another tab to compile and run your code.
  4. Open a third tab to run version control commands (e.g., git).

This way, you can easily switch between different aspects of your project without needing to detach or reattach to sessions.

While these alternatives lack some of the advanced features of tmux, they offer simpler and often readily available solutions for managing multiple terminal sessions. The best approach depends on your specific needs and workflow. Install and Use tmux on Linux provides the best solution for many users.

Leave a Reply

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