10 Examples of Screen Commands for Managing Linux Terminals

s2 0

the screen is a full-screen software program that can be used to multiplex a physical console between multiple processes (usually interactive shells). It offers a user the option of opening several separate terminal instances in a single terminal window manager.

[ You might also like: How to Use ‘Tmux Terminal’ to Access Multiple Terminals Inside a Single Console ]

the screen Application is very useful when you are working with multiple programs from a command line interface and for separating programs from the terminal shell. You can also share your sessions with other users and detach / attach terminal sessions.

Examples of screen commands

On my Ubuntu Server Edition, screen is installed by default. But in Linux Mint didn’t have a screen installed by default, I need to install it with that first apt-get command before you use it.

Please follow the installation procedure of your distribution to install the screen.

$ sudo apt-get install screen       [On Debian, Ubuntu and Mint]
$ sudo yum install screen           [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a sys-apps/screen    [On Gentoo Linux]
$ sudo pacman -S screen            [On Arch Linux]
$ sudo zypper install screen       [On OpenSUSE]    

Actually it is screen is a very good terminal multiplexer program in Linux that is hidden in hundreds of Linux commands.

Let’s start with the following examples to see how to use the screen command on Linux.

Home screen for the first time

Just give that screen at the command prompt. Then it is screen displays an interface just like the command prompt.

[email protected] ~ $ screen
Screen terminal program
Screen terminal program

Show screen parameters

When you enter the screen, you can do all of your work as you would in the normal command line environment. But since then screen is an application, so it has commands or parameters.

Type “Ctrl-A” and “?“Without quotes. Then you will see any commands or parameters on the screen.

Key assignment of the screen terminal
Key assignment of the screen terminal

To exit the help screen, you can use the “Space bar”Button or“Enter“. (Please note that all links that “Ctrl-A“Takes place without quotation marks).

Disconnect the terminal session with the screen

One of the advantages of a screen that is, you can solve it. Then you can restore it without losing everything you did on the screen. Here is the example scenario:

You are right in the middle of it SSH on Your server. Suppose you load a. down 400 MB Patch your system with the wget command.

[ You might also like: How to Secure and Harden OpenSSH Server ]

The download process will likely take time 2 hours long. When you disconnect SSH If you accidentally lose your session or suddenly the connection is lost, the download process will stop. You have to start all over again. To avoid this we can use a screen and remove it.

Take a look at this command. First you need to enter the screen.

[email protected] ~ $ screen

Then you can go through the download process. For example, I update mine on my Linux Mint dpkg Package with the apt-get Command.

[email protected] ~ $ sudo apt-get install dpkg
Sample output
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following packages will be upgraded:
1 upgraded, 0 newly installed, 0 to remove and 1146 not upgraded.
Need to get 2,583 kB of archives.
After this operation, 127 kB of additional disk space will be used.
Get:1 https://debian.linuxmint.com/latest/ 
testing/main dpkg i386 1.16.10 [2,583 kB]
47% [1 dpkg 1,625 kB/2,583 kB 47%]     14,7 kB/s

During the download you can “Ctrl-A” and “D.“. You won’t see anything when you press these buttons. The output will look like this:

[detached from 5561.pts-0.mint]
[email protected] ~ $

Reconnect the terminal session to the screen

Let’s say you disconnect the screen after disconnecting the screen SSH Meeting and going home. In your home you start to SSH back to your server and you want to see the progress of your download process. To do this, you need to restore the screen. You can run this command:

[email protected] ~ $ screen -r

And you will see that the process you left is still running.

If you have more than 1 screen Session, you need to enter the screen session I WOULD. Use the screen -ls to see how many screens are available.

[email protected] ~ $ screen -ls
Sample output
[email protected] ~ $ screen -ls
There are screens on:
        7849.pts-0.mint (10/06/2021 01:50:45 PM)        (Detached)
        5561.pts-0.mint (10/06/2021 11:12:05 AM)        (Detached)
2 Sockets in /var/run/screen/S-pungki

When you want to restore the screen 7849.pts-0.mint and enter this command.

[email protected] ~ $ screen -r 7849

Using terminal windows with multiple screens

If you need more than 1 screen to do your job is that possible? Yes it is. You can run multiple screen windows at the same time. There are 2 (two) ways to do this.

First, you can detach the first screen and run another screen on the real terminal. Second, create a nested screen.

Switching between screen terminal windows

If you are creating a nested screen, you can use the “Ctrl-A” and “n“. It moves to the next screen. If you want to return to the previous screen, just press “Ctrl-A” and “P.“.

To create a new screen window, simply press “Ctrl-A” and “C.“.

Enable screen logging on Linux

Sometimes it is important record what you did while in the console. Let’s say you are a Linux administrator who manages many Linux servers.

With this screen log, you don’t have to write down every single command you ran. To activate the screen logging function, simply press “Ctrl-A” and “H“. (Please be careful, we are using capital ‘H‘Letters. Use of non-capital ‘H‘, just takes a screenshot of the screen in another file called Hardcopy).

A notification will appear at the bottom left of the screen asking you to: Create a log file “screenlog.0“. You will find screenlog.0 File in your home directory.

This function appends everything you do while you are in the screen window. To close the screen and log current activity, press “Ctrl-A” and “H” again.

Another way to activate the logging function is to use the parameter “-L“When the screen is run for the first time. The command will look like this.

[email protected] ~ $ screen -L

Lock Linux Terminal Screen

The screen also has a shortcut to it lock the screen. You can press the “Ctrl-A” and “x“To lock the screen. This is useful when you want to quickly lock your screen. Here is a sample output of the lock screen after you hit the shortcut.

Screen used by Pungki Arianto  on mint.

You can use your Linux password to unlock it.

Add password to lock screen

For security reasons, you may want the password to your screen session. A password is always asked if you want reattach the screen. This password is different from that Lock screen Mechanism above.

To make your screen password protected, you can use the “$ HOME / .screenrc“File. If the file doesn’t exist, you can create it manually. The syntax will be as follows.

password crypt_password

Create “crypt_password“Above you can choose the option”mkpasswd”Command on Linux. Here is the command with the password “pungki123“.

[email protected] ~ $ mkpasswd pungki123

mkpasswd generates a hash password as shown above. As soon as you have received the hash password, you can enter it into your “.screenrc”File and save it. So the “.screenrc”File will look like this.

password l2BIBzvIeQNOs

The next time you run the screen and disconnect it, a password will be asked if you try reattach it as shown below:

[email protected] ~ $ screen -r 5741
Screen password:

Enter your password, the “pungki123“And the screen will reattach again.

After you have implemented this screen password and clicked on “Ctrl-A” and “x“Then the output will be like this.

Screen used by Pungki Arianto on mint.
Screen password:

You will be asked for a password twice. The first password is yours Linux password, and the second password is the password you put in your. entered .screenrc File.

Exit the screen terminal session

There is 2 (two) ways to exit the screen. First of all we use “Ctrl-A” and “D.“To remove the screen. Second, we can use the Exit command to exit the screen. You can also “Ctrl-A” and “K“To exit the screen.

That’s a lot screen Uses in everyday life. There are still many features in the Screen command. You can see that On-screen man page for more details.