20 Screen Command Examples to Manage Linux Terminals

Brief: In this guide, we will discuss some useful examples of the 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, executing time-consuming database queries, and so on. Sometimes these important tasks get 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 multiplexes a physical console between several processes (typically interactive shells). It offers a user to open several separate terminal instances inside a single terminal window manager.

The screen application is very useful if you are dealing with multiple programs from a command-line interface and for separating programs from the terminal shell. It also allows you to share your sessions with others users and detach/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 usage and benefits of the screen command using practical examples.

So let’s get started.

Install Screen Command in Linux

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

Please follow your distribution installation procedure to install the screen.

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

Now, let’s verify that the command has been installed successfully:

$ screen --version

Screen version 4.8.00 (GNU) 05-Feb-20

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

Let’s start to see the usage of the screen command in Linux with the following examples.

Screen Command Syntax

The syntax of the screen command is as follows

$ screen [OPTIONS] [ CMD [ ARGS ] ]

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

1. Start Screen for the First Time

Just type the screen at the command prompt. Then the screen will show an interface exactly as the command prompt.

$ screen
Screen Terminal Program
Screen Terminal Program

2. Show Screen Parameter

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

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

Screen Terminal Key Bindings
Screen Terminal Key Bindings

To get out of the help screen, you can press the “space-bar” button or “Enter“. (Please note that all shortcuts which use “Ctrl-A” are done without quotes).

3. How to List All Open Windows

In the previous example, we started a new shell session using the screen command. Now let’s use the ctrl-a + " key combination to list all open windows of the current screen session:

List Screen Open 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 it starts from zero.

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

4. How to Terminate Screen Window Session

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

Terminate Screen Window Session
Terminate Screen Window Session

In the above output, we can see that the screen command is waiting for the user’s confirmation. It shows the warning message in the left bottom corner.

Just like other Linux commands, we can use 'y' to continue or 'n' to abort the operation.

5. How to Start a Screen Session with Name

The screen command allows us to create a session with a user-defined name, which comes in handy when there are multiple sessions 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. Detach the Terminal Session with Screen

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

You are in the middle of SSH on your server. Let’s say that you are downloading a 400MB patch for your system using the wget command.

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

Take a look at this command. First, you have to enter the screen.

$ screen

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

$ sudo apt install dpkg
Sample Output
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following packages will be upgraded:
  dpkg
1 upgraded, 0 newly installed, 0 to remove and 1146 not upgraded.
Need to get 2,583 kB of archives.
After this operation, 127 kB of additional disk space will be used.
Get:1 http://debian.linuxmint.com/latest/ 
testing/main dpkg i386 1.16.10 [2,583 kB]
47% [1 dpkg 1,625 kB/2,583 kB 47%]     14,7 kB/s

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

[detached from 5561.pts-0.mint]
pungki@mint ~ $

7. Re-attach the Terminal Session with Screen

After you detach the screen, let’s say you are disconnecting your SSH session and going home. In your home, you start to SSH again to your server and you want to see the progress of your download process. To do that, you need to restore the screen. You can run this command:

pungki@mint ~ $ screen -r

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

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

pungki@mint ~ $ screen -ls
Sample Output
pungki@mint ~ $ screen -ls
There are screens on:
        7849.pts-0.mint (10/06/2021 01:50:45 PM)        (Detached)
        5561.pts-0.mint (10/06/2021 11:12:05 AM)        (Detached)
2 Sockets in /var/run/screen/S-pungki

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

pungki@mint ~ $ screen -r 7849

8. Using Multiple Screen Terminal Windows

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

First, you can detach the first screen and run another screen on the real terminal. Second, you do a nested screen as shown.

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

List Screen Active Windows
List Screen Active Windows

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

9. How to Rename a Screen Window

In the previous example, we created two new windows. However, their default names are not providing any details about them. In such cases, we can assign some meaningful names to them.

We can use the ctrl-a + A key combination to rename the current window session. This key combination prompts a 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. Next, 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 ctrl-a + n key combination to move to the next windows. Let’s understand this with an example.

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

Switch to Screen Windows
Switch to Screen Windows

