1. Introduction

It’s not unusual to lose connection when we’re working on a remote server using ssh and screen.  This may leave us with a screen session attached.

In this short tutorial, we’ll talk about how to detach and reattach to another screen. This way, we can regain control of it and continue our work.

2. Forcefully Detach

To regain control of a screen session, we need to detach it first. We’ll be using the parameter -d to achieve this. However, if there’re multiple sessions, we must specify which one to detach.

Let’s run an example listing the current screen sessions using the ls command:

$ screen -ls
There are screens on:
        15259.pts-6.slack  (Attached)
        15293.pts-5.slack  (Attached)

Now, let’s detach the first one by its id:

$ screen -d 15259
[15259.pts-6.slack detached.]

Now we have only one attached screen left, so we can use -d without specifying the session:

$ screen -d
[15293.pts-5.slack detached.]

3. Reattach to a Screen Session

When we want to take control of another screen, we’ll reattach it using the parameter -r. If there is more than one session, we have to specify which session to reattach. Alternatively, we can use -rr to reattach to the first available screen.

Let’s reattach to one of the detached session from the previous example:

$ screen -r 15293

We can only reattach to a detached screen. But instead of running screen twice, we can use both parameters -d and -r at the same time.

Then, to recover our previous screen session in one step, we only need to run screen -d -r:

$ screen -d -r 15293

4. Conclusion

In this article, we saw how to detach and reattach to a screen session. We first detach using -d and then -r to reattach, or simply using both parameters together, -d -r.

Inline Feedbacks
View all comments