How to Use the Linux screen Command to Multiplex Terminals

When working on a remote Linux server via SSH, you face a significant limitation: if your internet connection drops, or if you accidentally close your terminal window, any commands running in that session are immediately terminated. Furthermore, you are restricted to a single command prompt unless you open multiple independent SSH connections.

The screen command solves both problems. It is a terminal multiplexer that allows you to create multiple virtual terminal windows within a single SSH session. More importantly, these virtual windows run persistently in the background. You can detach from them, close your laptop, reconnect tomorrow, and pick up exactly where you left off.

Starting and Detaching from a Screen Session

To start a new screen session, simply type:

screen

You may see a welcome message; press Space to dismiss it. You will be presented with what looks like a normal command prompt, but you are now inside a virtual screen session.

You can start a long-running process, such as compiling a large software package or running a database migration. Once it is running, you can detach from the session, leaving it running safely in the background.

To detach, press Ctrl+A, release both keys, and then press d.

You will be returned to your original, outer SSH prompt, and you will see a message like [detached from 12345.pts-0.server]. You can now safely close your SSH connection; the process inside the screen session will continue running uninterrupted.

Reattaching to a Screen Session

When you log back into the server later and want to check on your progress, you need to reattach to your running session.

To list all currently running screen sessions, use:

screen -ls

This will output a list of session IDs. If you only have one screen session running, you can reattach to it quickly using the -r (resume) flag:

screen -r

If you have multiple sessions running, you must specify the session ID (the numbers before the dot in the screen -ls output):

screen -r 12345

You will instantly be dropped back into the virtual terminal exactly as you left it.

Multiplexing: Creating Multiple Windows

While inside a screen session, you don’t need to detach to run another command. You can create multiple virtual windows (like tabs in a web browser) within the same session.

All screen commands begin with the prefix Ctrl+A.

  • Create a new window: Press Ctrl+A, then press c (create). A fresh, blank prompt will appear.
  • Switch to the next window: Press Ctrl+A, then press n (next).
  • Switch to the previous window: Press Ctrl+A, then press p (previous).
  • List all windows: Press Ctrl+A, then press (double quote). A menu will appear allowing you to select a specific window using the arrow keys.

This allows you to edit a configuration file in one window, view the system logs in a second window, and restart a service in a third, all without opening multiple SSH connections.

Naming Your Sessions

If you use screen frequently, it can become confusing to remember which session ID corresponds to which task. You can name your sessions when you create them for easier identification.

screen -S database_backup

Now, when you run screen -ls, it will be clearly labelled. To reattach to it, you can use the name instead of the numeric ID:

screen -r database_backup

Terminating a Screen Session

When you are completely finished with a virtual window, you can close it by typing exit at the prompt (or pressing Ctrl+D). If you exit the final window in a screen session, the session will terminate completely, and you will see the message [screen is terminating].

The screen command is an essential survival tool for Linux administration, ensuring that network instability never ruins a critical, long-running task again.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.