Screen allows you to:

  • Use multiple shell windows from a single SSH session.
  • Keep a shell active even through network disruptions.
  • Disconnect and re-connect to a shell sessions from multiple locations.
  • Run a long running process without maintaining an active shell session.

Installing Screen

Screen might already be installed on your system. To check, enter the following command:


# which screen


If it is already installed, the path to screen should display below.


If it is not yet installed, we need to install it from your distribution's repository. On RedHat and CentOS, enter the following command:


# yum install screen


On Debian and Ubuntu,


# apt-get install screen


 

Starting Linux Screen

Screen is started from the command line just like any other command:


# screen


You are now inside of a window within screen. This functions just like a normal shell except for a few special characters.

 

Control Command

Screen uses the command “Ctrl-a” that’s the control key and a lowercase “a” as a signal to send commands to screen instead of the shell.


For example, entering "Ctrl-a" "?" brings up the screen help page.


Screenshot_1.png


Screen key bidings are the commands that screen accepts after "Ctrl-a". These are configurable in a .screenrc file.

 

Creating Windows

To create a new window, enter "Ctrl-a" "c". This will create a new window with a default prompt. Don't worry, your previous window is still active.


For example, I want to download a file using wget or curl and the open a new window and do other things. wget/curl is still there and stays running in the background.

 


Switching Between Windows

Screen allows you to move forward and back. "Ctrl-a" "n" switches you to the next window.


You can create several windows and toggle through them with “Ctrl-a” “n” for the next window or “Ctrl-a” “p” for the previous window.


Each process will keep running until you kill that window.

 


Detaching Windows

Detaching is the most important part of screen and what makes it extremely useful. Screen allows you to detach from a window and reattach later.


You can detach from the window using “Ctrl-a” “d”. This will drop you into your shell. All screen windows are still there and you can re-attach to them later.


This can be very useful for example when using rsync for server migrations.

 

Re-attach to a Window

If your connection is dropped or you detached from a screen, you can re-attach or return to that screen by using the following command:


# screen -r


This will re-attach your screen. But if you have multiple screens you may get this:


Screenshot_2.png


In this case, just specify the screen you want to re-attach:


# screen -r  31917.pts-5.office

 


Logging Screen Output

I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy.

Using “Ctrl-a” “H”, creates a running log of the session.


Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.


You'll find the log file in your home directory. Another way of activating logging when starting screen is by:


# screen -L

 


Getting Alerts

Monitoring for Activity


Screen can monitor a window for activity or inactivity. This is great if you are downloading large files, compiling, or waiting for output.


If you are waiting for output from a long running program, you can use “Ctrl-a” “M” to look for activity. Screen will then flash an alert at the bottom of the page when output is registered on that screen.


Monitoring for Inactivity


If you are downloading a large file or compiling a program, you can be notified when there is no more output. This is a great signal to when that job is done. To monitor for silence or no output use “Ctrl-A” “_”.

 

Locking the Screen Session

To lock your session, enter “Ctrl-a” “x”.  This will require a password to access the session again. Just use your Linux password.


You can also create a separate password for screen. For this, you'll need to edit your .screenrc file in your home directory “$HOME/.screenrc”.


The syntax will be like this:


password encrypted_password


Where encrypted_password is the password generated by mkpasswd. You can use mkpasswd to create a hashed password for locking your screen sessions. For example:


# mkpasswd john123


# l2BIBzvIeQNOs


Just copy the hash password into your .screenrc file. It should look like this:


password l2BIBzvIeQNOs


The next time you detach and re-attach your screen session, you will be asked for the password you created in .screenrc.


When you lock and unlock your screen session, you will be asked for a password twice. Both your Linux password and the password you created in .screenrc.

 


Stop Screen

To stop screen you can usually just type exit from your shell. This will close that screen window. You have to close all screen windows to terminate the session.


You should get a message once you close all sessions:


# [screen is terminating]


Alternatively, you can use “Ctrl-a” “k”.  You should get a message if you want to kill the screen.