Linux Professional Institute Learning Logo.
Skip to main content
  • Home
    • All Resources
    • LPI Learning Materials
    • Become a Contributor
    • Publishing Partners
    • Become a Publishing Partner
    • About
    • FAQ
    • Contributors
    • Roadmap
    • Contact
  • LPI.org
103.5 Lesson 2
Topic 101: System Architecture
101.1 Determine and configure hardware settings
  • 101.1 Lesson 1
101.2 Boot the system
  • 101.2 Lesson 1
101.3 Change runlevels / boot targets and shutdown or reboot system
  • 101.3 Lesson 1
Topic 102: Linux Installation and Package Management
102.1 Design hard disk layout
  • 102.1 Lesson 1
102.2 Install a boot manager
  • 102.2 Lesson 1
102.3 Manage shared libraries
  • 102.3 Lesson 1
102.4 Use Debian package management
  • 102.4 Lesson 1
102.5 Use RPM and YUM package management
  • 102.5 Lesson 1
102.6 Linux as a virtualization guest
  • 102.6 Lesson 1
Topic 103: GNU and Unix Commands
103.1 Work on the command line
  • 103.1 Lesson 1
  • 103.1 Lesson 2
103.2 Process text streams using filters
  • 103.2 Lesson 1
103.3 Perform basic file management
  • 103.3 Lesson 1
  • 103.3 Lesson 2
103.4 Use streams, pipes and redirects
  • 103.4 Lesson 1
  • 103.4 Lesson 2
103.5 Create, monitor and kill processes
  • 103.5 Lesson 1
  • 103.5 Lesson 2
103.6 Modify process execution priorities
  • 103.6 Lesson 1
103.7 Search text files using regular expressions
  • 103.7 Lesson 1
  • 103.7 Lesson 2
103.8 Basic file editing
  • 103.8 Lesson 1
Topic 104: Devices, Linux Filesystems, Filesystem Hierarchy Standard
104.1 Create partitions and filesystems
  • 104.1 Lesson 1
104.2 Maintain the integrity of filesystems
  • 104.2 Lesson 1
104.3 Control mounting and unmounting of filesystems
  • 104.3 Lesson 1
104.5 Manage file permissions and ownership
  • 104.5 Lesson 1
104.6 Create and change hard and symbolic links
  • 104.6 Lesson 1
104.7 Find system files and place files in the correct location
  • 104.7 Lesson 1
  1. Topic 103: GNU and Unix Commands
  2. 103.5 Create, monitor and kill processes
  3. 103.5 Lesson 2

103.5 Lesson 2

Certificate:

LPIC-1

Version:

5.0

Topic:

103 GNU and Unix Commands

Objective:

103.5 Create, monitor and kill processes

Lesson:

2 of 2

Introduction

The tools and utilities seen in the previous lesson are very useful for process monitoring at large. However, a system administrator may need to go one step further. In this lesson we will discuss the concept of terminal multiplexers and learn about GNU Screen and tmux as — despite today’s modern and great terminal emulators — multiplexers still preserve some cool, powerful features for a productive system administrator.

Features of Terminal Multiplexers

In electronics, a multiplexer (or mux) is a device that allows for multiple inputs to be connected to a single output. Thus, a terminal multiplexer gives us the ability to switch between different inputs as required. Although not exactly the same, screen and tmux share a series of common features:

  • Any successful invocation will result in at least a session which — in turn — will include at least a window. Windows contain programs.

  • Windows can be split into regions or panes — which can aid in productivity when working with various programs simultaneously.

  • Ease of control: to run most commands, they use a key combination — the so-called command prefix or command key — followed by another character.

  • Sessions can be detached from their current terminal (that is, programs are sent to the background and continue to run). This guarantees complete execution of programs no matter if we accidentally close a terminal, experience an occasional terminal freeze or even remote connection loss.

  • Socket connection.

  • Copy mode.

  • They are highly customizable.

GNU Screen

