Sometimes we need to use multiple terminal sessions within one window while performing several tasks. Fortunately, In the Linux system, we have a console application “screen” which allows us to use multiple terminal sessions in a window. Using “screen” applications, it is possible to run any number of interactive command shells, console-based applications, etc. It is very useful to keep running the program even if you accidentally close the terminal.
For instance, you are working in a remote Linux machine using an SSH session, but many times you get the SSH session terminated and you lose the task you are working on. In this case, the screen utility tool helps to resume the session. In this article, we will learn about the common usage of screens in Ubuntu 20.04.
Installation of the screen application is a simple and straightforward process. Run the following command to install screen in Ubuntu 20.04.
$ sudo apt-get update
$ sudo apt-get install screen
To verify the installation of the screen, run the following command.
$ screen --version
Start screen session
By using a simple screen command, the screen session can be started. The command will create a new terminal inside the current running terminal and if you do not want to use it you can use the exit command to come out of the screen.
A new window can be started within the screen with the name as follows. In this example, I have used a screen name example-screen which can be reattached at a later stage.
$ screen -S example-screen
List screen processes
We have started a new window using the screen command. Now to display all the current opened screens run the command:
$ screen -ls
Detach the current screen
You can use ctrl+a followed by d command to detach your screen session from the current terminal. Type control+a d in the terminal session as :
Reattach the screen session
You can reattach your detached screen session using screen command with -r option. From the list of the screen session, you can simply type screen -r and screen name to reattach the session again. In this example, I have reattached an 4351.example-screen session from my screen list. You can select your session accordingly.
$ screen -r 4351.example-screen
You can also attach the screen session using ID of the screen session or using name only as:
$ screen -r 4351
$ screen -r example-screen
List screen parameters
You can list all the screen parameters using ctrl+a followed by character ? .
Split the screen vertically
The terminal window can be split either vertically or horizontally as per your convenience. To split the window vertically, press the ctrl + a followed by character | as in the screenshot. The same process can be repeated for n number of vertical screens.
To navigate to another screen type ctrl+a followed by Tab
Split the screen horizontally
To split the screen horizontally press ctrl + a followed by S (upper case). You can repeat the same process to get n number of horizontal screens.
Unsplit the screen
Splitted screen either vertically or horizontally can be unsplited by pressing ctrl+a followed by Q (uppercase q)
Create a new terminal in split session
Just after splitting the screen horizontally or vertically, it does not create any terminal automatically.Move to the new terminal session using key ctrl+a followed by Tab and press ctrl+a followed by c (lowercase c) to create a new terminal session.
Terminal screen session
To terminate the current screen session, press ctrl+a followed by k (lowercase k) as in the screenshot below.
Check all the options available with screen command
To check all the options available with screen command, run the following command in your terminal
$ screen --help
Check the owner of the screen
To check the owner of the opened screen session, you can list the content of the directory /var/run/screen using ls -lthr /var/run/screen command as:
Check the man page of Screen command
To check the man page of screen command, run man screen command as:
$ man screen
In this article, we have learned how to use screen commands in ubuntu 20.04. If you have more screen command tips, do not forget to share them in the comment below.