Home » Shell Scripting » BASH » Linux Screen Command Tutorial

About Toni


Linux Screen Command Tutorial

Screen is a window manager that allows to have several virtual terminals, several sessions and programs in text mode executing simultaneously in the same console. In this tutorial we will see how to use it.

For this tutorial, Linux Mint 18 has been used.

1. Installation

If you don’t have installed Screen, you can install it easily with apt-get:

sudo apt-get update
sudo apt-get install screen

2. Creating a screen

To create a screen, we just have to execute the screen command:


We will see an informative screen about the software itself. Just hit return to exit.

At this moment, a new screen will be generated inside the terminal window. You may have noticed it, since the buffer has been cleared.

And that’s it!

This has created a pipe file for this session in /var/run/screen/S-<username>/ directory. The username I’m working with is named julen, so, the directory is /var/run/screen/S-julen/.

In my case, the name for the session is 3360.pts-0.julen-VAIO. screen generates the sessions with the following format:


You may be wondering how to exit the screen. You can obviously close the terminal, but that would leave the session alive. If you want to kill it, you can execute the following command inside the session you want to kill:


You will see that you return to the “original” shell, and that screen has told that, effectively, you have exited its screen:

[screen is terminating]

If you check the directory where screen saves the sessions, you will notice that the previous session has disappeared.

2.1. Creating windows inside screens

We have seen that executing screen in the host computer, creates a new session, which is saved in /var/run/screen/S-<username>/ directory. If we would execute screen n times in the host, we would have n sessions.

Inside each session, we can have other several screens, named windows. The windows are created, again, executing the screen command. So, the following:

# We are know inside a screen session

Would only create a session in /var/run/screen/S-<username>/:


This is because, as we have said, executing screen inside a screen session will create a window inside that screen, not another session.

In the next sections we will see the commands available for these screens.

3. Screen commands options

screen command has many inline options. The following table shows and explains them.

Create screenscreenCreates a session, as we have already seen.
Create screen with namescreen -S <name>Creates a session with a specific name. This is, actually, the recommended way for creating sessions, for having identified each session with a proper name.
Create detached screenscreen -S <name> -dmSThis is for creating a screen that will be detached from its creation, for which -dmS option is used. When using this option, the -S option is mandatory.
Show screensscreen -ls|-listShows the screen sessions. This can also be done looking at the directory where there are stored, as we have seen before. But using this option (actually, one of them, -ls or -list) can be considered better since more information about the session is shown, apart from being easier to type.
Reattach screenscreen -r  <name|id>Reattaches a detached session. If there’s only one session detached, it’s not necessary to specify the name/id; screen is smart enough to deduce that will be that. If there’s more than one session detached, you will need to specify the session, with the name (if you used the -S option to create the session); or the id (tty.host format).
Try to reattach screen, create new one if impossiblescreen -R <name|id>Tries to reattach a detached screen. If the screen couldn’t be reattached, a new session will be created. If no name or id is specified, a new session will be created.
Specify a shell for the screenscreen -s <path/to/shell>Creates a session with the specified shell. By default, the sessions are created with $BASH value, which will probably be /bin/bash.
Create a window with namescreen -t <window-name>By default, the created windows inside a session are named bash. We can specify a name that can help us to have identified properly each window with this option.
Delete screenscreen -X -S <name|id> killKills the screen session. This would be equivalent to removing the pipe file from /var/run/screen/S-<username/.
Delete all screensrm /var/run/screen/S-${USER}/*Not actually a screen command, but serves for this. As each user saves its sessions in different directories, we have to use the $USER variable.

4. Key bindings

When we are inside a screen and its windows, we have available some special key combinations to perform some actions, e.g., navigating through different windows.

These key bindings consist on pressing Ctrl + a, and then pressing the key(s) in question. Not at the same time: first, Ctrl + a; then, release them; and finally, press the key in question.

The following table shows the most used key bindings.

Detach screenCtrl + a dDetaches the screen.
Clear windowCtrl + a CClears the window, just like with clear command.
Create windowCtrl + a cCreates a new window in the screen.
Show number and title of current windowCtrl + a NShows a message with the window number and title like: This is window n (title).
Lock windowCtrl + a Ctrl + xLocks the window. Just like locking the host. The password to unlock the window is, of course, the password of the user where the session is running in.
List windowsCtrl + a wShows a list of the windows of the current screen, with the number and the name of each one.
Rename current windowCtrl + a AShows a menu for introducing a new name for the current window.
Go to window nCtrl + a n (0…9)Navigates to the window.
Go to the next windowCtrl + a nNavigates to the next window.
Go to the previous windowCtrl + a backspaceNavigates to the previous window (the opposite of the previous command).
List windows and select where to goCtrl + a Shows a menu of the current windows, allowing to select to which window navigate to.
Select the window to go toCtrl + a Prompts a menu for entering the window number to navigate to.
Kill the current windowCtrl + a kKills the current window, asking for confirmation.
Kill all the windows, and terminate the sessionCtrl + a \Kills all the window and the screen session, asking for confirmation.
Show helpCtrl + a ?Shows the key bindings.

5. Customizing Screen

screen allows us to configure it as we want. It looks for the configuration in a file named .screenrc in the user’s home directory; or system wide, in /etc/screenrc.

5.1. Disabling startup message

You may already be tired of the startup message when you create a screen, that one about the version, copyright, etc. This can be disabled with the following line in the configuration file:

startup_message off

5.2. Setting default windows for each session

One of the most interesting possibilities is defining a set of default windows for every screen session. For that, we just have to write the commands we would execute, in the configuration file.

For example, let’s say that for every screen session we want, apart from a shell, a Python and PHP console. We could write the following in the configuration file:

screen -t Shell  /bin/bash
screen -t Python /usr/bin/python3ve
screen -t PHP    /usr/bin/php -a # Interactive mode.

select 0 # After creating the windows, go to the first one.

Now, for the configuration shown above, you would have, for each screen session, those three windows.

5.3. Key bindings

The remaining customization that is worth mentioning is the configuration of key bindings, for having a better experience.

A usual practice is to use the function keys (F1 – F12). For example, we could use them for accessing windows:

bindkey "^[[[A"  select 1  # F1 -> window 1
bindkey "^[OQ"   select 2  # F2 -> window 2
bindkey "^[OR"   select 3  # F3 -> window 3
bindkey "^[OS"   select 4  # F4 -> window 4
bindkey "^[[15~" select 5  # F5 -> window 5
bindkey "^[[17~" select 6  # F6 -> window 6
bindkey "^[[18~" select 7  # F7 -> window 7
bindkey "^[[19~" select 8  # F8 -> window 8
bindkey "^[[20~" select 9  # F9 -> window 9
bindkey "^[[21~" select 10 # F10 -> window 10
bindkey "^[[23~" prev      # F11 -> previous window
bindkey "^[[24~" next      # F12 -> next window

Note: if you want to know which key code corresponds to each keystroke, you can use showkey:

showkey -a

And then press a key to know its code.

6. Summary

In this tutorial we have seen how to use the Screen utility, for managing several virtual windows in the same terminal. We have seen the command’s inline options, and also the special key combinations for when we are inside the virtual windows. To end up with the tutorial, we have shown how we can customize Screen with the configuration file.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments