How to Use the screen Command to Keep Terminal Sessions Alive on Linux

The Problem with Remote Terminal Sessions

When you connect to a remote Linux server via SSH (Secure Shell) to perform administrative tasks, update software, or run a lengthy backup script, your session is entirely dependent on your local network connection. If your Wi-Fi drops, your laptop goes to sleep, or your VPN disconnects, the SSH session is terminated. Consequently, any process running in that terminal is abruptly killed.

This behaviour can be disastrous if you are in the middle of a critical database migration or compiling software. To solve this, Linux administrators rely on terminal multiplexers. The most ubiquitous and reliable of these is the screen command. screen creates a virtual terminal session on the server that remains active even if you disconnect, allowing you to reattach and resume your work later.

How to Install the Screen Utility

While screen is installed by default on many Linux distributions, it is occasionally missing on minimal server installations. To install it on Ubuntu or Debian-based systems, run:

sudo apt update
sudo apt install screen

For CentOS, RHEL, or Fedora systems, use:

sudo dnf install screen

How to Start and Detach a Screen Session

Using screen is incredibly straightforward.

Step 1: Start a New Session

To launch a new virtual terminal, simply type:

screen

Press Enter to clear the introductory message. You are now operating inside a screen session. It looks exactly like a normal terminal, but it is now protected from disconnects.

Pro Tip: It is highly recommended to name your sessions, especially if you plan to run multiple background tasks simultaneously. To start a named session, use the -S flag:

screen -S database_backup

Step 2: Run Your Command

Begin the long-running process you need to execute. For example, a large file transfer using rsync or a system upgrade:

sudo apt upgrade

Step 3: Detach from the Session

While the process is running, you can safely leave the session without killing the command. To detach, you must use a specific keyboard shortcut sequence:

  1. Press and hold Ctrl and tap the A key (Ctrl+A).
  2. Release both keys.
  3. Tap the D key.

You will be returned to your original, unprotected terminal prompt, and you will see a message confirming you have detached from the screen. The process you started is still running safely in the background on the server.

How to Reattach to a Screen Session

Once you are ready to check on the progress of your task, or if you have recently reconnected to the server after an unexpected network drop, you need to bring the background session back to the foreground.

Step 1: List Active Sessions

If you have forgotten the name of your session, or want to see all running screens, type:

screen -ls

This will output a list of active sessions, displaying their process IDs (PIDs) and names (e.g., 14532.database_backup (Detached)).

Step 2: Reattach

To resume your session, use the -r flag followed by the session name or PID:

screen -r database_backup

Your terminal will immediately switch back to the exact state you left it in, allowing you to see the output of your finished or ongoing command.

How to Terminate a Screen Session

When your task is completely finished and you no longer need the virtual terminal, you should close the session to free up system resources.

Do not simply detach from it. Instead, reattach to the session (as shown above), and type:

exit

Alternatively, you can use the keyboard shortcut Ctrl+D. The terminal will display [screen is terminating], confirming that the virtual session has been successfully closed.

Get the best tech tips delivered straight to your inbox.

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