Detaching and Reattaching
This chapter delivers the one skill the whole guide rests on: leaving a session without ending it, and coming back to it later — from the same terminal, a different terminal, or a different machine — with everything exactly where you left it.
Detaching with the keyboard
Inside a session, press Ctrl-A and then d. Screen prints a line like this real one from transcript t02:
[detached from 23358.live]
and you are back at the ordinary shell
you started screen from. The session
is not paused, closed, or saved. It is
running, on that machine, and the
programs inside it are working away as
if you were still watching.
Everything else in this chapter is a variation on that one move.
Listing what is waiting
screen -ls
From the lab's transcript t01, with one session waiting:
There is a screen on:
23237.work (10/27/26 03:06:33) (Detached)
1 Socket in /run/screen/S-lab.
The number is the server's process id,
the word after the dot is the name you
gave with -S, the date and time are
when the session started, and
(Detached) is the state you want to
see. With several sessions the header
reads "There are screens on:" and each
is listed. A session you are attached
to from another terminal shows
(Attached) — on the lab's build it
shows that even with two clients
attached (transcript t06); chapter 14
explains the (Dead ???) state when
you meet it.
Transcript
screen-lab/transcripts/t01-detach-reattach.txt
runs the detached half of this chapter
non-interactively — a session started,
listed, and printed into from outside
while nobody was attached — and
transcript t02 reattaches for real,
with keystrokes.
Reattaching
screen -r work
-r reattaches. With exactly one
detached session you may omit the name;
with several, name the one you want
(tab completion will offer the full
12345.work form, and that works too).
You land in the window you left, at the
prompt you left, with the output that
arrived while you were gone already
scrolled into place.
Three flags cover the real-world cases:
screen -r work— the plain case: the session is detached, you take it.screen -d -r work— detach it there, reattach here. The session is still attached on your desk machine and you are now at a laptop across the house:-dpolitely detaches the other client first. This is the combination most people end up typing daily, often shortened toscreen -dr work.screen -x work— attach without detaching: two clients, one session, both live. That is multi-display mode, and chapter 9 builds sharing and pair work on it.
Ending a session on purpose
Detach keeps; exit ends. To close a session deliberately, reattach, exit every window's shell (or press Ctrl-A k in each window and confirm, which kills that window), and the last window's death ends the session. From outside, the same result:
screen -S work -X quit
The lab uses that line to clean up after
itself in transcript t01. screen -ls
afterwards reports one fewer socket,
and when none are left:
No Sockets found in /run/screen/S-lab.
A dead session holds no grudge and keeps no copy: its windows, their scrollback, and any program still running in them are gone. Chapter 13 states what that means for work you care about — detach is reversible, quit is not.
Check
Start a session, start something slow
inside it (the lab uses a small loop
that prints a line every few seconds),
detach with Ctrl-A d, and run
screen -ls. The session shows
(Detached). Wait a minute, reattach
with screen -r, and the slow thing
has kept printing the whole time you
were away — timestamps in the lab's
hardcopy capture, transcript t01, show
the lines arriving during the detached
minutes. Nothing was paused. That
continuity is the entire point of the
tool, demonstrated in under five
minutes.