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:
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:
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
220.127.116.11 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.
18.104.22.168 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.
22.214.171.124 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.
126.96.36.199 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.
188.8.131.52 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:
You can control all of these features with the .screenrc resource file. See man screen for details.
1.13.6 See Also