How Do I Detach a Screen Session From a Terminal?

A screen session in Linux refers to a virtual terminal that allows users to run multiple terminal sessions within a single window. It allows creating, managing, and switching between multiple screens, each running its processes or applications. Screen sessions are particularly useful for remote or long-running processes as they continue running even after the user disconnects, ensuring uninterrupted execution.

Detaching a screen session means disconnecting from it while leaving it running in the background. This feature is crucial in Linux terminal usage. It allows users to start a process, detach from the session, and later reattach it from any location, ensuring process continuity and convenience.

This post will address the possible methods to detach a screen session in linux from the terminal. The commands will be practiced on Ubuntu 22.04.

Prerequisites: Install screen Command

Before moving further, you must ensure the screen command is installed on your Linux system. If not, use the commands below to install it on the respective Linux distribution:

$ sudo apt install screen    #Debian/Ubuntu-Based
$ sudo yum install screen  #CentOS/RHEL-Based
$ sudo dnf install screen    #Fedora-Based
$ sudo pacman -S screen   #Arch-Based

Method 1: Detach a Screen Session Using the screen Command

The screen command in Linux can be used to create and manage terminal sessions. The screen command lets you detach from a screen session, allowing it to continue running in the background.

The syntax of the screen command to detach a screen session is as follows:

$ screen -d <session-id>

If you look at the syntax, the session id is required to detach the screen session. Let’s practice it through an example:

Step 1: List the screen Sessions

First, list the possible screen session attached right now:

$ screen -ls

detach screen session |

Step 2: Terminate the Session

Use the session id of the screen session in the screen command to detach that session from a terminal. In our case, the session id is 3302, as shown in the previous step:

$ screen -d 3302

detach screen session |

The output shows the session has been detached, and the original terminal will be back. Further, you can verify whether the session has been detached using the “screen -ls” command.

Note: If you want to reattach the session, use the command “screen -r <session-id>.

Method 2: Detach a Screen Session Using the Keyboard Shortcut

Interestingly, the keyboard shortcut is another way to detach the screen session. First, you must open the terminal to which the screen session is attached. Then, use the shortcut key “CTRL+A+D” to detach that session. Let’s do it practically. This shortcut will terminate the first encountered screen session.

We have opened up a terminal where the screen session is attached. Upon using the shortcut key “CTRL+A+D,” the screen session detaches instantly with the following prompt on the terminal screen:

detach screen session |

The output shows that the session having the id=3411 has been detached. To verify it, you can use the following command:

$ screen -ls

detach screen session |

Wrap Up

The screen sessions allow you to run multiple processes/applications on various sessions. This process is carried out using the screen command. When there are multiple sessions, and you want to detach a few from the terminal, you can use the screen command and the shortcut key “CTRL+A+D.”

The screen command only terminates the session whose session ID is provided, whereas the shortcut key detaches the first encountered screen session. Further, users can use the “scree -r <session-id>” command if they want to reattach any session.

You have learned the possible methods and their steps to detach a screen session from a terminal. For more tips and tricks on Linux, keep visiting Linux Genie and subscribe to the newsletter for the latest posts.

Print Friendly, PDF & Email