Software for the Open Enterprise™

Turbocharge an SSH Connection with screen

Novell Cool Solutions: Feature
By Kirk Coombs

Rate this page
Reader Rating    from 0 ratings

Posted: 13 Sep 2005

Applies to:

  • SUSE LINUX Professional
  • SUSE LINUX Enterprise Server
  • Open Enterprise Server
  • Novell Linux Desktop

Background

SSH provides a great mechanism for gaining shell access to a remote computer.  It does have some limitations, however:

  • It does not allow for more than one shell session per connection.  If multiple sessions are needed, more SSH connections need to be created.  This results in more windows to deal with on the client.
  • If a SSH connection is broken, and processes that were running in it are immediately terminated.

screen is a handy utility which fixes these limitations.  If screen is installed on a remote computer (and it is by default on all SUSE distributions), then a user can SSH into that computer then use screen to add more sessions, copy and pase text between these sessions, and keep the sessions alive across SSH connections. screen does not have to be used in conjunction with SSH, but it makes most sense to do so.

A Quick screen Example

As an example, consider two computers zeus and hades.  A user on zeus would like to connect to hades and use two programs at the same time, say top and vi. Do do so, the user simply needs to SSH into hades and execute the screen command, as shown.

connecting to screen

As soon as screen launches it displays a splash screen. Pressing the space key returns the user to the command prompt.

screen splash

Now screen is active, and can be used to launch additional programs.  To launch another program and maintain the current command-prompt use:

# screen program_name

as follows.

launch programs

The new programs are launched in their own 'screen'.  These are numbered 0-9, starting at 0.  In this case, the command-prompt is on screen 0, top is on screen 1, and vi is on screen 2. To switch between the screens, use CTRL-a [0-9].  For example, after typing screen top the application is launched in its own 'screen'. To return to the command-prompt to launch vi hold CTRL and press a.  Release both of them, and press 0.

All command-keys in screen begin the the sequence, CTRL-a.  Another great command is ".  Press CTRL-a then " and a list of the current screens is listed.  Simply navigate them with the arrow keys to select the desired screen.

screens list

After launching both programs, return to the command-prompt on screen 0, and type pstree. Notice that the top and vi process both are sub-processes of screen.

pstree output

Now, simulate a broken SSH connection by closing the window.  If this were a typical connection, both top and vi would immediately terminate. With screen they do not.  Initiate a new SSH connection then pull up a list of the available screen sessions by entering:

# screen -ls

Notice that there is an active session.  Multiple sessions can exist at once.  To re-connect to a session, enter:

# screen -r <session name>

as shown.

re-attach to a screen session

screen also has great copy and paste support.  To copy, execute the command: CTRL-a [.  This enters copy mode.  Use the arrow keys to move the cursor to the beginning of a selection, then hit space. Move the cursor with the arrow keys to create the selection then hit space again to signal the end of the selection.  Now, go to to desired paste location (in the current screen or a different screen).  Type CTRL-a ] to paste the selection.

Copy...

copy

...and paste.

paste

To terminate screen simply exit each program on each screen then type exit on the final command-prompt.  To exit all screens at once, type CTRL-a :exit.

Additional screen Options

This example only scratched the surface of what screen can do. For more information see the info pages (info screen), the man page (man screen), or the online help within screen (CTRL-a ?). There is also a great WIKI page at http://gentoo-wiki.com/TIP_Using_screen.

Following are some of the more common functions. Remember all functions are preceded with CTRL-a.


c
Create a new screen. This creates a new screen with a shell.  It is an alternative to using screen program_name.
0-9
Switch to the desired screen.
"
Give a list of the current screens.
?
Activate the online help
[
Begin copy mode.  To make a selection use the arrow-keys to move to the beginning of the selection and press space.  Continue the selection and press space to end.
]
Paste a copied selection.
S
Split the current view. This creates an empty pane that other screens can be displayed in.
X
Remove the current split view.
TAB
Switch to another pane in a split view.
k
Kill a screen window.
d
Detach the current screen session.

Legal | Privacy | Subscribe | Feedback | RSS | Contact | Printer Friendly

© 2005 Novell, Inc. All Rights Reserved.