Introduction
In this hands-on lab, you'll discover the power of the screen
command in Linux. This essential tool enables you to create and effectively manage multiple terminal sessions simultaneously. This is incredibly beneficial for handling long-running processes, maintaining remote sessions, or effortlessly switching between various tasks without interrupting your workflow. We'll start with installing the screen
utility, then move on to creating and navigating through different screen sessions. Finally, you’ll learn how to detach from and reattach to these sessions, ensuring your work is preserved even when disconnecting from the terminal. This knowledge is crucial for any systemadmin.
Introduction to the screen Command
In this section, we'll explore the screen
command, a robust utility that facilitates the creation and management of multiple terminal sessions. The screen
command proves invaluable when dealing with tasks like long-running scripts, remote connections, or the need to juggle multiple activities without losing progress.
Let's begin by installing the screen
package on our Ubuntu 22.04 Docker container:
sudo apt-get update
sudo apt-get install -y screen
Now, let's initiate a new screen session:
screen
This command will launch a fresh screen session, displaying a message confirming your entry into the session.
To view a list of all currently active screen sessions, use the following command:
screen -ls
Example output:
There is a screen on:
12345.pts-0.labex (Detached)
1 Socket in /run/screen/S-labex.
To disconnect from the current screen session, press Ctrl+A
followed by d
. This action will leave the session running seamlessly in the background.
To reconnect to a detached screen session, use the following command:
screen -r
This command will re-establish your connection to the specified running screen session.
Creating and Navigating Screen Sessions
This step focuses on the creation and navigation of multiple screen sessions, a key feature of the screen
command that enhances multitasking capabilities for any systemadmin working with Linux.
First, let's create a new screen session with a user-defined name:
screen -S mysession
This will create a new screen session named "mysession".
Next, let's create another screen session:
screen -S othersession
You now have two screen sessions actively running.
To display a list of all existing screen sessions, execute the following command:
screen -ls
Example output:
There are screens on:
12345.mysession (Detached)
67890.othersession (Detached)
2 Sockets in /run/screen/S-labex.
To switch between screen sessions, use these commands:
## Switch to the "mysession" screen session
screen -r mysession
## Switch to the "othersession" screen session
screen -r othersession
While inside a screen session, utilize these keyboard shortcuts for efficient navigation:
Ctrl+A c
: Create a new screen windowCtrl+A n
: Switch to the next screen windowCtrl+A p
: Switch to the previous screen windowCtrl+A "
: List all the screen windows
Experiment with creating and switching between multiple screen sessions to gain practical experience.
Detaching and Reattaching Screen Sessions
In this section, you’ll learn to detach from a screen session and subsequently reattach to it, a vital skill for managing long-running processes on a Linux system, especially when working as a systemadmin or needing persistent processes.
First, let's initiate a new screen session:
screen -S myapp
Now, let's start a long-running process within the screen session, such as launching a simple web server:
python3 -m http.server 8000
To detach from the screen session, press Ctrl+A
followed by d
. The process will continue execution in the background, even after detachment.
You can verify the continued operation of the screen session by using the screen -ls
command:
screen -ls
Example output:
There is a screen on:
12345.myapp (Detached)
1 Socket in /run/screen/S-labex.
To reattach to the screen session, enter the following command:
screen -r myapp
This will reconnect you to the "myapp" screen session, where you should see the web server still actively running.
To terminate the screen session, press Ctrl+C
to halt the web server, followed by Ctrl+D
to exit the session entirely.
Summary
This lab provided a comprehensive overview of the screen
command, a powerful tool for managing multiple terminal sessions effectively. You learned to install the screen
package, create, detach from, and reattach to screen sessions. Furthermore, you explored the creation and navigation of multiple screen sessions, an essential skill for managing long-running processes or switching between tasks seamlessly. Mastering the screen
command is an invaluable asset for any systemadmin working with Linux environments.
You also learned to list active screen sessions and switch between them using the screen -r
command. These techniques contribute to increased efficiency and productivity in a terminal-based workflow. This is particularly helpful when needing to run tasks as root and retain access even after disconnecting from the server.