Team LiB
Previous Section Next Section

Hack 49. Share Applications and Monitors with screen

Although doing clever things with X gets most people's attention, you can perform some cool tricks with the console using a tool called screen.

screen is a window manager for your terminals. It provides the following nice features:

  • Multiple screens

  • A scrollback buffer

  • Copy and paste buffers

Although this sounds like a fairly boring listafter all, you can do this with multiple X terminalsbear in mind that these features are provided within the terminal. You don't need to be using X; screen works just as well (or better) from one of the Linux virtual terminals or even on a remote machine. For more advanced use, it also provides the ability to detach and share the output of a process. screen is a GNU tool and is available for most Unix-based platforms, including Linux. Most, if not all, distributions provide a package for it, or alternatively it can be downloaded from the GNU web site and compiled.

You can access screen's functions via the control key combination Ctrl-A. Pressing this, and then the question mark (?) key, displays a list of some of the commands you can use.

7.2.1. (Dis)connected

One of the most commonly used features of screenbesides its support for running multiple applicationsis its ability to continue running applications while detached. When you run screen, it creates a new process separate from your current terminal. Then you can close that terminal (or log out), open a new one (or log back in), and reattach to your screen. This is useful for running long background jobs (such as compiling a kernel) that you don't want to accidentally "break" by closing the terminal or logging out. And it's especially useful if you're performing that job on a remote computer where there is a distinctive risk of your connection dropping.

To begin, simply run screen without any parameters:

foo@bar:~$  screen

Now press the control key combination Ctrl-A; then press D. You'll be returned to your original console with the following message:


To see a list of your running screens, type:

foo@bar:~$ screen -ls

You'll see something like this:

There is a screen on:       (Detached)
1 Socket in /var/run/screen/S-user.

Now you can reattach to your screen with:

foo@bar:~$ screen -r

If you've got multiple screens listed, you can select which one to connect to with this:

foo@bar:~$ screen -r n

Here you should replace n with the number shown by screen -ls (e.g., 25091), as shown earlier.

7.2.2. Mirror, Mirror

Another useful feature is the ability to attach to an already attached screen. This is often used for rescuing an uncleanly detached session (e.g., your connection dropped), but you also can use it to mirror applications.

IRC is a good example for this. People running several machines at once (e.g., laptop and desktop) might want to have their IRC channels open on both machines, but don't want to be signed in twice. With screen you don't have to!

From a shell, enter the following:

foo@bar:~$ screen irssi

This launches screen and loads irssi (a console IRC client) onto screen 0 (you don't always have to use an interactive shell).

From another shell (local, or remotely in an SSH shell), enter the following:

foo@bar:~$ screen -ls

This will give you a list of sessions like these:

There is a screen on:        (Attached)
1 Socket in /var/run/screen/S-user.

Using this session number, enter the following:

foo@bar:~$ screen -x 3483

Now you are sharing your screen session; go ahead, try it from multiple shells. You can attach as many sessions as you want, but when you exit the application (assuming you launched the application directly like you did for irssi, not from a screen shell), that screen terminates.

7.2.3. It's Good to Share

It is also possible for multiple users to access a single screen instancee.g., to collaborate on a document or some code. For this to work, the following must apply:

  • screen must be compiled with "multiuser" enabled (most packages are).

  • screen must be suid root (chmod +s /usr/bin/screen).

To do this as user1 and assuming screen is running, press Ctrl-A and then type :multiuser on. Still as user1, press Ctrl-A and then type :acladd username, where username is the user you want to be able to access your screen. For this example, use user2.

As user2, launch screen with:

foo@bar:~$ screen -x user1/

At this point you should be connected to user1's screen. Besides acladd, you also have the aclchg, acldel, and aclgrp commands for controlling who can access your screen, and what they can do (e.g., you can make them read-only). To see who's connected to your session press Ctrl-A. Then press the asterisk (*) key.

To prevent people from (temporarily) editing all your windows, press Ctrl-A and then type :writelock on to lock editing and :writelock off to remove the lock. If people have write permissions, they can "steal" writelock from you. To prevent someone from editing any of your windows, press Ctrl-A and type :aclchg username -w "#".

David Murphy

    Team LiB
    Previous Section Next Section