Introduction
Screen or GNU screen is a terminal utility. In simple words, you can start a screen terminal session. It splits a physical terminal into multiple virtual sessions. If you disconnect from the SSH terminal session, the processes running in screen will continue to run.
For Example:
You may have faced a situation where you perform long running tasks in an SSH terminal session, and suddenly your network connection is lost, so the SSH session gets disconnected/terminated, and your work is lost. Well, we all have faced this situation, right? But, if you work on a SSH terminal session in screen, you can be able to connect to the terminal session back from the same screen you worked on before disconnecting, you can resume the terminal session .
You can run a task like rsync or installing some software and disconnect the SSH terminal session and come back later, resume SSH terminal session and check the status of rysnc.
Linux Screen may be basic in its structure, but it has its own powerful syntax and features, every Linux user should know how to use.
In this guide, we’ll discuss how to Install and Use Screen on Ubuntu.
Prerequisites
Before you begin this guide, you’ll need to do the following:
- Root access to the server via SSH
Step 1 – Installing Screen on Ubuntu 20.04
Connect to the server through SSH with root user
Now install the screen by running the following commands. (Screen is often installed by default on Ubuntu, if not, you can install it by following.)
sudo apt update sudo apt install screen
After the installation is complete, run the following command to verify whether screen is installed.
which screen
Step 2 – Using Screen on Ubuntu 20.04
Starting Screen Session
Start a screen session, simply run the following command
screen
Output:
Just press Enter to continue. You can now run tasks in a screen session.
Starting Named Screen Session
You can name the screen session and it is very useful when running multiple screen sessions.
To start a named screen session, run the following command:
screen -S session_name
screen -S taskname2
Detach (exit) from Screen Session
Note: The task running in the screen session will continue to run even after you detach from the session.
You can detachfrom the screen session by pressing keyboard shortcut Ctrl+a
and d
or you can run the exit
command:
exit
Reattach (resume) into a Screen Session
How can we resume back into a screen session? To reattach screen session, run the following command.
screen -r
In case you have multiple screen sessions running, How to resume specific screen session? Reattach to the screen session by typing its ID number after the -r flag.
First, check the ID number of running screen session using following command
screen –ls
Output:
There are screens on: 1234.taskname1 (19/09/2022 14:19:50 AM) (Detached) 1122.taskname2 (19/09/2022 14:19:50 AM) (Attached) 2 Sockets in /run/screen/S-root.
Now we can reattach to session one by typing its ID number after the -r flag.
For example:
screen –r 1234
Conclusion
In this tutorial, You learned how to install and use screen and create multiple screen sessions, exit from screen and resume into screen session.