The screen command is a very common command used on Linux to launch and arrange multiple terminal shells within one single shell.
Screen is most of the time used for two purposes.
It can either be used in order to organize multiple shells and navigate between them easily.
It can also be used in order to have long running commands on remote servers.
In fact, screen is launched in order to ensure that you don’t lose any distant work in case of a sudden network outage.
In this tutorial, we are going to have a complete overview of what the screen command on Linux is and how it can be used effectively.
In order to install new packages, you will have to be a sudo user.
If you need to add a user to sudoers on Debian, there is a tutorial for it.
There is also one for Red Hat based distributions.
When you are ready, type this command to make sure that you have sudo rights.
$ sudo -l User user may run the following commands on localhost: (ALL) ALL
Installing Screen on Linux
In order to install screen, you will have to run one of the following commands.
On Debian-based distributions, you can run
$ sudo apt-get install screen
For Red Hat based distributions, you will have to run
$ sudo yum install -y screen
When you are done, you can run this command in order to check your current screen version.
$ screen -v Screen version 4.06.02 (GNU) 23-Oct-17
- Cron Jobs and Crontab on Linux Explained | What is Cron Job & Crontab in Linux with Syntax?
- How To Install Docker on Ubuntu 18.04 & Debian 10
- How To Add User To Sudoers On Ubuntu 20.04
Interacting with Screen on Linux
As described previously, screen is used in order to start an interactive shell where you can rearrange multiple shells in it.
To start your first screen, simply type the following command.
As you can see, there are no differences with your previous shell except for the fact that the header writes
screen 0: antoine@localhost:~
From there, it means that a bash interpreter is executed within another bash interpreter through the screen utility.
To run a command, simply type a command like you would normally do.
Getting help with screen
When interacting with the screen command, you will have to run shortcuts on your keyboard to execute actions within the screen session.
By default, the “Ctrl + A” keystrokes are designed to interact with screen.
As an example, type “Ctrl + A” then “?” in order to get the screen help.
As you can see, you many different options to interact with screen.
The most popular ones are probably the following ones :
- “Ctrl + A” then “d” : detach mode. You can use this option in order to detach (meaning going back to your original shell) and let screen run in the background. This is particularly handy when you are running long tasks on your host.
- “Ctrl + A” then “x” : lockscreen. This is used in order to protect your screen session to be used by another user. As a consequence, a password is set to your session.
- “Ctrl + A” then “c” : screen command. One of the most used commands by far, those bindings are used in order to create a new window within your screen instance.
- “Ctrl + A” then “|” : vertical split. By default, this command will split your current window into two different areas that you can interact with.
- “Ctrl + A” then “S” : horizontal split.
- “Ctrl + A” then “n” : used to navigate between your different window sessions from the one with the lowest index until the one with the greater index.
- “Ctrl + A” then “Tab” : used in order to move your input cursor to one of your different areas within screen.
In most of the cases, those are the commands that you are going to use with screen.
Splitting regions with the screen command
As an example, we are going to create the following layout with the screen command on Linux.
As you can see, we are going to split the terminal both vertically and horizontally.
One window will be used in order to execute a command (like a long running command for example).
A second window will be used in order to monitor the system performance using the top command.
Finally, another window can be used in order to edit a file via the nano editor.
Screen window creation
First, create a screen session by executing the following command.
$ screen -S user-screen
The -S option will create a named session for your screen environment.
When multiple administrators are working on the same system, it might be a good idea to have named sessions to distinguish your sessions from others.
Next, as you are going to manipulate three shell sessions, you are going to need two additional screens.
Execute the “create” command two times (“Ctrl + A” then “c”). You should end up with the following screen.
Notice the header or your terminal shell displaying “screen 2” because three screens are currently active for your session.
Splitting screen windows vertically and horizontally
The next step will be to create your different regions on your current window.
To achieve that, first split your current region vertically by pressing “Ctrl +A” then “|”
You should end up with the following output.
Next, split your layout horizontally by pressing “Ctrl +A” then “S”.
This is what you should get.
Navigate to your second region by pressing “Ctrl +A” then “Tab”. By default, your region should be empty, so you can press “Ctrl +A” then “n” in order to navigate to your screen 0 session.
From there, execute the command you want to run.
Repeat the previous steps in order to execute commands in the other regions.
Remember that you have to navigate between your screen windows when you first enter a split region.
Awesome! You created your complete custom screen session.
Detaching from your screen session
In order to detach from your screen session, simply press the following keystrokes.
$ Ctrl + A then d
Your screen session is still executing in the background.
This is one of the main aspects of the screen command.
It can be used in order to create to a remote host via SSH, perform some actions, and exit to come back to them later on.
This way, you don’t have to manipulate background and foreground that you may lose by closing your current terminal.
To verify that your screen session is still running, run the following command
$ pstree | grep -A 2 -E "screen-"
Reattaching to your screen session
First of all, you can list your screen windows by executing the following command.
$ screen -ls
If you named your screen session, you can simply run the following command
$ screen -r <session_name>
In the example above, I would have to run this command in order to go back to my session.
$ screen -r user-screen
Note that you can also get back to your session by using the screen ID at the very left of the ls command.
$ screen -r 3600
Unfortunately, you lose all your visual changes and you will have to split your windows again.
However, with enough practice, you might learn about the shortcuts very easily.
Locking your screen session
Sometimes, you might want to prevent other users from interacting with your screen session.
In order to lock your screen, hit “Ctrl + A” then “x”.
In order to unlock it, you have to enter the password of the user owning the screen session.
In today’s tutorial, you learnt how you can easily manipulate the screen command on Linux in order to create custom shell environments within a common shell.
You learnt that it can be used on remote servers in order to make sure that you can exit the session and still save your work (if you have long running commands in the foreground for example).
If you are interested about Linux System Administration, we have a complete section dedicated to it on the website.