Hack 12 Use Multiple Screens on One Terminal


figs/moderate.gif figs/hack12.gif

Running a graphical environment is great. You can have numerous applications and utilities running, and you can interact with all of them at the same time. Many people who have grown up with a GUI environment look down upon those poor souls who continue to work in a terminal console environment. "After all," they say, "you can only do one thing at a time and don't get the same information and control that you have in a desktop environment."

It's true; they do say those things. (I am curious to know who they are, however.)

It's also true that the utility of a graphical environment diminishes when you need to administer machines remotely. Do you really want to squander network bandwidth just to maintain a GUI session?

Here are some more questions to ask yourself regarding remote administration:

  • Are you worried about making your services vulnerable just so you can administer them across the Internet?

  • Do you want a secure connection?

  • Do you want to run multiple terminal sessions from a single login?

  • Do you want to be able to password protect your session to prevent unauthorized access?

  • Do you want multiple windows with labels and of different sizes?

  • Do you want to copy and paste between the windows?

  • Are you prepared to lose a connection to your remote machine in the middle of a critical operation?

  • Would you like to be able keep the session active even after you've lost it or you've logged off the remote server?

  • Do you want to take over a session that you launched from another machine?

  • Would you like to keep a hardcopy log of your sessions?

You are indeed a poor soul if you've reconciled yourself to the standard ssh login without any hope of resolving these questions. But all is not lost the solution is screen.

1.13.1 What Is screen?

screen is a terminal screen window manager. That means you can use a console and run multiple terminals at the same time. The fancy term for this ability is multiplexing.

Getting and installing screen is straightforward using the ports facility:

# cd /usr/ports/misc/screen # make install clean

I'm working with Version 4.00.01 (FAU) 18-Sep-03.

1.13.2 Getting Started

screen has many options, settings, and commands. Although we'll attempt to address the major features, the definitive reference is, of course, the manpage.

There are three methods of command-line invocation:


screen [ - options ] [ cmd [ args ] ]

For invoking screen for the first time and running specific options and commands


screen -r [[ pid.]tty[ .host]]

For attaching to and detaching from running sessions


screen -r sessionowner/[[ pid.]tty[ .host]]

For attaching to and detaching from existing sessions being run by other users

1.13.3 Multitasking with screen

The best way to understand screen's power is to give you an example of how you can take advantage of it.

Suppose you are sitting at workstation alpha. You want to access your machine, bravo, to download and compile the latest PostgreSQL source code. Log into host bravo as user charlie using SSH:

% ssh -l charlie bravo

Invoke screen. If you give it a session name, with the -s flag, you can address this session by name. This will pay off shortly.

% screen -s A

Go ahead and download the source code now:

% ftp ftp://ftp3.ca.postgresql.org/pub/source/v7.4/postgresql-7.4.tar.gz

1.13.3.1 Using windows with screen

So far, this has no advantage over a normal SSH login. Yet suppose you need to send some email while you're downloading. Instead of putting the download into the background, create another screen window with the key sequence C-a c. This symbolizes that the Ctrl key is pressed with the lowercase letter a and then, after releasing them, you press a second key, in this case c.

At this point the console will go blank. You'll be presented with a second window. Use your favorite email client to compose your message.

1.13.3.2 Switching between windows

You'll probably want to switch between the download and mailer windows. Cycle forward in the window list with C-a n. Cycle backward with C-a p, although you'll likely see no difference with two windows.

1.13.3.3 Splitting windows

Being the efficient person that you are, you'd like to compile the source code as soon as it has downloaded. Even though you haven't completed your email, go back to the original window and extract the tarball:

% tar -xzpvf  postgresql-7.4.tar.gz

Wise administrators read the INSTALL file to make sure all the correct options are invoked. It'd be very handy to be able to read the instructions as you compose the configure command in the same console. screen comes to the rescue here, too: split the window horizontally, running configure in the top half and reading the documentation in the bottom half.

Type C-a S to split the screen, where the S is uppercase. You should see a wide horizontal bar in the middle of the screen. The top window will show whatever existed when you split the window. You'll also see the window's ID on the left side of the middle bar, along with the name of the shell.

The bottom window doesn't yet have a shell running. Set the focus to the other window with C-a Tab. Create a new window with C-a c, as usual. Notice that the window has the ID of 2 (shown in the bottom lefthand corner); that's because the email window that you created after starting the download has the ID of 1.

1.13.3.4 Better window switching

To list all windows associated with this session, use the command C-a ".

If cycling through windows is onerous, you can also switch between windows by ID. For example, C-a ' 1 will go to window 1.

Be prepared for a little confusion because the screen remains split and now shows the window of your choice in the currently focused window. You can quite easily show the same window in both the top and bottom halves.

Enter window 0 with C-a ' 0, and extract the tarball into its own directory. Enter window 2 with C-a ' 2, and navigate to the uppermost directory of the source code to read the INSTALL file.

1.13.3.5 Naming windows

ID numbers identify windows pretty well, but I prefer to use names of my own choosing. Change a window's name with the command C-a A. For example, C-a A email, C-a A source, and C-a A doc seem like a big improvement for the currently active windows.

Now, listing the active windows with C-a " will show the following:

NUM NAME 0   source 1   email 2   doc

At this point, you have one screen session with three windows. Your terminal is split such that it shows two windows at the same time.

1.13.4 Attaching and Deattaching

Suppose you are called away from the workstation in the middle of a sensitive operation (that is, you haven't yet sent your email). Type C-a x to protect your session. Depending on your configuration, you will either input a password for the screen or use the default account password.

Now suppose you don't return to your workstation. What do you do? You can ssh into bravo from a new location and attach to your existing screen session with screen -dr A. Remember, A was the name of the screen session.

After finishing and sending your email, kill off that screen. Type the command C-a k in the email window.

With that business finished, scroll back through the INSTALL text file to find interesting configuration switches. You could retype them, but screen has a perfectly capable copy mode. Type C-a ESC.

Use the cursor keys to navigate to the portions of the document you want to copy. Hit the space bar to mark the beginning of the text to copy. Scroll around to the end of the text with the cursor keys. The current selection will display in reverse video. When you're satisfied, hit the space bar to copy the current selection into the buffer.

Switch to the source window and use C-a ] to paste the copied text.

You don't need the doc window anymore, so switch into it and either exit the shell or use the key sequence C-a k to kill it. You could also merge the split screens together with the key sequence C-a X.

Once you've started compiling, you can close the terminal but leave the session active by detaching it; just type C-a d. One of the nice features about detaching the screen is that it is done automatically if you lose connection with the server, so you won't lose your session. You can reattach to the session later from the same location or from another workstation altogether.

1.13.5 Additional Features

These are only the basics of what screen can do. Here's a quick listing of other features you might enjoy:

  • Since the key bindings are not cast in stone, you change them as you see fit in the .screenrc resource file.

  • It's possible to authorize other users access to your screen session via an access control list.

  • More than one user can access the same screen session.

  • You can create any number of windows in a given screen session.

  • It's possible to hardcopy all activity in a screen session to a buffer and even a file.

  • An extensive system of copy and paste features exist within the screen session.

You can control all of these features with the .screenrc resource file. See man screen for details.

1.13.6 See Also

  • man screen

  • The GNU Screen home page (http://www.gnu.org/software/screen)



BSD Hacks
BSD Hacks
ISBN: 0596006799
EAN: 2147483647
Year: 2006
Pages: 160
Authors: Lavigne

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net