In the earlier days of Unix (1970s - 80s) computers basically consisted of terminals connected to a central computer. That was all, no multiple windows or tabs. And that was the reason behind the creation of GNU Screen in 1987: emulate multiple independent VT100 screens on a single physical terminal.

Windows

GNU Screen is invoked just by typing screen into the terminal. You will first see a welcome message:

GNU Screen version 4.05.00 (GNU) 10-Dec-16

Copyright (c) 2010 Juergen Weigert, Sadrul Habib Chowdhury
Copyright (c) 2008, 2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib Chowdhury
Copyright (c) 1993-2002, 2003, 2005, 2006, 2007 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann
(...)

Press Space or Enter to close the message and you will be shown a command prompt:

$

It may seem nothing has happened but the fact is that screen has already created and managing its first session and window. Screen’s command prefix is Ctrl+a. To see all windows at the bottom of the terminal display, type Ctrl+a-w:

0*$ bash

There it is, our one and only window so far! Note that the count starts at 0, though. To create another window, type Ctrl+a-c. You will see a new prompt. Let us start ps in that new window:

$ ps
  PID TTY          TIME CMD
  974 pts/2    00:00:00 bash
  981 pts/2    00:00:00 ps

and type Ctrl+a-w again:

0-$ bash  1*$ bash

There we have our two windows (note the asterisk indicating the one that is being shown at the moment). However, as they were started with Bash, they are both given the same name. Since we invoked ps in our current window, let us rename it with that same name. For that, you have to type Ctrl+a-A and write the new window name (ps) when prompted:

Set window's title to: ps

Now, let us create another window but provide it a name from the start: yetanotherwindow. This is done by invoking screen with the -t switch:

$ screen -t yetanotherwindow

You can move between windows in different ways:

  • By using Ctrl+a-n (go to next window) and Ctrl+a-p (go to previous window).

  • By using Ctrl+a-number (go to window number number).

  • By using Ctrl+a-" to see a list of all windows. You can move up and down with the arrow keys and select the one you want with Enter:

    Num Name                                                Flags
    
       0 bash                                                   $
       1 ps                                                     $
       2 yetanotherwindow

While working with windows it is important to remember the following:

  • Windows run their programs completely independent of each other.

  • Programs will continue to run even if their window is not visible (also when the screen session is detached as we will see shortly).

To remove a window, simply terminate the program running in it (once the last window is removed, screen will itself terminate). Alternatively, use Ctrl+a-k while in the window you want to remove; you will be asked for confirmation:

Really kill this window [y/n]

Window 0 (bash) killed.

Regions

screen can divide a terminal screen up into multiple regions in which to accommodate windows. These divisions can be either horizontal (Ctrl+a-S) or vertical (Ctrl+a-|).

The only thing the new region will show is just -- at the bottom meaning it is empty:

   1 ps                                                               --

To move to the new region, type Ctrl+a-Tab. Now you can add a window by any of the methods that we have already seen, for example: Ctrl+a-2. Now the -- should have turned into 2 yetanotherwindow:

$ ps                                                  $
  PID TTY          TIME CMD
 1020 pts/2    00:00:00 bash
 1033 pts/2    00:00:00 ps
$ screen -t yetanotherwindow




   1 ps                                                                2 yetanotherwindow

Important aspects to keep in mind when working with regions are:

  • You move between regions by typing Ctrl+a-Tab.

  • You can terminate all regions except the current one with Ctrl+a-Q.

  • You can terminate the current region with Ctrl+a-X.

  • Terminating a region does not terminate its associated window.

Sessions

So far we have played with a few windows and regions, but all belonging to the same and only session. It is time to start playing with sessions. To see a list of all sessions, type screen -list or screen -ls:

$ screen -list
There is a screen on:
        1037.pts-0.debian       (08/24/19 13:53:35)     (Attached)
1 Socket in /run/screen/S-carol.

That is our only session so far:

PID

1037

Name

pts-0.debian (indicating the terminal — in our case a pseudo terminal slave — and the hostname).

Status

Attached

Let us create a new session giving it a more descriptive name:

