How to Use the Screen Utility
There are times in the life of a system administrator that one needs to launch a *long*... *long*... *long* ... task to run on a remote server. The challenges with this are as follows:
- if the remote connection is interrupted, the odds of the job being interrupted and having to start from scratch again are equally true.
- The connecting (client) system needs to be running for the duration of the job too
Now, the common approach to these challenges is to simply run the job in 'background' mode. However, dependent on how the server is configured, you could run into issues of any output from the program going to the primary console. This can of course be changed by simply redirecting the output to .. say.. a file.
This article is intended to step you through an alternate method for running a job safely in the background, along with some other tricks.
What is Screen?
The screen utility, originating from the GNU project, is a method for creating 'virtual' terminals / consoles that, if used correctly, can greatly simplify the task of accessing information both remotely and locally. The actual terminology attached to this item of software is "terminal multiplexer" - which is typical of the technological world in that the title is intended to scare the bejeezus out of regular folk.
To put it in laymens terms, "screen" can be thought of as a method for accessing multiple separate terminal sessions inside a single terminal window or remote terminal session. It is, in a way, like a 'text' version of graphical window managers - essentially acting as a wrapper to the background nuances of being able to launch commands.
Similar to remote desktop access software such as VNC, GNU screen allows a user to start applications from one computer, and then reconnect from a different computer and continue using the same application without having to restart it. This makes migrating from locations such as work to home simple and easy.
As already alluded, GNU screen also permits one to run multiple sessions without having to continually log in on separate sessions, making it easier to be productive from any location.
Enough with the small talk though, let's see what we can do with screen in a real life example!
Getting the Software
Enough with the small talk though, let's see what we can do with screen in a real life example!
To begin with, you will need to have screen actually installed. In Debian based systems like Debian, Ubuntu, and Knoppix, you can install the screen utility with the simple command:
apt-get install screen
This will install the utility along with any support packages that it requires. For YUM/RPM based systems such as Redhat, CentOS, and Fedora Core you can install the package with the command:
yum install screen
If you are running a Linux distribution that does not utilize a packaging system such as .DEB or .RPM, then you will need to refer to the documentation specific to that distribution. As a worst case scenario, you can download the source at http://www.gnu.org/software/screen then compile and install it as follows:
tar -zxvf screen-x.x.x.tar.gz (where x.x.x is the version of screen you downloaded) cd screen-x.x.x ./configure make make install
I am not going to get into any error messages etc. related to the manual build process as this is beyond the scope of this document.
Getting to know the Basics
Now that screen is installed, let's start it up! Open a terminal window (either your favorite terminal emulator or one of your system virtual terminals and run the command:
screen
You should be presented with a page displaying the version and license information for the utility. Press Space or Enter to continue.
Now, at this stage you may be a bit confused - Nothing changed! However, you are now in a 'virtual' screen session. Screen has created a new window, numbering the session as '0', and started a command shell inside of it. Type something in the window so that you will recognize it later, perhaps:
export PS1="myscreen0:$PWD: "
which will change the command prompt for that session.
Now .. for the proof. Let's detach that session. Screen has two input modes: command shell mode, and screen administration mode. To enter screen administration mode, you will need to press Ctrl+A simultaneously. There will be no visible change on your terminal, however, screen will now respond to the letter codes specific to screen until such time as a command is completed, or is pressed. To 'detach' from this session of screen, type 'd' then . Voila! You should be back to the prompt you had prior to launching screen!
So.. how do you reattach to that session? Simple! Simply type the command:
screen -r
and your session will "reattach"! Now.. the only time that this may not work is if you have multiple screen sessions launched. In this case, it will return back a listing of the active screen sessions. So, for this you will need to type:
screen -r nnnn.pts-n.hostname
The number will be the PID of the screen session, the pts-n will be the virtual terminal that the screen session was originally launched from, and the 'hostname' will be the hostname of the server it is running on. Note that this will all be presented in the listing of open sessions. Sample output:
#:screen -r There are several suitable screens on: 13864.pts-1.aries (Detached) 13852.pts-1.aries (Detached) screen -r 13864.pts-1.aries
So.. to recap..
-
to start a new screen session, type:
screen
then run your commands etc. that you wish to keep running - You can then detach your session with the key stroke Ctrl+A followed by 'd'.
-
To reattach to the session from any location, you can then type the command:
screen -r
It's as simple as that!
Beyond the Basics
So, now you know how to start a screen session, detach from it, and reattach later on. There is however much more to screen than that.
Multiple Windows
Screen also has the capability of running multiple 'windows' within a single screen session. Each window is enumerated numerically. When you first launch a screen session, the window you are presented with is numbered "0". To create a new window, you will enter 'administrative' mode with the aforementioned keystroke: +A. Then you launch the 'create' command by typing the letter 'c' (create - get it?). So, now you should have two windows available i your session. The initial 'create' will create the new window within the next numeric sequence (ie: 1) but will leave you in window '0'. So, to get to navigate between windows, you enter administrative mode again. The letter commands are:
n - next p - previous
There are some other tricks available for 'window' modes:
- Ctrl+A +A will return you to your previous window
- Ctrl+A # will take you directly to the '#' window id
- Ctrl+A N will display the current window id
- Ctrl+A " will display a selectable list of open windows to switch to. You can then use the arrow keys to select the window you wish to go to.
- Ctrl+A ' will prompt you for which window number to switch to
There are of course much more than just this, but I figure these are some helpful quick tips. Now - what if you want to have the multiple windows all displayed at once in a split screen environment? Simple enough!
Let's start by splitting the screen:
Ctrl+A S
This will create a split 'region' of the screen - where your active window will be at the top. Let's then make the second 'new' region active.
Ctrl+A <Tab>
So.. now the input focus is on the new region, but it can't do anything since there is no window active there. Using the trick we already learned, we can then create a new window in the active region:
Ctrl+A c
Use the Ctrl+A <tab> trick to jump between them and away you go!
When you want to shutdown a region used in a 'split' environment, you can use Ctrl+A x - note: you may be prompted for your password.
Advanced - Multi User Session
We will now go to a more advanced practical application of screen. Have you ever needed to step someone through a set of commands? Have you heard the saying "a picture is worth a thousand words" ? Well then, surely having someone see the commands as you type them, including the results, is even more practical. Now, you could do this via screen shots, or a tutorial like this, but... screen can resolve this for you too.
Let's assume that both you and the remote person have logged in to the same system - remote or otherwise. You (the host) starts off as normal by launching a screen session - but this time with a specific name:
screen -S mysharedsession
Now.. you as the host needs to enable multiuser mode....enter administrative mode using +A and turn the mode on by typing:
:multiuser on
Now.. if you are both logged in as the same user, you can simple have the other person attach to your session by running the command:
screen -x mysharedsession
This will work - you will both see the commands exactly as you type them - however... the drawback to this approach is that the remote user has the exact same ability as you to run commands within this session as well as - shutting the session down. Not only that but the 2nd party must also know the password for the same account you are running...
So.. let's look at setting up a secure session with read only access.
Assuming that you follow the same steps as above to start a screen session, then turn on multiuser mode, let's assume there is another system account called 'guest'. We can then do the following from the host screen:
Ctrl+A :acladd guest
This adds the 'guest' account to the acl list. Now... you will need to change the default permissions...
Ctrl+A :aclchg -w "#"
What this does is remove 'write' permissions for all actions. There are two more things you will want to change to ensure that the 'guest' does not have permission to change any permissions...
Ctrl+A :aclchg -x "#" (removes executable bit) Ctrl+A :aclchg -w "at" (removes administrative write commands)
That's all there is to it - there are of course further things you can do similar to this - like setting group permissions for things like group conferences and the like, but this should help to illustrate the basics. Further information should also be available by typing "man screen".
That concludes this introduction to the screen utility.