Guides

screen command in Linux: sessions, detach, reattach and scrollback

The screen command in Linux keeps a session running after SSH disconnects: screen -S, Ctrl-a d, screen -ls, screen -r, windows, scrollback and logging.

Published ~8 min read
On this page

Short answer: screen -S name starts a named terminal session that keeps running on the server after you disconnect. Press Ctrl-a d to detach from it, run screen -ls to list your sessions and screen -r name to get back in. Every screen key binding is Ctrl-a, pressed and released, followed by one more key; the two are never held down together.

any Linux
screen -S upgrade      # start a session called "upgrade"
# ... start something long, then press Ctrl-a d to detach ...
screen -ls             # list sessions
screen -r upgrade      # reattach

The commands below were checked on Debian 12 with GNU Screen 4.09.00, the version in Debian’s package. Outputs are copied from those runs, with PIDs and names changed and the socket directory shown as Debian’s default (/run/screen/S-deploy for a user called deploy); the key bindings are from the screen man page.

Install screen

Debian · Ubuntu
sudo apt install -y screen
screen -v
Rocky · AlmaLinux
sudo dnf install -y epel-release
sudo dnf install -y screen
Example output of screen -v on Debian 12
Screen version 4.09.00 (GNU) 30-Jan-22

On Rocky Linux and AlmaLinux 8 and 9, screen is no longer in the base repositories, only in EPEL. tmux is in the base repositories there, which is one reason many people on those systems never switch back.

Start, detach, list and reattach

CommandWhat it does
screen -S nameStart a new session with a name
screen -lsList your sessions (screen -list is the same)
screen -r nameReattach a detached session
screen -r 48213Reattach by PID, when names are ambiguous
screen -d -r nameDetach it from wherever it is attached, then attach here
screen -x nameAttach without detaching the other side: both terminals see the same session
screen -dmS name cmdStart a session already detached, running cmd (for scripts and cron)
screen -S name -X quitEnd a session and everything running in it
screen -wipeRemove sessions listed as Dead

Inside a session, Ctrl-a d detaches: you are back at your normal shell and everything in the session keeps running. Closing the terminal window or losing the SSH connection detaches you the same way, which is the reason to use screen at all. Here is what the list looks like:

Example output of screen -ls
There are screens on:
	48213.upgrade	(10/21/26 09:14:02)	(Detached)
	47790.minecraft	(10/20/26 18:30:41)	(Attached)
2 Sockets in /run/screen/S-deploy.

Each line is PID.name, the start time and the state. Detached means the session is waiting for you; Attached means a terminal is connected to it right now, possibly one you have long since closed (see the next section). Sessions belong to the user who started them, so root and your normal account see different lists.

screen -ls name exits with 0 when a matching session exists and 1 when it prints No Sockets found (checked on 4.09), which makes a start-once line easy:

bash
screen -ls minecraft > /dev/null || screen -dmS minecraft /opt/minecraft/start.sh

When screen -r refuses: the usual messages

Example output
$ screen -r upgrade
There is a screen on:
	48213.upgrade	(10/21/26 09:14:02)	(Attached)
There is no screen to be resumed matching upgrade.

The session is still attached to another terminal, very often an SSH connection that died without the server noticing yet (the SSH broken pipe guide explains why that happens). screen -d -r upgrade detaches the old connection and attaches yours. screen -x upgrade joins it without detaching the other side, which also works for two people looking at the same terminal.

MessageCauseFix
No Sockets found in /run/screen/S-deploy.No session of this user matches: it has ended, or you are logged in as someone elseCheck whoami; sessions are per user
Must be connected to a terminal.screen was started without a terminal: from cron, a script, or ssh host screen -rUse screen -dmS name cmd to start detached, or ssh -t host screen -r name
(Dead ???)The session's process is gone but its socket file was left behind; screen -ls adds: Remove dead screens with 'screen -wipe'.screen -wipe
screen -r with several sessions and no namescreen does not guess which one you mean, so it attaches noneName it, or use the PID from screen -ls
Cannot open your terminal '/dev/pts/0' - please check.You switched user with su; the terminal still belongs to the account you logged in withLog in over SSH as that user directly

Key bindings cheat sheet

Keys (after Ctrl-a)Action
dDetach: leave the session running and return to your shell
cNew window with a fresh shell
n / pNext / previous window
0 to 9Jump to a window by number
"Pick a window from a list
ARename the current window
kKill the current window
SSplit into two regions, one above the other
|Split into two regions side by side
TabMove to the next region
XClose the current region (the window in it keeps running)
QClose every region except the current one
[ or EscCopy mode: scroll back and select text
]Paste what you copied
HStart or stop logging the current window to a file
aSend a real Ctrl-a to the program (start of line in bash)
?Show every key binding
\Kill all windows and end the session

Windows and split regions

A session holds windows, numbered from 0, each with its own shell. Run the long job in window 0, open a second with Ctrl-a c for htop or a log, and switch with Ctrl-a n and Ctrl-a p, all over one SSH connection. Name them with Ctrl-a A so the list under Ctrl-a " means something.

Splitting works differently from tmux. Ctrl-a S or Ctrl-a | divides the display into regions, and the new region starts empty: press Ctrl-a Tab to move into it, then Ctrl-a c for a new window or Ctrl-a n to show an existing one there. Regions are a view, not a container: closing one with Ctrl-a X leaves its window running, and Ctrl-a Q goes back to a single region without ending anything.

Typing into a session from outside

any Linux
screen -S minecraft -p 0 -X stuff 'say Restart in 5 minutes\n'
screen -S minecraft -p 0 -X hardcopy /tmp/console.txt     # save what window 0 shows

-X stuff types the text into window 0 (-p 0) of a detached session as if you had typed it, and \n presses Enter. hardcopy writes what the window currently shows to a file. Checked on 4.09: the command ran in the detached window and the hardcopy contained its output. Many older game-server scripts drive a console this way; the Minecraft server guide uses RCON and a systemd unit instead, which also restarts the server after a crash.

Scrolling back: copy mode

Your mouse wheel and your terminal’s scrollbar move through your own terminal’s history, not the session’s, so they do not show what scrolled past inside screen. screen keeps its own scrollback for every window. Ctrl-a [ (or Ctrl-a Esc) enters copy mode:

KeyAction
Arrow keys, or h j k lMove the cursor
Ctrl-b / Ctrl-fOne page up / down
Ctrl-u / Ctrl-dHalf a page up / down
g / GTop / bottom of the scrollback
/ and ?Search forward / backward; n and N repeat
SpaceSet the first mark, move, Space again to copy
EscLeave copy mode (any key without a meaning here does)
Ctrl-a ]Paste the copied text, back in normal mode

The upstream default is only 100 lines per window. Debian’s /etc/screenrc raises it to 1024 (checked on Debian 12). For more, put defscrollback 10000 in ~/.screenrc; it applies to windows opened after that. In a window that is already running, Ctrl-a : opens screen’s command line, where scrollback 10000 resizes just that window. To keep the whole history as a file:

any Linux
screen -S upgrade -X hardcopy -h ~/upgrade-history.txt   # -h includes the scrollback

Checked: with defscrollback 10000, that file held all 300 lines of a seq 1 300 run in the window.

Logging a session to a file

any Linux
screen -L -Logfile ~/upgrade.log -S upgrade    # log window 0 from the start
# in a running session, Ctrl-a H turns logging on or off for the current window

Three things the man page mentions only in passing, all seen in the 4.09 runs:

  • Without -Logfile the file is screenlog.0 (screenlog.1 for window 1, and so on) in the directory you started screen from. Two sessions started with -L from the same directory both appended to the same screenlog.0, so name the file per session.
  • The log lags. screen writes it every 10 seconds by default. logfile flush 1 in ~/.screenrc brought that down to about a second.
  • It is raw terminal output: carriage returns, colour codes and the shell’s control sequences around every prompt. Read it with less -R, or strip it into plain text:
any Linux
sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' ~/upgrade.log | tr -d '\r' > ~/upgrade.txt

Ending a session

  • From inside: type exit in each window. When the last window closes, screen prints [screen is terminating] and the session is gone. Ctrl-a \ ends all windows at once.
  • From outside: screen -S name -X quit.
  • A session that no longer responds: kill its PID, the number before the dot in screen -ls, then run screen -wipe to clear the leftover socket. How to kill a process covers which signal to try first.

A small ~/.screenrc

~/.screenrc
# No splash screen on start
startup_message off

# Scrollback per window (upstream default 100, Debian's /etc/screenrc 1024)
defscrollback 10000

# Write log files every second instead of every 10
logfile flush 1

# Last line: host name, window list, date and time
hardstatus alwayslastline "%{= kw}[%H] %-w%{= kG}%n %t%{-}%+w %=%Y-%m-%d %c"

This file was loaded with screen -c on 4.09 without errors. It applies to sessions started after you save it; an attached session can reread it with Ctrl-a : and source ~/.screenrc.

Keys that catch people out

  • The window froze and typing shows nothing. You probably hit Ctrl-a s, which sends Ctrl-s (XOFF) to the window and pauses its output. Ctrl-a q sends Ctrl-q (XON) and everything you typed appears.
  • It suddenly asks for a password. Ctrl-a x locks the terminal; enter your account password to unlock it.
  • Ctrl-a no longer jumps to the start of the line. screen takes it; Ctrl-a a passes one through to bash.
  • screen inside screen. Over SSH from one session into a server that runs another, every Ctrl-a goes to the outer one. Ctrl-a a d detaches the inner session.

screen vs tmux

screentmux
Prefix keyCtrl-aCtrl-b
Start a named sessionscreen -S nametmux new -s name
DetachCtrl-a dCtrl-b d
List sessionsscreen -lstmux ls
Reattachscreen -r nametmux attach -t name
Scroll backCtrl-a [Ctrl-b [
Default scrollback100 lines (1024 with Debian's /etc/screenrc)2000 lines
SplitsRegions that show windowsPanes, each with its own shell
Serial consolesscreen /dev/ttyUSB0 115200Not built in
Rocky, AlmaLinuxEPEL onlyBase repositories

For the core job, a terminal that survives a disconnect, they are equal. If screen is already in your fingers, keep it. For a new setup most people pick tmux, and the tmux cheat sheet covers it key by key.

Frequently asked

How do I detach from a screen session?

Press Ctrl-a, release it, then press d. The session and everything running in it keep going on the server and you are back at your normal shell. Closing the terminal or losing the SSH connection detaches it the same way.

How do I reattach to a screen session?

Run screen -ls to list your sessions, then screen -r NAME, or screen -r PID if the names are ambiguous. If screen says there is no screen to be resumed because the session is still Attached, run screen -d -r NAME to detach the old connection and attach yours.

How do I scroll up in screen?

Press Ctrl-a and then [ to enter copy mode, move with the arrow keys or Ctrl-b and Ctrl-f for a page at a time, search with /, and press Esc to leave. Upstream screen keeps 100 lines per window and Debian's /etc/screenrc raises that to 1024; put defscrollback 10000 in ~/.screenrc for more.

How do I kill a screen session?

From outside, run screen -S NAME -X quit. From inside, type exit in each window, or press Ctrl-a followed by a backslash to end all windows at once. A session listed as Dead is already gone, and screen -wipe removes its leftover entry.

Related articles

All guides