$ screen -S "second session"

The terminal display will be cleared and you will be given a new prompt. You can check for sessions once more:

$ screen -ls
There are screens on:
        1090.second session     (08/24/19 14:38:35)     (Attached)
        1037.pts-0.debian       (08/24/19 13:53:36)     (Attached)
2 Sockets in /run/screen/S-carol.

To kill a session, quit out of all its windows or just type the command screen -S SESSION-PID -X quit (you can also provide the session name instead). Let us get rid of our first session:

$ screen -S 1037 -X quit

You will be sent back to your terminal prompt outside of screen. But remember, our second session is still alive:

$ screen -ls
There is a screen on:
	1090.second session	(08/24/19 14:38:35)	(Detached)
1 Socket in /run/screen/S-carol.

However, since we killed its parent session, it is given a new label: Detached.

Session Detachment

For a number of reasons you may want to detach a screen session from its terminal:

  • To let your computer at work do its business and connect remotely later from home.

  • To share a session with other users.

You detach a session with the key combination Ctrl+a-d. You will be taken back into your terminal:

[detached from 1090.second session]
$

To attach again to the session, you use the command screen -r SESSION-PID. Alternatively, you can use the SESSION-NAME as we saw above. If there is only one detached session, neither is compulsory:

$ screen -r

This command suffices to reattach to our second session:

$ screen -ls
There is a screen on:
        1090.second session      (08/24/19 14:38:35)     (Attached)
1 Socket in /run/screen/S-carol.

Important options for session reattaching:

-d -r

Reattach a session and — if necessary — detach it first.

-d -R

Same as -d -r but screen will even create the session first if it does not exist.

-d -RR

Same as -d -R. However, use the first session if more than one is available.

-D -r

Reattach a session. If necessary, detach and logout remotely first.

-D -R

If a session is running, then reattach (detaching and logging out remotely first if necessary). If it was not running create it and notify the user.

-D -RR

Same as -D -R — only stronger.

-d -m

Start screen in detached mode. This creates a new session but does not attach to it. This is useful for system startup scripts.

-D -m

Same as -d -m, but does not fork a new process. The command exits if the session terminates.

Read the manual pages for screen to find out about other options.

Copy & Paste: Scrollback Mode

GNU Screen features a copy or scrollback mode. Once entered, you can move the cursor in the current window and through the contents of its history using the arrow keys. You can mark text and copy it across windows. The steps to follow are:

  1. Enter copy/scrollback mode: Ctrl+a-[.

  2. Move to the beginning of the piece of text you want to copy using the arrow keys.

  3. Mark the beginning of the piece of text you want to copy: Space.

  4. Move to the end of the piece of text you want to copy using the arrow keys.

  5. Mark the end of the piece of text you want to copy: Space.

  6. Go to the window of your choice and paste the piece of text: Ctrl+a-].

Customization of screen

The system-wide configuration file for screen is /etc/screenrc. Alternatively, a user-level ~/.screenrc can be used. The file includes four main configuration sections:

SCREEN SETTINGS

You can define general settings by specifying the directive followed by a space and the value as in: defscrollback 1024.

SCREEN KEYBINDINGS

This section is quite interesting as it allows you to redefine keybindings that perhaps interfere with your everyday use of the terminal. Use the keyword bind followed by a Space, the character to use after the command prefix, another Space and the command as in: bind l kill (this setting will change the default way of killing a window to Ctrl+a-l).

To display all of screen’s bindings, type Ctrl+a-? or consult the manual page.

Tip

Of course, you can also change the command prefix itself. For example to go from Ctrl+a to Ctrl+b, just add this line: escape ^Bb.

TERMINAL SETTINGS

This section includes settings related to terminal window sizes and buffers — amongst others. To enable non-blocking mode to better cope with unstable ssh connections, for instance, the following configuration is used: defnonblock 5.

STARTUP SCREENS

You can include commands to have various programs running on screen startup; for example: screen -t top top (screen will open a window named top with top inside).

