Have you experienced working on a remote machine, only for your connection to suddenly drop, thus resulting in unsaved work and a fully terminated Secure Shell (SSH)?
Are you about to create a new window, but worry about what might happen with the progress you’ve made on the previous session? Thankfully, ‘screen’ is here to save the day.
By using this command, you will not have to worry anymore about your work being unsaved or lost.
Are you a beginner with regards to working with the Linux screen command? Do you need help when it comes to understanding what a screen command is?
This article is here to guide you along the way and provide you with the basics needed, to use the screen command.
Definition of a GNU Screen
A GNU Screen is a terminal multiplexer of a physical terminal between many processes (usually interactive shells).
Basically, it means you can start a new screen session while opening multiple windows, also known as virtual terminals, inside that particular session.
The different processes running on the screen will continue while your windows are closed or even if the entire screen session is detached from the user’s terminal.
When a screen is created, it produces a new window with a shell/SSH session in it.
This enables you to use the current session, as you normally would.
At any given time, you can always create new screen sessions with different programs (including shells), to turn output logging on and off, etc.
Installation Process
Linux GNU Screen
The screen package is usually pre-installed on most Linux distros. You can see whether it is already installed in your system by typing the following command:
screen --version
However, if your system does not have it pre-installed, the installation process is simple and easy.
First, log in to your server using the ssh session command. Open up your terminal and type:
ssh your-user@server
Depending on the Linux distro being used, here are the needed inputs to install your respective Linux screens.
If you are using Debian, Ubuntu, or Linux Mint, you can input the following command:
sudo apt install screen sudo apt install screen
In the case of CentOS Linux or Fedora Core, this is the screen command to input:
sudo yum install screen
If the user cannot run sudo commands, you have to operate it as a root user:
yum install screen
At the end of your installation, you can check the current version of your screen, using the following screen command line:
screen –version
Step by Step Procedure
Starting Up A Screen Linux Session
To get a screen session started, all you have to do is type ‘screen’ on your device.
screen
This will open up a screen session, create a new window, and start a new shell on that particular window. Now that you have opened up a screen session, you can list the possible command options by typing:
Ctrl+a ?
Starting Named Screen Session
Naming your sessions comes in handy when it comes to running multiple screen sessions.
By doing this, you can give yourself an easier time in terms of keeping track of your progress and the work you have done.
In order to create a named session, operate the screen command with the following arguments:
screen -S session_name
It can often be beneficial to come up with a descriptive and unique session name per command screen.
Working with Linux Screen Windows
When it comes to starting a new screen session, it creates a new window that contains a shell in it.
You are allowed to have more than one screen window inside one screen session.
To create a screen session with shell, type the command Ctrl+a c, the first available number ranging from 0 to 9 will be assigned to it.
Here are some basic commands for handling Linux Screen Sessions:
- Ctrl+a “ – List all window
- Ctrl+a c – Allows you to create a new window (with shell)
- Ctrl+a A – For renaming the current window
- Ctrl+a 0 – Switches to window 0 (by number)
- Ctrl+a | – Splits the current region vertically into two regions
- Ctrl+a S – Splits the current region horizontally into two regions
- Ctrl+a Ctrl+a – If you want to toggle between the current and previous region
- Ctrl+a tab – Switches the input focus to the next region
- Ctrl+a Q – Closes all regions except for the current one
- Ctrl+a X – Lets you close the current region
- Screen -dms – Gives you the option for submitting jobs in background non interactively
- Screen -rd – Allows you to detach the screen from outside of the screen session
Detach from Linux Screen Session
If you want to detach from the current session you are in, type:
Ctrl+a d
The program currently running on the screen will resume once you have finished detaching from the screen session.
By doing this, you are now able to work on other screen sessions, without worrying about losing your work from the previous window.
Reattach from Linux Screen
To resume your screen session, type in the following command:
$ screen -r
In case you have many screen sessions concurrently running on your machine, you will need to attach the screen session ID after inputting the ‘r’ switch. To do this list the current running screen sessions with:
$ screen -ls
You’ll then be presented with the output:
There are screens on: 13987.pts-0.linux-system (detached) 13687.pts-0.linux-system (detached) 2 sockets in /run/screens/S-linux
In order to restore screen 13987.pts-0, type the following command:
screen -r 10837
How to Manage Multiple Sessions with Linux Screen
One of the various features Linux screen has, is its ability to perform tasks and leave it in the background so that when you return to it, they won’t be deleted.
Let’s take htop for example. We can install it on Ubuntu, Mint, Debian, and Linux using the following command:
sudo apt install htop
You can run htop on your system by typing this command on your terminal:
htop
The next step is to detach the execution from the terminal using the commands CTRL+a & CTRL+d.
From here, you can continue working on your different screens, while the previous window is still running in the background. If you want to open the same htop terminal, enter this command:
screen -ls
It will present all your saved sessions. To reopen htop, type:
screen -r process_number
Conclusion
In this tutorial, you were given a brief background on how to operate a GNU screen.
It’s vital to know how to install and utilize this, especially when it comes to technical and administrative work.
Knowing and understanding the basics of handling a Linux screen can give you a useful tool that can be beneficial for your overall workflow.
Hopefully, you were able to learn how to install screen and use its basic commands.