In Linux, the screen command permits you to create and use multiple sessions from a single SSH session or terminal. It is mainly used to resume the accidentally terminated session. When you start a session using the screen command, it can be detached and reattached at any time. The “screen” command also manages the detached session and resumes the ongoing operations upon reattaching it. This guide will discuss how to use the screen command in Linux. Each method will be provided to you from the screen session creation to its detachment and reattachment. So, let’s start!
How to install screen command in Linux
Before using the screen command, the first thing you have to do is install the screen in your Linux system. To do so, open up the terminal by pressing “CTRL+ALT+T” and then execute the below-given command:
$ sudo apt install screen
The error-free output declares that the screen is successfully installed on your system. You can confirm this operation by checking the screen version:
$ screen --version
From the output, you can see that “screen version 4.08.00” is installed:
To use the screen command, you must know its syntax first. So, move ahead and check out the screen command syntax provided in the next section.
Syntax of screen command in Linux
Here is the basic syntax of the screen command:
$ screen [Option] [Command <Argument>]
In “Option”, you can add the screen command options according to your requirement. Next, write out the “Command” you want to execute in the current screen session.
Note: A table of screen command options with their description is added at the end of this post.
How to use screen command in Linux
You can utilize the simple “screen” command for starting a screen session in the current terminal session:
Press “Space” to move to the next page or “Escape” to return from this window and back to the created screen session:
How to create a named screen session in Linux
The naming convention is useful when you are operating multiple screen sessions simultaneously. To create a named screen session, add the “-S” option in the screen command and specify the “session name” afterward. It is also preferred to use a descriptive name in the screen session, as it will assist in detaching or reattaching the screen session.
For instance, the following “screen” command will create a screen session named “firstScreen”:
$ screen -S firstScreen
The screen session name will be only saved in the system screen table and will not appear on the opened session:
How to use screen command for listing screen sessions in Linux
The combination of the “-ls” option is used for listing out the opened screen sessions, whether they are running in the background or not. This session list will also comprise the attached and detached screen sessions:
$ screen -ls
From the output, you can check out the other details of the screen sessions such as their screen IDs, screen name, date and time of creation, and their current status, which can be “Attached” or “Detached”:
How to use screen command for detaching a screen session in Linux
To detach a screen session, add the “-d” option in the “screen” command and specify the screen ID or the screen name. You can also utilize the “CTRL-a + d” shortcut for this purpose:
$ screen -d firstScreen
From the below-given output, you can see that our “firstScreen” session is detached successfully:
To verify the previously performed action, you can list out the screen session and check out the state of the specified session from there:
How to use screen command for reattaching a screen session in Linux
Want to reattach a detached screen session? The “screen” command permits you to perform this operation with the help of the “-r” option. For instance, in the below-given command, we will reattach the “firstScreen” session by adding its screen ID “4622” after the “-r” option:
$ screen -r 4622
You can also use the “CTRl-a + r” shortcut for reattaching a screen session:
How to view screen command manual in Linux
In Linux-based systems, the “man” command is used to print out any command’s manual in the Linux terminal. For instance, to view the screen command manual, we will type “man screen” in the terminal:
$ man screen
Other options of screen Command in Linux
Here is the list of some of the other options of the screen command:
|The “-c” option is utilized in the “screen” command for reading the configuration file instead of “.screenrc”.
|The “-f” option is utilized in the “screen” command for turning on the flow control.
|The “-i” option is utilized in the “screen” command for interrupting the output when the flow control is turned on.
|The “-l” option is utilized in the “screen” command for turning on the login mode.
|The “-L” option is utilized in the “screen” command for turning in the output logging.
|The “-p” option is utilized in the “screen” command for pre-selecting the specified session if it exists.
|The “-t” option is utilized in the “screen” command for adding a title for the screen session.
|The “-v” option is utilized in the “screen” command for printing out the screen version.
|The “-X” option is utilized in the “screen” command for executing a screen command in a particular session.
Other shortcuts of the screen command in Linux
Check out the below-given table to know more about the screen command shortcuts:
|CTRL-a + c
|The “CTRL-a + c” shortcut key is utilized for creating a new screen session.
|CTRL-a + n
|The “CTRL-a + n” shortcut key is utilized for switching to the next screen session.
|CTRL-a + w
|The “CTRL-a + w” shortcut key is utilized to print out the currently opened screen sessions list.
|CTRL-a + p
|The “CTRL-a + p” shortcut key is utilized for switching to the previous screen session.
|CTRL-a + k
|The “CTRL-a + k” shortcut key is utilized for killing or terminating all screen sessions.
|CTRL-a + l
|The “CTRL-a + l” shortcut key is utilized for splitting the screen session vertically.
|CTRL-a + S
|The “CTRL-a + S” shortcut key is utilized for splitting the screen session horizontally.
|CTRL-a + [
|The “CTRL-a + [“ shortcut key is utilized for starting the copy mode.
|CTRL-a + ]
|The “CTRL-a + ]” shortcut key is utilized for pasting the copied text.
Screen is a terminal multiplexer that is used for creating multiple screen sessions. In Linux, using the “screen” command will let you create a screen session and open as many virtual terminals as you like within that session. Even if you get disconnected or the screen session is not visible, the jobs or processes running on the screen will continue to perform the respective operations. In this write-up, we have talked about screen command, its syntax, and how to use the screen command in Linux. Moreover, the screen command options and its shortcuts are also provided.