tmux

tmux was released in 2007. Although very similar to screen, it includes a few notable differences:

  • Client-server model: the server supplies a number of sessions, each of which may have a number of windows linked to it that can, in turn, be shared by various clients.

  • Interactive selection of sessions, windows and clients via menus.

  • The same window can be linked to a number of sessions.

  • Availability of both vim and Emacs key layouts.

  • UTF-8 and 256-colour terminal support.

Windows

tmux can be invoked by simply typing tmux at the command prompt. You will be shown a shell prompt and a status bar at the bottom of the window:

[0] 0:bash*                                                        "debian" 18:53 27-Aug-19

Other than the hostname, the time and the date, the status bar provides the following information:

Session name

[0]

Window number

0:

Window name

bash*. By default this is the name of the program running inside the window and — unlike screen — tmux will automatically update it to reflect the current running program. Note the asterisk indicating that this is the current, visible window.

You can assign session and window names when invoking tmux:

$ tmux new -s "LPI" -n "Window zero"

The status bar will change accordingly:

[LPI] 0:Window zero*                                                 "debian" 19:01 27-Aug-19

tmux’s command prefix is Ctrl+b. To create a new window, just type Ctrl+b-c; you will be taken to a new prompt and the status bar will reflect the new window:

[LPI] 0:Window zero- 1:bash*                                         "debian" 19:02 27-Aug-19

As Bash is the underlying shell, the new window is given that name by default. Start top and see how the name changes to top:

[LPI] 0:Window zero- 1:top*                                         "debian" 19:03 27-Aug-19

In any case, you can rename a window with Ctrl+b-,. When prompted, supply the new name and hit Enter:

(rename-window) Window one

You can have all windows displayed for selection with Ctrl+b-w (use the arrow keys to move up and down and enter to select):

(0)  0: Window zero- "debian"
(1)  1: Window one* "debian"

In a similar fashion to screen, we can jump from one window to another with:

Ctrl+b-n

go to next window.

Ctrl+b-p

go to previous window.

Ctrl+b-number

go to window number number.

To get rid of a window, use Ctrl+b-&. You will be asked for confirmation:

kill-window Window one? (y/n)

Other interesting window commands include:

Ctrl+b-f

find window by name.

Ctrl+b-.

change window index number.

To read the whole list of commands, consult the manual page.

Panes

The window-splitting facility of screen is also present in tmux. The resulting divisions are not called regions but panes, though. The most important difference between regions and panes is that the latter are complete pseudo-terminals linked to a window. This means that killing a pane will also kill its pseudo-terminal and any associated programs running within.

To split a window horizontally, we use Ctrl+b-":

Tasks:  93 total,   1 running,  92 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  4050960 total,  3730920 free,   114880 used,   205160 buff/cache
KiB Swap:  4192252 total,  4192252 free,        0 used.  3716004 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 1340 carol     20   0   44876   3400   2800 R  0.3  0.1   0:00.24 top
    1 root      20   0  139088   6988   5264 S  0.0  0.2   0:00.50 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.04 ksoftirqd/0
    4 root      20   0       0      0      0 S  0.0  0.0   0:01.62 kworker/0:0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S  0.0  0.0   0:00.06 rcu_sched
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
   10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain
   11 root      rt   0       0      0      0 S  0.0  0.0   0:00.01 watchdog/0
   12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 cpuhp/0
$
───────────────────────────────────────────────────────────────────────────────────────────────
$












[LPI] 0:Window zero- 1:Window one*                                      "debian" 19:05 27-Aug-19

To split it vertically, use Ctrl+b-%:

                                                                               │$
    1 root      20   0  139088   6988   5264 S  0.0  0.2   0:00.50 systemd     │
                                                                               │
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd    │
                                                                               │
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.04 ksoftirqd/0 │
    4 root      20   0       0      0      0 S  0.0  0.0   0:01.62 kworker/0:0 │
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H│
    7 root      20   0       0      0      0 S  0.0  0.0   0:00.06 rcu_sched   │
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh      │
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0 │
                                                                               │
   10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drai│
