Most system administrators I know carry a pocket multi-tool of some sort. The free software equivalent of that
tool is a program called GNU Screen.
According to its website, GNU Screen (referred to as "screen") is "a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells." In short, you can have multiple terminal sessions running in a single window or serial console session without needing a graphical user interface (GUI).
Screen is included with most modern Linux distributions and Mac OS X, and binary packages are available for Solaris, AIX, HP-UX and IRIX. For most any other UNIX-like systems, source code can be downloaded from the Free Software Foundation.
Once screen (and its associated termcap entries -- see the Install file if you're compiling from source) is installed, run it from a command prompt:
Version information and GNU GPL copyright notice will be displayed along with a prompt to press the space bar or return key. Do so and the screen will clear, giving a command prompt back. Screen has now taken over and your interactive shell session is now running "inside of" it.
Screen's most useful feature is terminal multiplexing. To demonstrate, start editing a file with your choice of text editors. When the session is up and you have a screen full of text, press CTRL-a and then c. This will create a second terminal session in the same window, clearing the screen and displaying another shell prompt. However, your original editor session is not gone! Press CTRL-a twice to switch back to the editor session from your second virtual screen.
To start additional screens, just press CTRL-a (which is screen's command key) and then c (for create). Once you have created more than two screens, you can't just flip back and forth between them by pressing CTRL-a twice; you'll have to press CTRL-a once and then the number of the screen you want to switch to (starting with 0 for the first screen, 1 for the second, and so on). If you forget where you are, pressing CTRL-a and " will give you an organized list of screens. Pressing CTRL-a and A will let you rename a screen from the default (normally the name of your shell) to something more useful. For instance, I use "email", "chat" and "usenet". CTRL-a and k will kill a specific terminal session once you're done.
The second most useful feature of screen is the ability to de/re-attach a multiplexed terminal session. Normally, disconnecting from an SSH or telnet shell session, killing an xterm window, or logging out from a serial console will cause you to lose whatever you are working on. Screen solves that problem by allowing you to put your entire session into the background, log out and then come back to that same session later with no data lost. To demonstrate, start screen and an editor session as in the prior example, then press CTRL-a and then d (for detach). A message will appear stating that your session has been detached, and your non-screen command prompt will return.
Your screen session is now running in the background. You can now kill your shell session or log out of the system. To retrieve your screen session, log back in or open another terminal window and tell screen to reattach what you were working on:
$ screen -r
Your previously detached screen session should pop back up, complete with text editor. This is useful if you have to switch terminals during a work day or work remotely and don't want to lose your work due to a flaky connection. If your controlling terminal disappears while running screen, the session will automatically detach so that you can log in and reattach it without losing data. Multiple screen sessions can be detached in the background; running
screen –r without specifying a session will get you a list and instructions on how to reattach to a specific session.
$ screen –r
There are several suitable screens on:
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
This short tutorial has barely scratched the surface of the many capabilities of GNU Screen. Other features include:
- Session logging to a text file or hard-copy printer
- Cut and paste between virtual screens
- Split-screen windows in the same display
- Screen session sharing between multiple connections
- Screen locking and password protection
To get a full list of screen commands and keybindings, press CTRL-a and ? (question mark).
More information is available from the manual page. To read it run
GNU Screen has been part of my sys admin toolkit for the past 12 years -- you should make it part of yours.