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.
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 # reattachThe 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
sudo apt install -y screen
screen -vsudo dnf install -y epel-release
sudo dnf install -y screenScreen version 4.09.00 (GNU) 30-Jan-22On 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
| Command | What it does |
|---|---|
screen -S name | Start a new session with a name |
screen -ls | List your sessions (screen -list is the same) |
screen -r name | Reattach a detached session |
screen -r 48213 | Reattach by PID, when names are ambiguous |
screen -d -r name | Detach it from wherever it is attached, then attach here |
screen -x name | Attach without detaching the other side: both terminals see the same session |
screen -dmS name cmd | Start a session already detached, running cmd (for scripts and cron) |
screen -S name -X quit | End a session and everything running in it |
screen -wipe | Remove 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:
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:
screen -ls minecraft > /dev/null || screen -dmS minecraft /opt/minecraft/start.shWhen screen -r refuses: the usual messages
$ 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.
| Message | Cause | Fix |
|---|---|---|
No Sockets found in /run/screen/S-deploy. | No session of this user matches: it has ended, or you are logged in as someone else | Check 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 -r | Use 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 name | screen does not guess which one you mean, so it attaches none | Name 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 with | Log in over SSH as that user directly |
Key bindings cheat sheet
| Keys (after Ctrl-a) | Action |
|---|---|
| d | Detach: leave the session running and return to your shell |
| c | New window with a fresh shell |
| n / p | Next / previous window |
| 0 to 9 | Jump to a window by number |
| " | Pick a window from a list |
| A | Rename the current window |
| k | Kill the current window |
| S | Split into two regions, one above the other |
| | | Split into two regions side by side |
| Tab | Move to the next region |
| X | Close the current region (the window in it keeps running) |
| Q | Close every region except the current one |
| [ or Esc | Copy mode: scroll back and select text |
| ] | Paste what you copied |
| H | Start or stop logging the current window to a file |
| a | Send 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
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:
| Key | Action |
|---|---|
| Arrow keys, or h j k l | Move the cursor |
| Ctrl-b / Ctrl-f | One page up / down |
| Ctrl-u / Ctrl-d | Half a page up / down |
| g / G | Top / bottom of the scrollback |
| / and ? | Search forward / backward; n and N repeat |
| Space | Set the first mark, move, Space again to copy |
| Esc | Leave 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:
screen -S upgrade -X hardcopy -h ~/upgrade-history.txt # -h includes the scrollbackChecked: with defscrollback 10000, that file held all 300 lines of a seq 1 300 run in the window.
Logging a session to a file
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 windowThree 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
-Lfrom the same directory both appended to the samescreenlog.0, so name the file per session. - The log lags. screen writes it every 10 seconds by default.
logfile flush 1in~/.screenrcbrought 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:
sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' ~/upgrade.log | tr -d '\r' > ~/upgrade.txtEnding a session
- From inside: type
exitin 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 runscreen -wipeto clear the leftover socket. How to kill a process covers which signal to try first.
A small ~/.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 qsends Ctrl-q (XON) and everything you typed appears. - It suddenly asks for a password.
Ctrl-a xlocks 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 apasses one through to bash. - screen inside screen. Over SSH from one session into a server that runs another, every
Ctrl-agoes to the outer one.Ctrl-a a ddetaches the inner session.
screen vs tmux
| screen | tmux | |
|---|---|---|
| Prefix key | Ctrl-a | Ctrl-b |
| Start a named session | screen -S name | tmux new -s name |
| Detach | Ctrl-a d | Ctrl-b d |
| List sessions | screen -ls | tmux ls |
| Reattach | screen -r name | tmux attach -t name |
| Scroll back | Ctrl-a [ | Ctrl-b [ |
| Default scrollback | 100 lines (1024 with Debian's /etc/screenrc) | 2000 lines |
| Splits | Regions that show windows | Panes, each with its own shell |
| Serial consoles | screen /dev/ttyUSB0 115200 | Not built in |
| Rocky, AlmaLinux | EPEL only | Base 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.