n                                                                              │
   11 root      rt   0       0      0      0 S  0.0  0.0   0:00.01 watchdog/0  │
                                                                               │
   12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 cpuhp/0     │
                                                                               │
$                                                                              │
───────────────────────────────────────────────────────────────────────────────┴───────────────
$















[LPI] 0:Window zero- 1:Window one*                                      "debian" 19:05 27-Aug-19

To destroy the current pane (along with its pseudo-terminal running within it, along with any associated programs), use Ctrl+b-x. You will be asked for confirmation on the status bar:

kill-pane 1? (y/n)

Important pane commands:

Ctrl+b-↑,↓,←,→

move between panes.

Ctrl+b-;

move to the last active pane.

Ctrl+b-Ctrl+arrow key

resize pane by one line.

Ctrl+b-Alt+arrow key

resize pane by five lines.

Ctrl+b-{

swap panes (current to previous).

Ctrl+b-}

swap panes (current to next).

Ctrl+b-z

zoom in/out panel.

Ctrl+b-t

tmux displays a fancy clock inside the pane (kill it by pressing q).

Ctrl+b-!

turn pane into window.

To read the whole list of commands, consult the manual page.

Sessions

To list sessions in tmux you can use Ctrl+b-s:

(0) + LPI: 2 windows (attached)

Alternatively, you can use the tmux ls command:

$ tmux ls
LPI: 2 windows (created Tue Aug 27 19:01:49 2019) [158x39] (attached)

There is only one session (LPI) which includes two windows. Let us create a new session from within our current session. This can be achieved by using Ctrl+b, type in :new at the prompt, then press Enter. You will be sent to the new session as can be observed on the status bar:

[2] 0:bash*                                                       "debian" 19:15 27-Aug-19

By default tmux named the session 2. To rename it, use Ctrl+b-$. When prompted, supply the new name and hit Enter:

(rename-session) Second Session

You can switch sessions with Ctrl+b-s (use the arrow keys and enter):

(0) + LPI: 2 windows
(1) + Second Session: 1 windows (attached)

To kill a session, you can use the command tmux kill-session -t SESSION-NAME. If you type the command from within the current, attached session, you will be taken out of tmux and back to your initial terminal session:

$ tmux kill-session -t "Second Session"
[exited]
$

Sessions Detachment

By killing Second Session we were thrown outside tmux. However, we still have an active session. Ask tmux for a listing of sessions and you will surely find it there:

$ tmux ls
LPI: 2 windows (created Tue Aug 27 19:01:49 2019) [158x39]

However this session is detached from its terminal. We can attach it with tmux attach -t SESSION-NAME (attach can be replaced by at or — simply — a). When there is only a single session, the name specification is optional:

$ tmux a

You are now back in your session; to detach from it, press Ctrl+b-d:

[detached (from session LPI)]
$
Tip

The same session can be attached to more than one terminal. If you want to attach a session making sure it is first detached from any other terminals, use the -d switch: tmux attach -d -t SESSION-NAME.

Important commands for session attaching/detaching:

Ctrl+b-D

select what client to detach.

Ctrl+b-r

refresh the client’s terminal.

To read the whole list of commands, consult the manual page.

Copy & Paste: Scrollback Mode

tmux also features copy mode in basically the same fashion as screen (remember to use tmux’s command prefix and not screen’s!). The only difference command-wise is that you use Ctrl + Space to mark the beginning of the selection and Alt+w to copy the selected text.

Customization of tmux

The configuration files for tmux are typically located at /etc/tmux.conf and ~/.tmux.conf. When started, tmux sources these files if they exist. There is also the possibility of starting tmux with the -f switch to supply an alternate configuration file. You can find a tmux configuration file example located at /usr/share/doc/tmux/example_tmux.conf. The level of customization that you can achieve is really high. Some of the things you can do include:

  • Change the prefix key

    # Change the prefix key to C-a
    set -g prefix C-a
    unbind C-b
    bind C-a send-prefix
  • Set extra key bindings for windows higher than 9

    # Some extra key bindings to select higher numbered windows
    bind F1 selectw -t:10
    bind F2 selectw -t:11
    bind F3 selectw -t:12