Now, let’s press the ctrl-a + n key combination two times to go to the third window and use the ctrl-a + " key combination to verify the same.

Switching Screen Windows
Switching Screen Windows

In the above output, we can see now we are on the third window.

12. How to Move to the Previous Screen Window

In a similar way, we can use the ctrl-a + p combination to switch to the previous window.

Currently, we are on the third window. So, let’s use the ctrl-a + p key combination two times and verify that we are on the first window again:

Switch to Previous Screen Window
Switch to the Previous Screen Window

In the above output, we can see that now we came back to the first window.

13. How to Switch Between Current and Previous Window

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

To understand this, first switch to the first window:

Switch to First Screen Window
Switch to First Screen Window

Next, switch to the last window:

Move to Last Screen Window
Move to the Last Screen Window

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

14. How to Jump to a Particular Screen Window

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

First, let’s use the ctrl-a + " key combination to find the window number:

List Screen Window Numbers
List Screen Window Numbers

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

Now, let’s use the ctrl-a + 2 key combination 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

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

Split Screen Window Horizontally
Split Screen Window Horizontally

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

Lastly, we can use the ctrl-a + X key combination to remove the current region. It is important to note that here we have to use an uppercase X character.

16. How to Split a Screen Window Vertically

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

Split Screen Window Vertically
Split Screen Window Vertically

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

17. Enable Screen Logging in Linux

Sometimes it is important to record what you have done while you are in the console. Let’s say you are a Linux Administrator who manages a lot of Linux servers.

With this screen logging, you don’t need to write down every single command that you have done. To activate the screen logging function, just press “Ctrl-A” and “H“. (Please be careful, we use a capital ‘H’ letters. Using non-capital ‘h’, will only create a screenshot of the screen in another file named hardcopy).

At the bottom left of the screen, there will be a notification that tells you to like: Creating logfile “screenlog.0“. You will find a screenlog.0 file in your home directory.

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

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

pungki@mint ~ $ screen -L

18. Lock Linux Terminal Screen

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

Screen used by Pungki Arianto  on 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 to your screen session. A Password will be asked whenever you want to re-attach the screen. This password is different from the Lock Screen mechanism above.

To make your screen password protected, you can edit the “$HOME/.screenrc” file. If the file doesn’t exist, you can create it manually. The syntax will be like this.

password crypt_password

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

pungki@mint ~ $ mkpasswd pungki123
l2BIBzvIeQNOs

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

password l2BIBzvIeQNOs

Next time you run the screen and detach it, a password will be asked when you try to re-attach it, as shown below:

pungki@mint ~ $ screen -r 5741
Screen password:

Type your password, which is “pungki123” and the screen will re-attach again.

After you implement this screen password and press “Ctrl-A” and “x“, then the output will be like this.

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

A Password will be asked to you twice. The first password is your Linux password, and the second password is the password that you put in your .screenrc file.

20. Leaving Screen Terminal Session

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

Screen Command Cheat Sheet

Command Description
screen -S <session_name> Start a new session with session name.
screen -ls List running sessions / screens.
screen -x Attach to a running session.
screen -r <session_name> Attach to a running session with name.
screen -d <session_name> Detach a running session.
Ctrl-a c Create new window.
Ctrl-a Ctrl-a Change to last-visited active window.
Ctrl-a <number> Change to window by number.
Ctrl-a ' <number or title> Change to window by number or name.
Ctrl-a n or Ctrl-a <space> Change to next window in list.
Ctrl-a p or Ctrl-a <backspace> Change to previous window in list.
Ctrl-a " See window list.
Ctrl-a w Show window bar.
Ctrl-a k Kill current window.
Ctrl-a Kill all windows.
Ctrl-a A Rename current window.
Ctrl-a S Split display horizontally.
Ctrl-a | or Ctrl-a V Split display vertically.
Ctrl-a tab Jump to next display region.
Ctrl-a X Remove current region.
Ctrl-a Q Remove all regions but the current one.
Ctrl-a H Enable logging in the screen session.
Ctrl-a x Lock (password protect) display.

That’s some of the screen command usage on daily basis. There are still a lot of features inside the screen command. You may see the screen man page for more detail.

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

Similar Posts