📒 

screen is a powerful terminal multiplexer that allows you to create, manage, and resume multiple shell sessions from a single terminal window. This is particularly useful when working on remote servers via SSH or when you need to run long-running processes that you want to keep running even after disconnecting from the session. In this guide, we will cover the basics of using screen, including how to start, detach, and reattach sessions.

Installing screen

Before using screen, ensure it is installed on your system. On most Linux distributions, you can install it using the package manager:

For Debian/Ubuntu:

sudo apt-get install screen

For CentOS/RHEL:

sudo yum install screen

For Fedora:

sudo dnf install screen

For macOS (with Homebrew):

brew install screen

Starting a screen Session

To start a new screen session, simply type:

screen

This will open a new screen session, and you will be presented with a standard shell prompt.

Naming a screen Session

To make it easier to identify your screen sessions, you can name them:

screen -S session_name

Replace session_name with a descriptive name for your session. This makes it easier to manage multiple sessions.

Example:

screen -S mysession

Detaching from a screen Session

To detach from a screen session without terminating it, press the following key combination:

Ctrl + A, then D
  • Ctrl + A: This signals screen that you want to send a command.
  • D: This is the command to detach the session.

After detaching, you’ll be back at the terminal prompt, but the screen session will continue running in the background.

Listing screen Sessions

If you have multiple screen sessions running, you can list them with:

screen -ls

This command will display all active screen sessions along with their session IDs and names.

Example output:

There are screens on:
12345.mysession (Detached)
67890.another_session (Detached)
2 Sockets in /var/run/screen/S-user.

Reattaching to a screen Session

To reattach to a screen session, use the -r option followed by the session ID or name:

Using the Session ID:

screen -r 12345

Using the Session Name:

screen -r mysession

If you only have one detached session, you can simply run:

screen -r

Attaching to a screen Session that is Already Attached

If a screen session is still attached and you want to forcefully reattach it (e.g., if you lost connection), use the -d -r options:

screen -d -r session_name_or_id
  • -d: Detach the session if it is already attached.
  • -r: Reattach to the session.

This command detaches the session from the previous terminal and reattaches it to the current one.

Creating a New Window in a screen Session

Once inside a screen session, you can create new windows (virtual terminals) with:

Ctrl + A, then C

Each new window is like a new terminal inside the screen session. You can switch between them, which can be useful for multitasking.

Switching Between Windows

To switch between windows inside a screen session:

  • To go to the next window:
    Ctrl + A, then N
  • To go to the previous window:
    Ctrl + A, then P
  • To list all windows:
    Ctrl + A, then “

    This will display a list of all open windows, allowing you to select one.

Closing a screen Session

To close a screen session, simply exit from all running processes or type exit in each window until all windows are closed. When the last window is closed, the screen session will terminate.

Summary of Common screen Commands

Conclusion

screen is a versatile tool for managing long-running processes and maintaining persistent console sessions on remote servers. With screen, you can detach from sessions, keep processes running in the background, and reattach later from anywhere, making it an invaluable tool for system administrators and developers. By mastering the basic commands for creating, managing, and navigating screen sessions, you can greatly enhance your productivity in terminal-based environments.