In essence, the screen command allows us to open multiple shell windows within a single terminal command window. It keeps the shell active on the server even if the connection from the workstation to the server is interrupted or disconnected, enabling us to reconnect to the shell window from various locations. It also allows running background shells with a long-running program. If you're genuinely interested in the purpose of this article, please follow along to learn how to use the Screen command in Linux in the following article.
Using the Screen command in Linux
Installation of Screen.
Firstly, you should check if screen is installed on the system. For most Linux operating systems like Redhat or CentOS, screen is typically installed in the directory: /usr/bin/screen. Alternatively, use the following command to search for the screen command:
[flex@office ~]$ which screen
/usr/bin/screen
If the screen command is not available on your system, you can use yum (CentOS) or apt-get (Ubuntu) to install it:
[flex@office ~]$ yum install screen
...
Installation Completed!
Using Screen
After successfully installing screen, you can initiate a screen session within a terminal as follows:
[flex@office ~]$ screen
If you wish to assign a name to the screen session for easier management, you can use the following command:
[flex@office ~]$ screen -S name
Now you'll have a new window inside the terminal. The interface of these two windows looks similar, the difference is that screen allows you to use certain commands through key combinations.
Command: 'Ctrl-a'
To use the command keys in screen, first, press the Ctrl+A key combination. For example, Ctrl+A then you press ?. Screen will display the commands you can use with screen.
Screen key bindings, first page out of 4.
…
Key Command: ^A Typed ^A: a
…
Interrupt: ^B or b Only Q
Clear: C Other ^A
Colon: : Power Break B
Copy: ^[ [ Power Detach D
Detach: ^D d Previous ^P p ^?
Digraph: ^V Read buffer
Display: * Redisplay ^L l
Fit: F Remove buffer =
Flow: ^F f Reset Z
Focus: ^I Screen ^C c
Hardcopy: h Select '
Help: ? Silence _
You can customize the default key bindings of screen by using the .screenrc file according to your preferences. However, according to Mytour, the above key bindings are quite convenient to use.
Exiting screen:
Command: Ctrl+A D
To exit screen, you use the following key combination: Ctrl+A D. This is a major advantage of screen, allowing you to exit and access it from anywhere while commands within screen continue to run on the server. In case of sudden disconnection, screen automatically exits to ensure your programs continue to run safely on the server.
List screens:
After exiting screen, to find out how many screen windows are running. From the terminal window, use the following command:
[flex@office ~]$ screen -ls
Currently active screen:
31917.name1 (Disconnected)
31844.name2 (Disconnected)
2 Sockets in /var/run/screen/S-flex
Access screen:
To access screen again, in the simple scenario where you have only one screen window, you can use the following command:
[flex@office ~]$ screen -r
In case you have multiple command windows, you can use the following command:
[flex@office ~]$ screen -x name1
Terminating screen:
There are two ways to terminate a screen session. If you are inside a screen session, you can use the key combination Ctrl+A K. While if you are outside of a screen session, in the terminal window, you can use the command:
[flex@office ~]$ screen -S name1 -X quit
Here are the basic commands and procedures for using screen in Linux that Mytour wants to share with you to apply them in managing workstations connected to computers running the Linux operating system.