Home > Environment & Utility > screen

screen: Managing Multiple Terminal Sessions

GNU Screen is a terminal multiplexer that allows you to manage multiple independent shell sessions within a single terminal. It offers powerful features such as maintaining sessions even if network connections drop, performing multiple tasks concurrently, and sharing sessions with other users. It is particularly useful for keeping long-running tasks running safely in the background on servers.

Overview

screen is a powerful tool that allows you to keep terminal sessions running in the background, create and switch between multiple sessions, and even share sessions. It is especially useful for ensuring that your work is not interrupted if your SSH connection is lost, and it's like using multiple virtual terminals within a single terminal window.

Key Features

  • Detaching and Reattaching Sessions
  • Multiple Window Management
  • Session Sharing
  • Logging
  • Scrollback Buffer

Key Options

This section describes the main options used when executing the screen command and the primary keybindings used within a screen session.

Command Execution Options

In-Session Keybindings (Ctrl+a Prefix)

Generated command:

Try combining the commands.

Description:

`screen` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various usage examples for the screen command.

Start a New Screen Session

screen

The most basic usage, starting a new screen session.

Start a Session with a Name

screen -S my_long_running_task

Assigns a name to the session, making it easier to identify and reattach later.

Detach Session

screen -d <session_name_or_pid>

Press Ctrl+a d within a screen session to detach it and return to the terminal. You can also force detach a running session from outside.

Reattach Session

screen -r

Reattaches to a detached session. If only one session exists, it connects automatically.

Reattach Specific Session by Name

screen -r my_long_running_task

Reattaches to a session with the specified name.

View List of Running Sessions

screen -ls

Checks the list of currently active screen sessions.

Force Terminate Session

screen -X <session_name_or_pid> quit

Forcefully terminates a session that is no longer needed. (Caution: This may interrupt ongoing work)

Installation

GNU Screen is typically included by default in most Linux distributions or can be easily installed via package managers.

Debian/Ubuntu

sudo apt update && sudo apt install screen

Installs using the APT package manager.

CentOS/RHEL/Fedora

sudo yum install screen
# or
sudo dnf install screen

Installs using the YUM or DNF package manager.

Arch Linux

sudo pacman -S screen

Installs using the Pacman package manager.

Tips & Precautions

Useful tips and points to note when using screen.

Utilizing the .screenrc File

You can customize screen's default settings by using the `.screenrc` file in your home directory. For example, you can configure the default shell, scrollback buffer size, status bar, etc.

  • Location: ~/.screenrc
  • Example Configuration: defscrollback 10000 caption always '%{= gk}%H %L=%-w%?%F%fn%W%? %t %{-}%+w %-= %D %m/%d %C%a'

Remember the Ctrl+a Prefix

All internal commands in screen use Ctrl+a as the prefix by default. Remembering this combination is key to using screen effectively. If Ctrl+a conflicts with other programs, you can change the prefix in `.screenrc` (e.g., `escape ^Jj` to change it to Ctrl+j).

Importance of Naming Sessions

By assigning meaningful names to sessions using `screen -S `, you can reduce confusion when managing multiple sessions and easily reattach to specific ones.

Using the Scrollback Buffer

Press Ctrl+a [ to enter scrollback mode, then use the arrow keys or PageUp/PageDown keys to view previous output. Press q to exit scrollback mode.


Same category commands