Chat Zalo Chat Messenger Phone Number Đăng nhập
20 Screen Command Examples to Manage Linux Terminals - Tecmint

20 Screen Command Examples to Manage Linux Terminals – Tecmint

Brief: In this guide, we will discuss some useful examples of screen command. By the end of this guide, users will be able to work with multiple shell sessions using a single Linux terminal window.

As Linux users, we often need to work on long-running tasks such as downloading or copying large files, running time-consuming database queries, etc. Sometimes, these important tasks are terminated abruptly due to a session timeout.

To mitigate such scenarios, we can use the screen command, which is a full-screen software program that can be used to multiplex a physical console between several processes (usually interactive shells). It offers the user to open several separate terminal instances within a single terminal window manager.

The display application is very useful if you are dealing with multiple programs from a command line interface and to separate programs from the terminal shell. It also allows you to share your sessions with other users and separate/attach terminal sessions.

In this guide, we will discuss how to install, configure, and use the screen command on a Linux system. Throughout this guide, we will understand the use and benefits of the screen command using practical examples.

So let’s get started.

Install Screen Command on Linux

On my Ubuntu Server Edition, the screen command has been installed by default. But, Linux Mint doesn’t have a screen installed by default, so I need to install it first using the apt-get command before using it.

Follow the distribution installation procedure to install the screen.

$ sudo apt-get install screen [In Debian, Ubuntu and Mint] $ sudo yum install screen [In RHEL / CentOS / Fedora and Rocky Linux / AlmaLinux] $ sudo emerge -a sys-apps/screen [In Gentoo Linux] $ sudo apk add screen [In Alpine Linux] $ sudo pacman -S screen [In Arch Linux] $ sudo zypper install screen [In OpenSUSE]

Now, let’s verify that the command was installed correctly:

$ screen -version Screen version 4.8.00 (GNU) 05-Feb-20

Actually, the screen is a very good terminal multiplexer program in Linux that is hidden inside hundreds of Linux commands

.

Let’s start looking at the use of the screen command on Linux with the following examples.

Screen command syntax The screen

command syntax is as follows $ screen

[OPTIONS] [ CMD [ ARGS ] ]

In the syntax above, we can see that all parameters are options and are represented by square brackets ([]).

1. Start screen for the first time

Just type the screen at the command prompt. Then the screen will display an interface exactly like the command prompt.

$ screen

Screen Terminal Program
Screen Terminal Program

2. Show display parameter

When you enter the screen, you can do all your work as if you were in the normal command-line environment. But since the screen is an app, it also has commands or parameters.

Type “Ctrl-A” and “?” without quotation marks. Then you will see all the commands or parameters on the screen.

Screen terminal key links

To exit the help screen, you can press the “spacebar” or “Enter” button. (Note that all shortcuts that use “Ctrl-A” are made without quotation marks.)

3. How to list all open windows

In the example above, we start a new shell session using the screen command. Now let’s use the key combination ctrl-a + ” to list all

the open windows of the current screen session:

List of open screen windows
List screen Open windows

In the above output, we can see the number and name of the window. The first column represents the window number and starts from scratch.

The following column represents the name of the window and its default format is <user>@<hostname>:<working-directory>.

4. How

to end the screen window session

We can end the active window session using the key combinations ctrl-a + k. It is important to note that this action will interrupt the execution of the command from the current virtual window.

<img src="https://www.tecmint.com/wp-content/uploads/2021/08/List-Screen-Open-Windows.png" alt="End Screen Window Session

” />
End Screen Window Session

In the above output, we can see that the screen command is waiting for user confirmation. Displays the warning message in the lower-left corner.

Like other Linux commands, we can use ‘y’ to continue or ‘n’ to abort the operation.

5. How to start

a named screen session The screen command allows us to create a session with a user-defined name

, which is useful when multiple sessions are available

. So, let’s use the -S option of the command to create a named session: $ screen -S demo-screen In the

above command, the demo-screen is the name of the session

.

6. Separate Terminal Session with Screen

One of the advantages of a screen is that you can peel it off. Then, you can restore it without losing anything you have done on the screen. Here’s the example scenario:

You’re in the middle of SSH on your server. Suppose you are downloading a 400 MB patch for your system using the wget command.

The download process is estimated to take 2 hours. If you disconnect the SSH session, or suddenly the connection is lost by accident, the download process will stop. You have to start from the beginning all over again. To avoid this, we can use a screen and take it off.

Check out this command. First, you have to enter the screen.

$ screen

Then you can do the download process. For example, on my Linux Mint, I’m updating my dpkg package using the apt command.

