Home arrow Site Administration arrow Page 4 - More Hacks for the User Environment in BSD

Hack 12: Use Multiple Screens on One Terminal - Administration

In this article, the third and last of a series, you'll learn how to customize a BSD environment for other users; maintain your chosen environment on multiple machines; and more. It is excerpted from chapter one of the book BSD Hacks, written by Dru Lavigne (O'Reilly, 2005; ISBN: 0596006799), Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. More Hacks for the User Environment in BSD
  2. Hack 10: Maintain Your Environment on Multiple Systems
  3. Hack 11: Use an Interactive Shell
  4. Hack 12: Use Multiple Screens on One Terminal
By: O'Reilly Media
Rating: starstarstarstarstar / 1
December 21, 2006

print this article



Who says you canít run multiple sessions from one terminal?

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:

  1. Are you worried about making your services vulnerable just so you can administer them across the Internet?
  2. Do you want a secure connection?
  3. Do you want to run multiple terminal sessions from a single login?
  4. Do you want to be able to password protect your session to prevent unauthorized access?
  5. Do you want multiple windows with labels and of different sizes?
  6. Do you want to copy and paste between the windows?
  7. Are you prepared to lose a connection to your remote machine in the middle of a critical operation?
  8. Would you like to be able keep the session active even after youíve lost it or youíve logged off the remote server?
  9. Do you want to take over a session that you launched from another machine?
  10. Would you like to keep a hardcopy log of your sessions?

You are indeed a poor soul if youíve reconciled yourself to the standardsshlogin without any hope of resolving these questions. But all is not lostóthe solution isscreen.

What Is screen?

screenis 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 installingscreenis 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.

Getting Started

screenhas 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 invokingscreenfor 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

Multitasking with screen

The best way to understandscreenís power is to give you an example of how you can take advantage of it.

Suppose you are sitting at workstationalpha. You want to access your machine,bravo, to download and compile the latest PostgreSQL source code. Log into hostbravoas usercharlieusing SSH:

  % ssh -l charlie bravo

Invokescreen. If you give it a session name, with the-sflag, 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-

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.

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

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.

TypeC-a Sto split the screen, where theSis 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 withC-a Tab. Create a new window withC-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.

Better window switching.To list all windows associated with this session, use the commandC-a ".

If cycling through windows is onerous, you can also switch between windows by ID. For example,C-a ' 1will 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 withC-a ' 0, and extract the tarball into its own directory. Enter window 2 withC-a ' 2, and navigate to the uppermost directory of the source code to read the INSTALL file.

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 commandC-a A. For example,C-a A email,C-a A source, andC-a A docseem like a big improvement for the currently active windows.

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

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.

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). TypeC-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 cansshintobravo from a new location and attach to your existing screen session withscreen -dr A. Remember,Awas the name of the screen session.

After finishing and sending your email, kill off that screen. Type the commandC-a kin theemailwindow.

With that business finished, scroll back through the INSTALL text file to find interesting configuration switches. You could retype them, butscreenhas a perfectly capable copy mode. TypeC-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 thesourcewindow and useC-a ]to paste the copied text.

You donít need thedocwindow anymore, so switch into it and eitherexitthe shell or use the key sequenceC-a kto kill it. You could also merge the split screens together with the key sequenceC-a X.

Once youíve started compiling, you can close the terminal but leave the session active by detaching it; just typeC-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.

Additional Features

These are only the basics of whatscreencan 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. Seeman screenfor details.

See Also

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

>>> More Site Administration Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates


Dev Shed Tutorial Topics: