Managing virtual terminals with Screen

MAGIC SCREEN


Cast a spell upon your console. Instead of juggling multiple terminals, you can manage all your important command line programs in a single window.

By Heike Jurzik

www.sxc.hu

Screen lets you launch multiple virtual consoles in a single terminal. This gives users the ability to keep processes running on remote computers after logging off. And you can open up the screen to other users, letting them share your session.

The terminal applications on KDE and Gnome point the way: you can launch multiple tabs, just like in a browser, thus combining multiple console sessions in a single program window. If you do not use a GUI, or if you use SSH to work on a remote computer, there is no need to do without this convenience. The Screen tool will help you manage multiple virtual terminals, giving you the ability to keep processes running if you log off the remote machine. Screen even provides tools that let you share a console session with other users.

Setting the Screen

To launch the program, just enter screen at the command line. This brings up the splash screen that tells you the version number, and prompts you to press the space bar or Enter to continue. If you prefer to do without the welcome message, you can disable it using an entry in the Screen configuration file (see the "Setting Up Screen" box).

After launching your window juggler, things may not appear to have changed at first glance - the terminal will look like it always did. But your fingertips will tell you a different story, at least they will if you press [Ctrl]+[A] in Bash to move to the start of the input line. Screen uses this shortcut to take you a collection of commands that control the program.

The "Screen Commands" table gives you an overview. Pressing [Ctrl]+[A] and "?" displays the online help (Figure 1). This command reference also tells you that you can restore [Ctrl]+[A]`s "normal" behavior by pressing [Ctrl]+[A], [A]. Pressing the space bar takes you to a second page of online help.

Figure 1: Pressing [Ctrl]+[A] and "?" displays a command reference.

Opening and Closing Windows

As I mentioned earlier, you can quit Screen without stopping the processes running in Screen. The programs that are running inside Screen keep on doing whatever they are supposed to do, and if you revive Screen at some point later, you can check the resulting output. The keyboard shortcut for this is [Ctrl]+[A],[D]. To reestablish contact with Screen, just enter screen -r on the console.

You can also "detach" Screen remotely; you can move Screen to the background without having to interact. To do this, enter screen -d.

You can combine these parameters - to detach a session, and revitalize the session in the current console, just enter screen -dr.

If you have launched multiple Screen sessions, you have to be more specific about what you want to do. The program will let you know if you are not clear about what you tell it to do. As Listing 1 shows, Screen also shows what is going on in the background, and it gives you instructions on moving a session to the foreground. To revitalize the second entry in Listing 1, for example, you would do this:

screen -dr 9865.pts-11.xena

The output is similar if you enter screen -ls. The list shows you the process number, the terminal name, and the computer name, besides letting you know if Screen is running in the foreground (attached) or background (detached). From time to time you see screens with a dead status, but you can easily clean up the mess by entering screen -wipe.

Listing 1: Screen Output
01 $ screen -dr
02 There are several suitable screens on:
03     9559.pts-11.xena  (Detached)
04     9865.pts-11.xena  (Attached)
05 Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Behind the Scenes

Screen lets you scroll back 100 lines by default to check out program output. If you need to increase the buffer, launch Screen with the -h parameter, specifying the new buffer size. The screen -h 1000 command gives you 1000 lines to play with in copy/scroll mode ([Ctrl]+[A],[Esc]). To make this behavior the default, just add a matching entry to the program's configuration file (see the "Setting Up Screen" box).

Window Cleaning

If you have a large number of sessions running in Screen, you can leverage a practical feature to avoid losing track: as the "Screen Commands" box tells you, a shortcut lets you display a status line where you can assign names to individual sessions. To make this view permanent, enter the following command:

screen -X caption always

Various escape sequences add the ability to configure the status line to tell you things like the current user and hostnames, the date and time - in color if you like. To do so, add a combination of colors and details to the command line shown above (not forgetting to escape this in double quotes):

screen -X caption always "%{rw}* %H * | $LOGNAME | %{bw}%c %D |%{-}%-Lw%{rw}%50>%{rW}%n%f* %t %{-}%+Lw%<"

The details in the curly brackets describe the colors, such as red on a gray background (%{rw}), blue on gray (%{bw}), or red on white (%{rW}). %{-} lets you enable the previous color scheme - the STRING ESCAPES section of the manpage gives you more detailed listings. The other variables represent the following:

Additionally, if the list of windows is too long, entering %50> tells Screen to curtail the list to display the following section in the middle of the line. Figure 2 shows the result of this window dressing.

Figure 2: Some window dressing makes working with Screen really convenient.

Leaning Out the Window

With a few tweaks, you can use Screen as a teaching aid. To use Screen as a teaching tool, first assume the root user role and modify the access privileges for the program. Then one user launches Screen and allows other users access to the program.

Working as root, start by setting the s bit for the screen binary:

sudo chmod +s /usr/bin/screen

Then adjust the permissions for the /var/run/screen directory, removing the group's write permission:

chmod g-w /var/run/screen

The user who will be giving visitors access to their screens, should now launch Screen, and type [Ctrl]+[A],[Shift]+[.] (":"), enable multi-user mode at the prompt, and enter one or more usernames as welcome guests:

: multiuser on
: addacl petrosilie

If you intend to invite more than one visitor to share your windows, just enter multiple comma-separated usernames of the visitors.

To visit the host user, a second user should now launch Screen, specifying the -x option, and entering and the host user's name, and the process number of the screen: screen -x petronella/4552.


s bit: Besides read, write, and execute, Linux supports a couple of special permissions. The setuid/setgid bit replaces the standard execute permission (x) for the owner or group. This means that the program will always run with the permissions of the file owner or group, no matter who launches it.

Setting up Screen

Settings for the program are stored in a ~/.screenrc file below your own home directory. You can use any text editor to modify the file. The file is not created by default, so you will need to create it. Most distributions store the global setup file in /etc/screenrc; this file has a number of useful comments and examples to help you get started.

To use the file as a template, just copy it to your own home directory, as follows:

cp /etc/screenc ~/.screenrc

Listing 2 gives you a sample Screen configuration file with comments.

Listing 2: Screen Configuration File
01 # /etc/screenc ~/.screenrc
02 # ------------------------
03 # switch off splash screens on program launch:
04 startup_message off
05
06 # Set buffer, e.g., to 1000 lines:
07 defscrollback 1000
08
09 # Displays a colorful status line with the
10 # session name, date, time, etc.:
11 caption always "%{rw} * %H * | $LOGNAME | %{bw}%c %D | %{-}%-Lw%{rw}%50>%{rW}%n%f* %t %{-}%+Lw%<"
12
13 # [Ctrl]+[F] launches a new virtual terminal
14 # called "MAIL" and automatically establishes a
15 # SSH connection to "another.computer.com":
16 bindkey ^f screen -t MAIL ssh another.computer.com
THE AUTHOR

Heike Jurzik studied German, Computer Science and English at the University of Cologne, Germany. She discovered Linux in 1996 and has been fascinated with the scope of the Linux command line ever since. In her leisure time, you might find Heike hanging out at Irish folk sessions or visiting Ireland.