The screen utility is quite useful when working with numerous programs from a command-line interface or terminal shell. It also offers you the facility to detach and attach terminal sessions and share your sessions with other users.
In this post, you will learn how I use the Screen command in the CentOS system. So let’s start!
How to install Screen in CentOS
Screen utility is not included in the CentOS 8 distribution. So, if you are using CentOS 8 and want to install “Screen“, then firstly, you have to enable the “EPEL repository” on your system:
Now, write out the below-given command for installing Screen on your CentOS system:
How to use Screen in CentOS
If you want to utilize the Screen command, then type “screen” in your CentOS terminal:
Now, you will see the following command-line interface of a Screen session:
To check out the parameters or commands of the Screen utility, Press “CTRL+A” and then “?”. This action will provide you the following information about the Screen:
You can press “Space” to view the next page or “Esc” to return from this window:
How to detach a Screen session in CentOS
We are currently working on the “screen 0” Screen session. To detach the “screen 0” session, we will press “CTRL+A” and then “d”:
How to reattach a Screen session in CentOS
Using your CentOS terminal, if you want to reattach any Screen session, then execute the command given below:
From the output, you can see that multiple screen sessions are running on our CentOS system:
How to find Screen session ID in CentOS
If multiple Screen sessions are operating on your CentOS system, you have to specify the session ID in the session reattach command. In the screen command, add the “-ls” combination option to list out the Screen session with their specific session IDs:
From the retrieved output, note down the ID of the required Screen session:
In the “screen” command, adding the “-r” option forces the Screen command to reattach a Screen session. Also, you have to specify session ID in this command:
Now, the screen session “screen 0” is reattached:
How to switch between Screen sessions in CentOS
We have mentioned earlier that more than one Screen session may be operating simultaneously on your CentOS system. Want to switch between those Screen sessions? Press “CTRL+a” and “n” to move to the next Screen and “CTRL+a” and “p” to switch back to the previous Screen.
For instance, currently, we are using the “screen 1” session. To move back to the “screen 0”, we will press “CTRL+p” and “n”:
You can see that the previous Screen is “screen 0”, in which we switched back in:
Again, by pressing “CTRL+a” and “n”, we will move to the next screen session:
How to enable screen logging in CentOS
Screen command also allows you to record every command you have executed in your Screen session. It creates a separate log file for this purpose. To utilize this feature of the Screen command, activate the screen logging by executing the below-given command in your current Screen session:
Here, the “-L” option is added to activate the Screen logging function:
How to lock a Screen session in CentOS
If you want to lock your Screen session, then press “CTRL+a” and “x”:
Now, your current Screen session “screen 2” is locked after pressing the keys mentioned above. To unlock it, enter your CentOS system password:
How to add a password to Screen session in CentOS
For certain security reasons, you may want to apply a password to your Screen session. This operation will ask the user to input the specified password whenever he/she wants to reattach that session.
Firstly, to utilize the functionality of adding a password to a Screen session, you have to generate a hash password. We will execute the below-given “mkpasswd” command for generating a hash password for the “linuxhint”:
From the terminal output, note down the hash password:
Now, open up the “.screenrc” file in your “nano” editor and add the hash password to it:
This “.screenrc” file is the configuration file of the GNU Screen:
Add the hash password, and press “CTRL+O” to save the “.screenrc” file content:
Now, reattach the Screen session having the session ID “4154”:
After switching back to the specified Screen session, press “CTRL+a” and “x” to lock this session:
Enter the hash password you have added in the “.screenrc” file to unlock this Screen session:
How to leave a Screen session in CentOS
There exist two ways through which you can leave your current Screen session. Either press “CTRL+a” and “d” to detach the session or “CTRL+a” and “k” to terminate the currently running Screen session:
Conclusion
The CentOS Screen command permits its users to utilize a single terminal window manager to open multiple terminal instances. In addition to a conventional command line, Screen also comprises extended features and capabilities. In this post, we have shared how to use the Screen command in the CentOS system. We have also shown you how to switch between multiple Screen sessions. Moreover, the procedure of detaching, reattaching, locking, adding a password to a session, and terminating a Screen session is also provided in this post.