For a comprehensive list of all bindings, type Ctrl+b-? (press q to quit) or consult the manual page.

Guided Exercises

  1. Indicate if the following statements/features correspond to GNU Screen, tmux or both:

    Feature/Statement GNU Screen tmux

    Default command prefix is Ctrl+a

    Client-Server Model

    Panes are pseudo-terminals

    Killing a region does not kill its associated window(s)

    Sessions include windows

    Sessions can be detached

  2. Install GNU Screen on your computer (package name: screen) and complete the following tasks:

    • Start the program. What command do you use?

    • Start top:

    • Using screen’s key prefix, open a new window; then, open /etc/screenrc using vi:

    • List the windows at the bottom of the screen:

    • Change the name of the current window to vi:

    • Change the name of the remaining window to top. To do that, first display a list of all windows so that you can move up and down and select the right one:

    • Check that the names have changed by having the window names displayed at the bottom of the screen again:

    • Now, detach the session and have screen create a new one named ssh:

    • Detach also from ssh and have screen display the list of sessions:

    • Now, attach to the first session using its PID:

    • You should be back at the window displaying top. Split the window horizontally and move to the new empty region:

    • Have screen list all windows and select vi to be displayed in the new empty region:

    • Now, split the current region vertically, move into the newly created empty region and associate it with a brand new window:

    • Terminate all regions except the current one (remember, although you kill the regions, the windows are still alive). Then, quit out of all the windows of the current session until the session itself is terminated:

    • Finally, have screen list its sessions one more time, kill the remaining ssh session by PID and check that there are actually no sessions left:

  3. Install tmux on your computer (package name: tmux) and complete the following tasks:

    • Start the program. What command do you use?

    • Start top (note how — in a couple of seconds — the name of the window changes to top in the status bar):

    • Using tmux’s key prefix, open a new window; then, create ~/.tmux.conf using nano:

    • Split the window vertically and reduce the size of the newly created pane a few times:

    • Now change the name of the current window to text editing; then, have tmux display a list with all its sessions:

    • Move to the window running top and back to the current one using the same key combination:

    • Detach from the current session and create a new one whose name is ssh and its window name is ssh window:

    • Detach also from the ssh session and have tmux display the list of sessions again:

      Note

      From this point on the exercise requires that you use a remote machine for ssh connections to your local host (a virtual machine is perfectly valid and can prove really practical). Make sure you have openssh-server installed and running on your local machine and that at least openssh-client is installed on the remote machine.

    • Now, start a remote machine and connect via ssh with your local host. Once the connection has been established, check for tmux sessions:

    • On the remote host, attach to the ssh session by name:

    • Back at your local machine, attach to the ssh session by name making sure the connection to the remote host is terminated first:

    • Have all sessions displayed for selection and go to your first session ([0]). Once there, kill session ssh by name:

    • Finally, detach from the current session and kill it by name:

Explorational Exercises

  1. Both screen and tmux can enter command line mode through _command prefix + : (we already saw a brief example with tmux). Do some research and the following tasks in command line mode:

    • Make screen enter copy mode:

    • Make tmux rename the current window:

    • Make screen close all windows and terminate session:

    • Make tmux split a pane into two:

    • Make tmux kill the current window:

  2. When you enter copy mode in screen not only can you use the arrow keys and PgUP or PgDown to navigate the current window and the scrollback buffer. There is also the possibility of using a vi-like full screen editor. Using this editor, perform the following tasks:

    • Echo supercalifragilisticexpialidocious in your screen terminal:

    • Now, copy the five consecutive characters (left-to-right) in the line right above your cursor:

    • Finally, paste the selection (stice) back at your command prompt:

  3. Suppose you want to share a tmux session (our_session) with another user. You have created the socket (/tmp/our_socket) with the right permissions so that both you and the other user can read and write. What other two conditions should be met in order for the second user to be able to successfully attach the session through tmux -S /tmp/our_socket a -t our_session?

