screen command in Linux provides the ability to launch and use multiple shell sessions from a single ssh session. It also allows you to share your sessions with other users and detach/attach terminal sessions.
In other words, it means that you can start a screen session and then open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run even when their window is not visible and even if you get disconnected.
The screen package is pre-installed on most Linux distributions. You can check if it is installed on your system by using the following command:
Then you should the version of screen package that is currently installed in your system. If not then you can install the screen with the following command.
Install Linux Screen on Debian and Ubuntu
sudo apt install screen
Install Linux Screen on CentOS and Fedora
sudo yum install screen
How to use screen command in Linux
Now let’s get to the part on how to use the screen command in Linux, to start a screen session in Linux, just type screen in your command prompt.
You can also assign a name to screen session, named sessions are useful when you run multiple screen sessions. To create a named session, run the screen command with the following arguments:
screen -S <session_name>
Then the screen will show with an interface exactly the same as usual command prompt. You can also have multiple screen sessions or screen inside screen running at any point in time.
Any point in time you want to see all the screen command options that you want to know about, just type Ctrl+A and ?. Then you will see all commands or parameters on screen.
To create a new screen session, you can use the following command
Following are some of the most common commands for managing the screen sessions
Ctrl+A c – Create a new screen session
Ctrl+A " – List all active screen sessions
Ctrl+A 0 – Switch to screen window 0 (by number )
Ctrl+A A – Rename the current screen session
Ctrl+A S – Split current region horizontally into two regions
Ctrl+A | – Split current region vertically into two regions
Ctrl+A tab – Switch the input focus to the next region
Ctrl+A Ctrl+A – Toggle between the current and previous region
Ctrl+A Q – Close all regions except the current one
Ctrl+A X – Close the current region
How to detach from Linux Screen Session
Let’s say you are in the middle of SSH-on your server doing a server upgrade or downloading a large file which you can’t interrupt but you want to locally run a command or open a new ssh session without interrupting whatever is currently running in the existing session, then we can screen to detach it.
You can use the following command
How to Reattach to a Linux Screen
Let’s see you have done some other stuff after detaching the screen session, now you want to get back to your original screen session which was upgrading a server or downloading the large file and you want to see the progress of it, then you can simply reattach and continue from where you left off.
You can use the following command to resume screen.
In case if you have more than 1 screen session, then you need to type the screen session ID to resume that particular session.
You can use screen -ls to see the list of the currently running screen sessions.
You will see a similar output like below
There are screens on:
2 Sockets in /run/screens/S-pawan.
If you want to restore screen 5380.pts-0, then type this command.
screen -r 5380
In the next post, we see all other things like password protecting a screen session, customising the screen and logging all the events.