How to use the display terminal multiplexer in Linux

Contents

How to use the GNU Screen utility on Linux

Want to run multiple virtual screen sessions from a single terminal console? Or avoid internet connection drops when logging out of the console via SSH? If so, welcome the GNU Screen terminal multiplexer to your toolkit today.

Using the GNU display utility

The GNU Screen utility for Linux is a versatile screen multiplexer that enables you to start multiple virtual screen sessions (think of them as additional console windows) from your current terminal session.

Getting started is easy. You can install the GNU Screen tool using sudo apt install screen in the command line of your terminal:

Installing the GNU screen

If you are using a RedHat based operating system / RPM, replace `apt` with` yum` in the above command.

To open a new virtual console, now you can just type screen and hit enter, which will present you with a welcome screen:

GNU screen welcome screen

Just press the space bar or go back to close the welcome screen. The command prompt will appear to return, but actually, you are now inside a virtual screen session. Think of it as an alternate terminal session, running inside the above terminal session.

One of the best things about the GNU screen is that even if the main terminal session / parent will fail or crash in some way (as an example, due to a lost network connection), and even when the main terminal session closes or ends / dad, this The screen session will remain active and running!

It is easy to check if we are currently in a screen session or not:

Checking if this is a GNU screen session or not

This if statement tests whether the $STY the variable is set or not. If he $STY environment variable is set, so this is a screen session.

Then, How do we get back to our main session / main terminal? This can be done using a keyboard combination that the GNU screen will hear in the background. Just press CTRL + A> CTRL + D. It doesn't matter whether or not you release the CTRL key while doing it.

You will see something equivalent to this output:

Exit a GNU screen session

This also tells us that we have only disconnected from the GNU virtual screen session; it's not over and, if we want, we can connect to her again. Even if for some reason the main session / father would have closed, the virtual screen session would not have ended and we can reconnect to it.

To reconnect to the session, we can issue a simple command:

Reconnect to a GNU screen session

Confirming this command will simply return us to the virtual screen session that we had running. Notice that we use the relevant procedure id (as reported when we parted ways with the screen session) to reconnect to the virtual screen session. This further clarifies immediately how we can initiate an indefinite number of screen sessions (even though each will consume a certain amount of resources) and connect to the one we want using the screen -d -r command.

We can read more about the alternatives we use for this command in the online manual of the screen:

Screen r and d manual clarification options

For a more extensive overview of the display and its many functionalities, you can use the man screen command:

GNU Screen Linux manual

If we want to see a list of screen sessions that are running on the machine at the moment, we can issue a screen -list command:

GNU Screens List

Note how the session is reported as' Separated’ o 'Attached’ depending on whether you are still connected to her somewhere or not.

Sometimes, you may want to scroll back within a screen session, as an example, when more output lines were generated than the number of lines that is enabled in your terminal window size. One would expect this to be as easy as using the mouse to scroll up and down, but is not.

Instead, you can use a CTRL keyboard combination + ESC to enable copy mode (even though a real copy is not made if you just scroll), and now your mouse, as well as the standard cursor up keys / below will work properly. When i'm done looking (and / or copy if you want, as an example, with mouse selecting and copying as usual), just press ESC key again to cancel copy mode.

Sometimes, a program running inside a screen may crash, as an example, due to program error or out of memory or disk situation. If this happens, the screen will show as' Dead’ at screen -list production:

A failed session of the GNU screen

At such a point, we can remove the dead screen session by issuing a screen -wipe command:

A deleted GNU screen session

In this post, we explore how to install the GNU display utility, how to start a new virtual screen session using it and how to disconnect and rejoin an existing screen session. In conclusion, we will see how to navigate within screen sessions, as well as how to handle dead screen sessions.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.