Summary

In this lesson you have learned about terminal multiplexers in general and GNU Screen and tmux in particular. Important concepts to remember include:

  • Command prefix: screen uses Ctrl+a + character; tmux, Ctrl+b + character.

  • Structure of sessions, windows and window divisions (regions or panes).

  • Copy mode.

  • Session detachment: one of the most powerful features of multiplexers.

Commands used in this lesson:

screen

Start a screen session.

tmux

Start a tmux session.

Answers to Guided Exercises

  1. Indicate if the following statements/features correspond to GNU Screen, tmux or both:

    Feature/Statement GNU Screen tmux

    Default command prefix is Ctrl+a

    x

    Client-Server Model

    x

    Panes are pseudo-terminals

    x

    Killing a region does not kill its associated window(s)

    x

    Sessions include windows

    x

    x

    Sessions can be detached

    x

    x

  2. Install GNU Screen on your computer (package name: screen) and complete the following tasks:

    • Start the program. What command do you use?

      screen

    • Start top:

      top

    • Using screen’s key prefix, open a new window; then, open /etc/screenrc using vi:

      Ctrl+a-c

      sudo vi /etc/screenrc

    • List the windows at the bottom of the screen:

      Ctrl+a-w

    • Change the name of the current window to vi:

      Ctrl+a-A. Then we have to type vi and press enter.

    • Change the name of the remaining window to top. To do that, first display a list of all windows so that you can move up and down and select the right one:

      First we type Ctrl+a-". Then we use the arrow keys to mark the one that says 0 bash and press enter. Finally, we type Ctrl+a-A, type in top and press enter.

    • Check that the names have changed by having the window names displayed at the bottom of the screen again:

      Ctrl+a-w

    • Now, detach the session and have screen create a new one named ssh:

      Ctrl+a-d screen -S "ssh" and press enter.

    • Detach also from ssh and have screen display the list of sessions:

      Ctrl+a-d screen -list or screen -ls.

    • Now, attach to the first session using using its PID:

      screen -r PID-OF-SESSION

    • You should be back at the window displaying top. Split the window horizontally and move to the new empty region:

      Ctrl+a-S

      Ctrl+a-Tab

    • Have screen list all windows and select vi to be displayed in the new empty region:

      We use Ctrl+a-" to have all windows displayed for selection, mark vi and press enter.

    • Now, split the current region vertically, move into the newly created empty region and associate it with a brand new window:

      Ctrl+a-|

      Ctrl+a-Tab

      Ctrl+a-c

    • Terminate all regions except the current one (remember, although you kill the regions, the windows are still alive). Then, quit out of all the windows of the current session until the session itself is terminated:

      Ctrl+a-Q. exit (to exit Bash). Shift+:, then we type quit and press enter (to exit vi). After that, we type exit (to exit the underlying Bash shell) q (to terminate top); then we type exit (to exit the underlying Bash shell).

    • Finally, have screen list its sessions one more time, kill the remaining ssh session by PID and check that there are actually no sessions left:

      screen -list or screen -ls

      screen -S PID-OF-SESSION -X quit

      screen -list or screen -ls

  3. Install tmux on your computer (package name: tmux) and complete the following tasks:

    • Start the program. What command do you use?

      tmux

    • Start top (note how — in a couple of seconds — the name of the window changes to top in the status bar):

      top

    • Using tmux’s key prefix, open a new window; then, create ~/.tmux.conf using nano:

      Ctrl+b-c nano ~/.tmux.conf

    • Split the window vertically and reduce the size of the newly created pane a few times:

      Ctrl+b-"

      Ctrl+b-Ctrl+↓

    • Now change the name of the current window to text editing; then, have tmux display a list with all its sessions:

      Ctrl+b-,. Then we supply the new name and press enter. Ctrl+b-s or tmux ls.

    • Move to the window running top and back to the current one using the same key combination:

      Ctrl+b-n or Ctrl+b-p

    • Detach from the current session and create a new one whose name is ssh and its window name is ssh window:

      Ctrl+b-d tmux new -s "ssh" -n "ssh window"

    • Detach also from the ssh session and have tmux display the list of sessions again:

      Ctrl+b-d tmux ls

      Note

      From this point on the exercise requires that you use a remote machine for ssh connections to your local host (a virtual machine is perfectly valid and can prove really practical). Make sure you have openssh-server installed and running on your local machine and that at least openssh-client is installed on the remote machine.

    • Now, start a remote machine and connect via ssh with your local host. Once the connection has been established, check for tmux sessions:

      On remote host: ssh local-username@local-ipaddress. Once connected to the local machine: tmux ls.

    • On the remote host, attach to the ssh session by name:

      tmux a -t ssh (a can be replaced by at or attach).

    • Back at your local machine, attach to the ssh session by name making sure the connection to the remote host is terminated first:

      tmux a -d -t ssh (a can be replaced by at or attach).

    • Have all sessions displayed for selection and go to your first session ([0]). Once there, kill session ssh by name:

      We type Ctrl+b-s, use the arrow keys to mark session 0 and press enter tmux kill-session -t ssh.

    • Finally, detach from the current session and kill it by name:

      Ctrl+b-d tmux kill-session -t 0.

