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.
Installing GNU Screen
Issue the following commands for Debian and Ubuntu systems to ensure that your system is up to date and to install screen:
apt-get install screen
For CentOS and Fedora systems, use the following commands to update the system and install screen:
yum install screen
For Arch Linux systems, issue the following commands to update the system’s package database and then install screen:
pacman -S screen
For Gentoo systems, send the following commands to update portage tree and install 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.
Basic Screen Interaction
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.
Managing Screen Attachment
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.
“screen -dr” detaches a running screen from its current session and reattaches the session on the local machine.
“screen -x” attaches to a running screen session without detaching it from its current attachment. This is useful if you and another user both need to access the same session concurrently.
“screen -DRR” detaches a running session from its current attachment and reattaches using whatever means necessary. This is helpful in cases where the -dr options are unsuccessful in gaining access to a screen session.
“screen -A” forces screen to resize all of its windows to the current window when it attaches.
“screen -X [command]” starts a screen session but rather than loading a shell, loads an arbitrary command. If you create additional screen “windows” they will run this command as well.
Manipulating 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:
“C-a a” sends a literal “C-a” to program or shell running in a screen window.
“C-a c” creates a new screen window. Screen windows are numbered beginning with 0, and run the default shell process for your system unless you imitate screen with the “screen -X [command]” option.
“C-a “” displays a list of all the currently active screen windows, including the window’s title and identifier
“C-a 0” through “C-a 9” switch the focus to the number designated.
“C-a x” switches focus to the previous window that had focus. Use this to toggle between two active screen windows.
“C-a n” and “C-a p” switches the focus to the next and previous windows respectively.
“C-a k” “kills” or terminates the current screen window if the process running within it cannot be exited through other means.
“C-a A” will create a prompt that allows you to change the name of the current screen window.
“C-a d” will detach the current session from the screen session.
“C-a ?” displays a help screen that provides an overview of a larger subset of screen functions.
Advanced Screen Usage
Using Screen with SSH
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 email@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.
Screen Terminal Emulation
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:
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.
GNU Screen Home Page
GNU Screen Manual