Tip

How to use GNU screen: Sys admin tutorial

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

    Requires Free Membership to View

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:

$ screen

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.

[detached]
$

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:
584.pts-1.machine (Detached)
20694.pts-33.machine (Detached)
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 man screen.

GNU Screen has been part of my sys admin toolkit for the past 12 years -- you should make it part of yours.

ABOUT THE AUTHOR: Bill Bradford is the creator and maintainer of SunHELP and lives in Houston, Texas, with his wife Amy.

This was first published in March 2008

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.