Screen command basics and examples in Linux

Screen is a very useful command that offers the ability to use multiple shell windows (sessions) from a single SSH session on Linux. When the session is disconnected or there is a network outage, the process that starts in the screen session will still work, and you can reconnect to the screen session at any time. This is also very handy if you want to run a long process all the time or connect to the session shell from multiple locations.

In this article, we will show you the basics of installing and using a screen on a Linux VPS.

How to install the screen

The screen comes preinstalled on some of the popular distributions. You can check whether it is installed on the server or not with the following command

screen -v
Screen version 4.00.03 (FAU)

If you do not have a VPS screen, you can easily install it using the package manager provided with the operating system.

CentOS / RedHat / Fedora

yum -y install screen

Ubuntu / Debian

apt-get -y install screen

How to start a screen session

You can start the screen by typing the word “screen” in the command line and a new screen session will be launched, which looks the same as on the command line

screen

It is good practice to start screen sessions with descriptive names so that you can easily remember what process is running in the session. To create a new session with a session name run the following command

screen -S name

and replace ‘name’ with a meaningful name for your session.

Detach screen from session

In order to disconnect from the current screen session, you can press the ‘Ctrl-A‘and’ D‘ on keyboard. All screen sessions will remain active and you can reconnect to them at any time later.

Re-enter the session screen

If you are disconnected from your session or your connection is interrupted for some reason, you can easily reconnect by running the following command:

screen -r

If you have multiple screen sessions you can list them with ‘ls

screen -ls

There are screens on:
7880.session    (Detached)
7934.session2   (Detached)
7907.session1   (Detached)
3 Sockets in /var/run/screen/S-root.

In our example, we have three active screen sessions. So, if you want to restore the “session2” session, you can do

screen -r 7934

or you can use the screen name

screen -r -S session2

End screen session

There are several ways to end your screen session. You can do this by pressing ‘Ctrl‘+’ D‘on the keyboard or using the command line command’exit‘.

You can check the man page screen to see all the useful properties of the command screen.

man screen

NAME
screen - screen manager with VT100/ANSI terminal emulation

SYNOPSIS
screen [ -options ] [ cmd [ args ] ]
screen -r [[pid.]tty[.host]]
screen -r sessionowner/[[pid.]tty[.host]]

Sidebar