Use the tmux Terminal Multiplexer on Oracle Linux

4
0
Send lab feedback

Use the tmux Terminal Multiplexer on Oracle Linux

Introduction

As a default, when your SSH connection terminates, any remote terminal sessions started by that connection also terminate. You can avoid that issue and reuse those sessions for yourself and others by using the tmux terminal multiplexer. You can also manage more complex tasks from a single SSH connection, as a terminal multiplexer can provide browser-style tabs for each task and even divide up your screen with multiple terminal sessions called panes.

In previous versions of Oracle Linux, you might have encountered a similar tool called screen. This tool has been around for years and is still available in the Oracle developer EPEL repository. However, we'll focus on and explore tmux, which Oracle ships in the base repository.

Objectives

In this tutorial, you'll learn how to:

  • Connect and disconnect from a persistent tmux session
  • Display, hide, and switch between multiple terminals in the same tmux session
  • Manage several persistent tmux sessions

Prerequisites

  • Minimum of a single Oracle Linux system

  • Each system should have Oracle Linux installed and configured with:

    • A non-root user account with sudo access
    • Access to the Internet

Deploy Oracle Linux

Note: If running in your own tenancy, read the linux-virt-labs GitHub project README.md and complete the prerequisites before deploying the lab environment.

  1. Open a terminal on the Luna Desktop.

  2. Clone the linux-virt-labs GitHub project.

    git clone https://github.com/oracle-devrel/linux-virt-labs.git
  3. Change into the working directory.

    cd linux-virt-labs/ol
  4. Install the required collections.

    ansible-galaxy collection install -r requirements.yml
  5. Deploy the lab environment.

    ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6"

    The free lab environment requires the extra variable local_python_interpreter, which sets ansible_python_interpreter for plays running on localhost. This variable is needed because the environment installs the RPM package for the Oracle Cloud Infrastructure SDK for Python, located under the python3.6 modules.

    The default deployment shape uses the AMD CPU and Oracle Linux 8. To use an Intel CPU or Oracle Linux 9, add -e instance_shape="VM.Standard3.Flex" or -e os_version="9" to the deployment command.

    Important: Wait for the playbook to run successfully and reach the pause task. At this stage of the playbook, the installation of Oracle Linux is complete, and the instances are ready. Take note of the previous play, which prints the public and private IP addresses of the nodes it deploys and any other deployment information needed while running the lab.

Install the tmux Terminal Multiplexer

  1. Open a terminal and connect via SSH to the ol-node-01 instance.

    ssh oracle@<ip_address_of_instance>
  2. Install the tmux package using the package manager on your instance.

    sudo dnf install -y tmux

Start and Connect to a tmux Session

Note: tmux sessions are not preserved between reboots, so they are best suited for persistent remote servers.

The tmux command rearranges your terminal window, with a green band running along the bottom that lists the currently active panes. By default, sessions and panes are ordered numerically according to dynamically assigned ID numbers that count upwards from zero.

All tmux sessions are hosted in a background service, so you can reconnect if your connection times out or your user account has previously logged off.

  1. Start tmux without any parameters to create a new session and connect to it.

    tmux
  2. List your currently open tmux sessions.

    tmux ls

    Example Output:

    oracle@ol-node-01 ~]$ tmux ls
    0: 1 windows (created Thu Aug 22 15:34:11 2024) [132x23] (attached)
    
    
    ...
    
    
    [0] 0:bash*                                                                                             "ol-node-01" 15:38 22-Aug-24
  3. Detach by pressing Ctrl+b, and then the d key.

  4. Reattach to your persistent tmux session.

    tmux attach -t 0

You can run tmux ls from inside or outside tmux to verify which sessions are available for you to reopen.

Manage Multiple tmux Sessions