Answers to Explorational Exercises

  1. Both screen and tmux can enter command line mode through command prefix + : (we already saw a brief example with tmux). Do some research and the following tasks in command line mode:

    • Make screen enter copy mode:

      Ctrl+a-: — then, we type copy.

    • Make tmux rename the current window:

      Ctrl+b-: — then, we type rename-window.

    • Make screen close all windows and terminate session:

      Ctrl+a-: — then, we type quit.

    • Make tmux split a pane into two:

      Ctrl+b-: — then, we type split-window.

    • Make tmux kill the current window:

      Ctrl+b-: — then, we type kill-window.

  2. When you enter copy mode in screen not only can you use the arrow keys and PgUp or PgDown to navigate the current window and the scrollback buffer. There is also the possibility of using a vi-like full screen editor. Using this editor, perform the following tasks:

    • Echo supercalifragilisticexpialidocious in your screen terminal:

      echo supercalifragilisticexpialidocious

    • Now, copy the five consecutive characters (left-to-right) in the line right above your cursor:

      We enter copy mode: Ctrl+a-[ or Ctrl+a-: and then type copy. Then, we move to the line above using k and press space to mark beginning of selection. Finally, we move forward four characters using l and press space again to mark the end of the selection.

    • Finally, paste the selection (stice) back at your command prompt:

      Ctrl+a-]

  3. Suppose you want to share a tmux session (our_session) with another user. You have created the socket (/tmp/our_socket) with the right permissions so that both you and the other user can read and write. What other two conditions should be met in order for the second user to be able to successfully attach the session through tmux -S /tmp/our_socket a -t our_session?

    Both users must have a group in common, e.g. multiplexer. Then, we must have the socket changed into that group as well: chgrp multiplexer /tmp/our_socket.

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Next Lesson

103.6 Modify process execution priorities (103.6 Lesson 1)

Read next lesson

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

LPI is a non-profit organization.

Linux Professional Institute (LPI) is the global certification standard and career support organization for open source professionals. With more than 200,000 certification holders, it's the world’s first and largest vendor-neutral Linux and open source certification body. LPI has certified professionals in over 180 countries, delivers exams in multiple languages, and has hundreds of training partners.

Our purpose is to enable economic and creative opportunities for everybody by making open source knowledge and skills certification universally accessible.

  • LinkedIn
  • flogo-RGB-HEX-Blk-58 Facebook
  • Twitter
  • Contact Us
  • Privacy and Cookie Policy

Spot a mistake or want to help improve this page? Please let us know.

© Copyright 1999-2020 The Linux Professional Institute Inc. All rights reserved.