$ sudo apt install dpkg Reading package lists… Done Creation of dependency tree Reading status information… Done The following packages will be updated: dpkg 1 updated, 0 newly installed, 0 to remove, and 1146 not updated. You need to get 2,583 KB of files. After this operation, 127 KB of additional disk space will be used. Get:1 http://debian.linuxmint.com/latest/ tests/dpkg main i386 1.16.10 [2,583 kB] 47% [1 dpkg 1,625 kB/2,583 kB 47%] 14.7 kB/s

While downloading, you can press “Ctrl-A” and “d”. You won’t see anything when you press those buttons. The output will be like this:

[separated from 5561.pts-0.mint] [email protected] ~$

7. Reconnect

the display terminal session After disconnecting

the display, suppose you are disconnecting your SSH session and go home. At home, you start SSH again on your server and want to see the progress of your download process. To do that, you need to restore the screen. You can run this command:

[email protected] ~$screen -r

And you will see that the process you left is still running

.

When you have more than 1 screen session, you must enter the screen session ID. Use screen -ls to see how many screens are available.

[email protected] ~ $ screen -ls [email protected] ~ $ screen -ls There are screens at: 7849.pts-0.mint (10/06/2021 01:50:45 PM) (Separate) 5561.pts-0.mint (10/06/2021 11:12:05 AM) (Separate) 2 Sockets in /var/run/screen/S-pungki

If you want to restore the 7849.pts-0.mint screen, type this command.

[email protected] ~$ screen -r 7849

8. Using Multi-Screen Terminal Windows

When you need more than 1 screen to do your job, is it possible? Yes. You can run multiple screen windows at the same time. There are 2 (two) ways to do this.

First, you can detach the first screen and run another screen on the actual terminal. Secondly, make a nested screen as shown.

To create new windows

, use the ctrl-a + c key combination twice to create two new windows. Next, let’s use the key combination ctrl-a + ” to list all active windows:

List Screen Active Windows
List Screen Active Windows

In the above output, we can see that there are now three windows.

9. How to rename a screen window

In the example above, we created two new windows. However, their default names do not provide any details about them. In such cases, we can assign them some meaningful names.

We can use the key combination ctrl-a + A to rename the session of the current window. This key combination prompts the user to set a window title:

Rename Screen Window Session
Rename Screen Window Session

10. Switching between screen terminal

windows

In the previous example, we created multiple windows. Now let’s see how to switch between them.

First, let’s list all active windows using the ctrl-a+ key combination.” Then use the up or down arrow key to select the appropriate window. Finally, press the Enter key to switch to the selected window.

Switch between screen windows
Switch between screen windows

In this example, we have selected the window with the title – db-query

. 11. How to move to the

next screen window

We can use the key combination ctrl-a + n to move to the following windows. Let’s understand this with an example.

First, switch to the first window and check it using the key combination ctrl-a + “:

<img src="https://www.tecmint.com/wp-content/uploads/2021/08/Switch-Between-Screen-Windows.png" alt

=”Switch to screen windows” />Switch to screen windows Now, let’s press the key combination ctrl-a +

n twice to go to the third window and use the key combination ctrl-a +” to check the same.

Toggle Screen Windows
Toggle Screen Windows

In the above output, we can see that we are now in the third window

.

12. How

to switch to the previous screen window

Similarly, we can use the combination ctrl-a + p to switch to the

previous window.

Currently, we are in the third window. So, let’s use the key combination ctrl-a + p twice and verify

that we are in the first window again:

Switch to previous screen windowSwitch to previous
screen

window

In the previous output, we can see that we are now back to the first window

.

13. How to switch between the current window and the previous one

Sometimes, we just want to switch between two windows. In such cases, we can use the key combination ctrl-a + ctrl-a.

To understand this,

first switch to the first window: Switch to the first screen windowSwitch to the first screen window

Then switch

to the last window: <img src="https://www.tecmint.com/wp-content/uploads/2021/08/Switch-to-First-Screen-Window.png" alt="Move to the last

window of
the
screen” /
>
Move to last

screen window

Finally, use the key combination ctrl-a + ctrl-a to jump to the first window again

.

14. How to jump to a particular screen window

In the examples above, we saw several ways to switch between windows. However, sometimes we want to jump into a particular window. In such scenarios, we can use the window numbers.

First, let’s use the key combination ctrl-a + ” to find

the window number: <img src="https://www.tecmint.com/wp-content/uploads/2021/08/Move-to-Last-Screen-Window.png" alt="Screen window number list

” />Screen window number list In the

above output, the first column represents the window number. It is important to note that, the screen command starts numbering from scratch.

Now, let’s use the key combination ctrl-a + 2 to jump to the third window and verify that we are in the correct window:

Jump to Particular Screen Window
Jump to a Particular Screen Window

15. How

