Blog / Linux/ A Complete Guide to the Linux Screen Command: Session Management, Background Tasks, and Remote Sharing

A Complete Guide to the Linux Screen Command: Session Management, Background Tasks, and Remote Sharing

What is the screen command?

Screen is a full-screen window manager that allows you to create multiple independent virtual terminal sessions within a single physical terminal (like an SSH connection). You can run multiple programs or tasks within a screen session. Even if you disconnect, these programs continue to run in the background, and you can later reconnect to resume your work interface.

How to install screen

Most Linux distributions come with screen pre-installed. If not, you can install it using your system's package manager:

  • CentOS/RHEL/Fedora: sudo yum install screen or sudo dnf install screen
  • Debian/Ubuntu: sudo apt-get install screen

Basic screen usage

Follow these steps to manage long-running background tasks, such as compilations or installations (e.g., LNMP).

1. Create a screen session

Run the following command to create a session named example:

screen -S example

You will enter a new full-screen terminal where you can run any command.

2. Detach from a session

To temporarily exit the current session while keeping its programs running, use the key sequence: Ctrl + a, then press d (i.e., press Ctrl and a together, release, then press d).

3. Reattach to a session

To reconnect to an existing session, run:

screen -r example

If you forget the session name, first list all sessions:

screen -ls

The output will show a list like 12345.example. Then use screen -r 12345.example or screen -r example to reattach.

4. Close (terminate) a session

Inside the session, run the exit command or press Ctrl + d. The session will terminate, displaying [screen is terminating].

Remote demonstration and session sharing

Screen supports multiple users viewing and interacting with the same session simultaneously, ideal for remote demos:

  1. The presenter creates a session on the server: screen -S demo
  2. Viewers SSH to the same server and run: screen -x demo
  3. Both parties will see a perfectly synchronized terminal interface.

Common keyboard shortcuts

Within a screen session, all shortcuts use Ctrl + a as the prefix:

  • Ctrl + a c: Create a new window in the current session
  • Ctrl + a w: Show the window list
  • Ctrl + a n: Switch to the next window
  • Ctrl + a p: Switch to the previous window
  • Ctrl + a 0-9: Switch directly to window 0 through 9
  • Ctrl + a d: Detach from the current session

Quick command reference

screen -S <name>      # Create a new session named <name>
screen -ls            # List all sessions
screen -r <name>     # Reattach to a specified session
screen -d <name>     # Detach a session remotely (leave it running)
screen -d -r <name>  # Detach from elsewhere, then reattach

What if you forget the session name?

If you didn't specify a name or forgot it, simply run screen -ls to view all current sessions. The output shows the session ID (e.g., 12345.pts-0.host) and name (if any). Use screen -r 12345 (just the numeric ID) or the full ID to reattach.

If a session is detached but you cannot reattach, its state might be 'Attached'. Try running screen -d <ID or name> to force a detach, then use screen -r to connect.