GNU Screen is a terminal multiplexer and session management tool developed to allow users to resume terminal sessions after they have disconnected. Screen creates terminal sessions that exist separately from the current session, and are thus impervious to flaws in SSH connections or local terminal emulators. Furthermore, a single screen session can host any number of shell sessions or "windows". Thus, screen may be deployed for range of tasks that include maintaining persistent IRC sessions and efficient multitasking in a terminal environment.
Screen can be used on any Unix-like system, on your Linux VPS, or Mac running OS X. Before beginning with this document, we recommend that you follow our getting started guide. Furthermore, if you are unfamiliar with operating in a terminal environment, you may want to consider our guide on using the terminal.
Contents
Issue the following commands for Debian and Ubuntu systems to ensure that your system is up to date and to install screen:
apt-get update apt-get upgrade apt-get install screen
For CentOS and Fedora systems, use the following commands to update the system and install screen:
yum update yum install screen
For Arch Linux systems, issue the following commands to update the system's package database and then install screen:
pacman -Sy pacman -S screen
For Gentoo systems, send the following commands to update portage tree and install screen:
emerge --sync emerge screen
Screen is installed by default on Mac OS X systems, and can be used without making any further preparations. Once installed, start screen with the screen command at a prompt.
To run applications within a screen session, start screen with the screen command at a terminal prompt. This will leave you at a prompt running within the screen session. Next, issue the command of the application you want to run such as irssi or mutt. You can continue to use the application or the shell environment normally. If you want to detach or end the current session without affecting the process running in the screen session, send the control sequence "C-a d" (CTRL+a+d) and you will return to your initial prompt before you started the screen session. However, the session and the applications running within it continue to run on your system. You may reattach to this session at any point by issuing the command "screen -r" at a system prompt.
When you execute the command "screen -r", screen will automatically reattach to the obvious last "detached" screen session. Since it is possible to run more than one screen session and have more than one detached screen session on a system, screen display a list of the current screen sessions if it cannot determine which screen to reattach to. To attach to a session in this case, specify the process id, or "pid", of the detached screen session. You can determine the "pid" of the screen session by issuing the "screen -ls" command.
If the screen session you want to attach appears to be attached, screen provides a number of command line arguments for invoking screen in a number of situations. Consider the following options for creating and attaching to screen sessions.
Once you've started screen or have attached to an existing session, all interactions with the screen session occur by way of control sequences beginning with Control plus the letter "A", or C-a. C-a is then followed by another command sequence to perform a number of possible options. Consider the following sequences, which represent a mere subset of basic screen functionality:
When connecting to a screen session on a remote machine, it might be more effective to imitate the screen command directly from the ssh command. Consider the following:
ssh -t squire@example.com screen -r
In this case, a pseudo-tty ssh (with the -t argument) connection to the server "example.com" with the user "squire" is established and the "screen -r" command is sent to the remote server. Modify this command to suit the needs of your situation, using your user name, hostname, and alternate screen session. For instance you may choose to use "screen -ADRR" if you suspect that the session might remain attached to another terminal.
In some situations, terminal applications including less and vim might not respond normally inside of the screen session. In many cases this is caused by the value of the "TERM" variable. If you experience this issue, issue the following command at the prompt within the screen session:
TERM=screen
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.
Last edited by Amanda Folson on Tuesday, May 17th, 2011 (r2055).