to split a screen window horizontally

Like normal terminals, we can also split the screen window. We can use the key combination ctrl-a + S to split the window horizontally.

Horizontally split screen window
Horizontally split screen window

Now, we can use the ctrl-a + tab key combination

to jump between regions. Finally, we can use the key combination ctrl-a +

X to delete the current region. It is important to note that here we have to use a capital X character.

16. How

to split a screen window vertically

Similarly, we can use the key combination ctrl-a + | to split the current

window vertically: <img src="https://www.tecmint.com/wp-content/uploads/2021/08/Split-Screen-Window-Horizontally.png" alt="Split screen window vertically

” />
Split screen window vertically

In addition to this, we can also use ctrl-a+tab key combination switch between regions and ctrl-a+X key combination to close the current region.

17. Enable screen logging on Linux

Sometimes it’s important to record what you’ve done while you’re on the console. Let’s say you’re a Linux administrator who manages many Linux servers.

With this screen log, you don’t need to type all the commands you’ve made. To activate the screen registration feature, simply press “Ctrl-A” and “H”. (Be careful, we use capital letters ‘H’. Using a non-uppercase ‘h’ will only create a screenshot of the screen in another file called a printout.)

At the bottom left of the screen, there will be a notification telling you to like: Creation of the log file “screenlog.0“. You will find a screenlog.0 file in your home directory.

This feature will add everything you do while in the screen window. To close the screen to record running activity, press “Ctrl-A” and “H” again.

Another way to activate the logging function, you can add the “-L” parameter when the screen is run for the first time. The command will be like this.

[email protected] ~$ screen -L

18. Lock Linux Terminal Screen

The screen also has a shortcut to lock the screen. You can press the shortcuts “Ctrl-A” and “x” to lock the screen. This is useful if you want to lock your screen quickly. This is a sample output of the lock screen after pressing the shortcut.

Screen used by Pungki Arianto in mint. Password:

You can use your Linux password to unlock it

.

19. Add

password to screen session

For security reasons, you may want to put the password in your screen session. You will be prompted for a password each time you want to reattach the screen. This password is different from the previous lock screen mechanism.

To protect your screen password, you can edit the “$HOME/.screenrc” file. If the file does not exist, you can create it manually. The syntax will be like this.

crypt_password password

To create the above “crypt_password“, you can use the “mkpasswd” command on Linux. Here is the command with the password “pungki123“.

[email protected] ~$mkpasswd pungki123 l2BIBzvIeQNOs

mkpasswd will generate a hashed password as shown above. Once you get the hashed password, you can copy it to your file.” screenrc” and save it. So the “. screenrc” will be like this.

password l2BIBzvIeQNOs

The next time you run the screen and detach it, you will be prompted for a password when you try to reattach it, as shown below

: [email protected] ~$screen -r 5741 Screen password:

Type in your password, which is “pungki123” and the screen will be reattached

.

After implementing this screen password and pressing “Ctrl-A” and “x”, the output will be like this.

Screen used by Pungki Arianto in mint. Password: Screen password:

You will be prompted for a password twice. The first password is your Linux password, and the second password is the password you put in your . screenrc.

20. Exit

the Screen Terminal session

There are 2 (two) ways to exit the screen. First, we are using “Ctrl-A” and “d” to separate the screen. Secondly, we can use the exit command to end the screen. You can also use “Ctrl-A” and “K” to kill the screen.

Cheat Sheet Display command

Description of the Display -S command <session_name> Start a new session with the name of the session. screen -ls List of running sessions/screens. screen -x Attach to a running session. screen -r <session_name> Attach to a running session with the name. screen -d <session_name> Detach a running session. Ctrl-a c Create new window. Ctrl-a Ctrl-a Switch to the last active window visited. Ctrl-a <number> Switch to window by number. Ctrl-a ‘ <number or title> Change to window by number or name. Ctrl-a n or Ctrl-a <space> Switch to the next window in the list. Ctrl-a p or Ctrl-a <backspace> Switch to the previous window in the list. Ctrl-a ” View list of windows. Ctrl-a w Show window bar. Ctrl-a k Delete current window. Ctrl-a Kill all windows. Ctrl-a A Rename the current window. Ctrl-a S Split the screen horizontally. Ctrl-a | or Ctrl-a V Split display vertically. Ctrl-a tab Go to the next display region. Ctrl-a X Remove Current Region. Ctrl-a Q Remove all regions except the current one. Ctrl-a H Enable logging in the screen session. Ctrl-a x Lock screen.

That’s part of using screen commands on a daily basis. There are still many features within the screen command. You may see the Display Manual page for more details.

Do you know any other best example of the screen command in Linux? Let us know your views in the comments below.

Contact US