Learn to Use the tmux Terminal Multiplexer on Oracle Linux

4
0
Send lab feedback

Learn to Use the tmux Terminal Multiplexer on Oracle Linux

Introduction

This tutorial provides step-by-step procedures for controlling multiple terminal windows in the same persistent session.

By default, terminating your SSH connection also terminates any remote terminal sessions started by that connection. If you use a terminal multiplexer, you can preserve those sessions for yourself and others to reuse. 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, but in this tutorial you will explore tmux. This tutorial is targeted at users of Oracle Linux 8 or later.

Objectives

Upon completion of this Lab you will be able 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

What do you need?

  • Any system with Oracle Linux 8

Install the tmux Terminal Multiplexer

Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and other usage instructions.

Install the tmux package on your instance by using the package manager, as follows:

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 that means you can reconnect to them if your connection times out, or if your user account has previously logged off.

  1. Start tmux without any parameters to create a new session and connect to it:
tmux
  1. List your currently open tmux sessions:
tmux ls
  1. Detach by pressing Ctrl+b, and then the d key.
  2. 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 always ensure that you have detached from your current tmux session before attempting to create or connect to any other tmux sessions.

  1. Rename your existing session to oracle:
tmux rename-session -t 0 oracle
  1. Detach by pressing Ctrl+b, and then the d key.
  2. Create a new tmux session called oracletemp:
tmux new -t oracletemp
  1. Detach by pressing Ctrl+b, and then the d key.
  2. List your open sessions:
tmux ls
  1. Reconnect to the oracle session:
tmux attach -t oracle

multiple tmux sessions

Manage terminal window panes

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.
  2. Switch to the next window by pressing Ctrl+b, and then the n key. You should notice an asterisk move between each numbered tab as you switch.
  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. Confirm the action.
  6. Rename the first window by pressing Ctrl+b, and then the comma (,) key. Set the new name to lab.

multiple panes in same tmux session

Manage split terminal window panes

You can split the current terminal window pane to suit your workflow. For example, you could input commands on one half of the terminal pane, and review the output for those commands on the other half.

split panes in same tmux session

  • You can split the current pane vertically by pressing Ctrl+b, and then the modulo (%) key.
  • You can split the current pane horizontally by pressing Ctrl+b, and then the quotation mark (") key.
  • You can switch between split panes by pressing Ctrl+b, and then the direction keys on your keyboard.
  • You can close any of the panes by pressing Ctrl+b, and then the x key. Confirm the action.

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_COMPUTE_INSTANCE>
  1. Reconnect to your tmux session:
tmux attach -t oracle

Terminate tmux sessions

  1. List your sessions:
tmux ls
  1. Detach by pressing Ctrl+b, and then the d key.
  2. Terminate your sessions.
  • Terminate the oracletemp session:
    tmux kill-session -t oracletemp
  • Terminate every tmux session simultaneously with a single command:
    tmux kill-server
SSR