Note: You cannot create nested tmux sessions by default, so ensure you have detached from your current tmux session before attempting to create or connect to any other tmux sessions.

  1. Rename your existing session from the default value of [0] to oracle.

    The terminal shows the tmux session name on the status bar.

    tmux rename-session -t 0 oracle
  2. Detach by pressing Ctrl+b, and then the d key.

  3. Create a new tmux session called oracletemp.

    tmux new -t oracletemp
  4. Detach by pressing Ctrl+b, and then the d key.

  5. List your open sessions.

    tmux ls

    Example Output:

    [oracle@ol-node-01 ~]$ tmux ls
    oracle: 1 windows (created Thu Aug 22 15:34:11 2024) [132x23]
    oracletemp-1: 1 windows (created Thu Aug 22 15:54:17 2024) [132x23] (group oracletemp)
  6. Reconnect to the oracle session.

    tmux attach -t oracle

Manage tmux Windows

Terminal window panes behave much like web browser tabs, where you can create, delete, and switch between them. Unlike a web browser, you must use keyboard shortcuts to perform those actions.

  1. Create a new terminal window by pressing Ctrl+b, and then the c key.

    You can confirm that a new tmux window was created by looking at the status bar, which lists two windows. An asterisk indicates the currently active window.

    Example Output:

    [0] 0:bash- 1:bash*                                                             "ol-node-01" 15:58 23-Aug-24
  2. Switch to the next window by pressing Ctrl+b, and then the n key.

    The asterisk indicating the currently active window moves between each numbered tab as you switch.

    Example Output:

    [0] 0:bash* 1:bash-                                                             "ol-node-01" 15:58 23-Aug-24
  3. Switch to the previous window by pressing Ctrl+b, and then the p key.

  4. Switch to the second window by pressing Ctrl+b, and then then the 1 key.

  5. Close the second window by pressing Ctrl+b, and then the ampersand (&) key.

    Enter y to confirm the action.

    Example Output:

    kill-window bash? (y/n)
  6. Rename the first window by pressing Ctrl+b, and then the comma (,) key.

    Example Output:

    (rename-window) lab                                                                                     

    Use the keyboard to replace the original name bash and update the new name to lab. Then press the Enter to confirm the change.

    Example Output:

    [0] 0:lab*                                                                      "ol-node-01" 15:58 23-Aug-24

Manage tmux Panes

tmux allows you to split the current terminal window into multiple panes to suit your workflow. For example, you could input commands on one pane and then review the output for those commands on the other.

  1. Split the current pane vertically by pressing Ctrl+b, and then the modulo (%) key.

  2. Split the current pane horizontally by pressing Ctrl+b, and then the quotation mark (") key.

  3. You can switch between split panes by pressing Ctrl+b, and then the direction keys on your keyboard.

    split panes in same tmux session

  4. Close any of the panes by pressing Ctrl+b, and then the x key.

    Confirm the action by pressing y.

  5. Create a new tmux window.

    Enter Ctrl+b, and then the c key to create a new tmux window. The split panes are no longer visible.

  6. Switch tmux windows.

    Now enter Ctrl+b and then the p key. The tmux window containing the panes you just created is displayed.

Reconnect to a tmux Session Over SSH

  1. Detach by pressing Ctrl+b, and then the d key.

  2. Run the exit command to end the SSH connection.

  3. Press the up direction key, and then reuse the command you previously executed to start a new SSH connection:

    ssh oracle@<ip_address_of_instance>
  4. Reconnect to your tmux session.

    tmux attach -t oracle

Terminate tmux Sessions

  1. List your sessions.

    tmux ls
  2. Detach by pressing Ctrl+b, and then the d key.

  3. Terminate your sessions.

    Terminate the oracletemp session:

    tmux kill-session -t oracletemp

    Terminate every tmux session simultaneously with a single command:

    tmux kill-server

Next Steps

That is the end of our introduction to connecting to a tmux session, persisting it, reconnecting to it, and switching between multiple tmux sessions on Oracle Linux. However, it barely scratches the surface of what tmux is capable of and how it can help you administer your Oracle Linux environments. Check out the tmux wiki for more details.

SSR