Image

Knowledge base → Using the screen virtual console in Linux

[Virtual servers]
Date of publication: 15.03.2024

Using an analogy with servers, a physical server can host several virtual ones. It’s the same with the console; you can run several virtual consoles within one ssh session. In each of them you can run your own commands and switch between each, observing the current state.

Why is this needed and what are the advantages:

  • If you close the regular console and run a command at the same time, it will be completed, which is bad, especially when the command takes a long time to complete. When you close the terminal with the virtual console running, the running command will continue to work and you can always reconnect.
  • Ability to launch multiple virtual consoles and switch between them using a unique ID.
  • When performing updates and losing connection with the server, the command will not complete correctly, which may cause disruption to services. In the virtual console, if the connection is lost, everything will continue to run and you can reconnect at any time.

We will use the screen application and the Linux Debian 12 operating system as a virtual console.

1. Installation

apt update
apt install screen

2. Usage

2.1 Run screen to get to the virtual console

screen

Now you are in a virtual terminal, you can execute all the same commands in it as in a regular one.

2.2 Disconnection

To exit the virtual console and still keep it active, use the keyboard shortcut:

Ctrl+a+d
[detached from 150087.pts-0.srv]

ID for reconnection - 150087

2.3 Closing the virtual console

exit

3. Basic commands

3.1 Disconnection

In addition to hotkeys, you can use the command:

screen -d
[detached from 150087.pts-0.srv]

3.2 List of running consoles

screen -ls

3.3 Reconnecting to the console

Use a number id to connect to a specific console

screen -r 150087

Using the virtual console is especially useful for commands that take a long time to complete. Now you will be sure that your command will complete correctly and will know its final output even if you lose connection with the main console.





No Comments Yet