screen Command in Linux: Syntax, Sessions, Windows & Key Bindings

GNU screen is a terminal multiplexer that keeps shell sessions alive after disconnect, supports multiple windows in one session, and reattaches over SSH when the network drops.

Published

Updated

Read time 9 min read

Reviewed byDeepak Prasad

screen Command in Linux: Syntax, Sessions, Windows & Key Bindings
About GNU screen is a terminal multiplexer that keeps shell sessions alive after disconnect, supports multiple windows in one session, and reattaches over SSH when the network drops.
Tested on Ubuntu 25.04 (Plucky Puffin); GNU screen 4.09.01; kernel 7.0.0-27-generic
Package screen
Man page screen(1)
Privilege none
Distros

Most Linux and BSD systems with the screen package installed.

Modern alternative: tmux (active development, scriptable layouts).

Related guide

screen — quick reference

Default command prefix: Ctrl+a (press and release, then the binding key). Inside screen, Ctrl+a ? lists all bindings.

Client startup

Flags passed when you launch screen from the shell.

When to use Command
Start a new interactive screen session screen
Start a named session (easier to reattach) screen -S session_name
Start detached in the background (daemon) screen -dmS session_name
Run a command in a new detached session screen -dmS session_name command
Print installed screen version screen -v
List sessions for this user screen -ls
screen -list
List sessions matching a name pattern screen -ls match
Reattach to a detached session by name or PID screen -r session_name
screen -r 12345
Reattach or create if none exists screen -R
Force new session even if $STY is set screen -m
Attach to a session that is already attached elsewhere screen -x session_name
Detach a session running elsewhere (by name or PID) screen -d session_name
Detach elsewhere and reattach here screen -dr session_name
Detach remote and log out (power detach) screen -D session_name
Do whatever is needed to recover a session screen -D -RR
Remove dead session sockets screen -wipe
Read an alternate config instead of ~/.screenrc screen -c /path/to/screenrc
Set scrollback buffer size (lines) screen -h 5000
Override shell for new windows screen -s /bin/bash
Set default window title screen -t title
Set $TERM for windows screen -T screen-256color
Enable UTF-8 mode screen -U
Resolve hostnames to IPv4 only in -r screen -4
Resolve hostnames to IPv6 only in -r screen -6
Quiet startup (non-zero exit if attach fails) screen -q
Send screen commands to stdout (scripting) screen -Q
Turn on session logging screen -L
Set log file path screen -Logfile /path/screen.log
Change command prefix characters (example: Ctrl+b) screen -e^Bb
Flow control on / off / auto screen -f
screen -fn
screen -fa
Interrupt output sooner when flow control on screen -i
Preselect a window by name or number if it exists screen -p 0
Optimal output vs strict vt100 screen -O
Adapt all windows to new terminal size on attach screen -A -r session_name
Force all capabilities into each window termcap screen -a
Run a command in the current session (scripting) screen -S name -X stuff 'command\n'

Session commands (prefix Ctrl+a)

Run these after Ctrl+a inside an attached session.

When to use Keys
Show key binding help ?
Detach from session (keeps processes running) d
Detach all and log out (power detach) D
Create a new window with the current shell c
Close current window (confirm) K
Switch to next window n
Switch to previous window p
Cycle through windows Space
Switch to window 0–9 09
Show window list and choose w
Rename current window A
Lock session (password prompt) x
Split horizontally (region) S
Split vertically |
Move focus to next region Tab
Close current region X
Enter copy/scrollback mode [
Write hardcopy of window to file Ctrl+a H (capital H toggles log; see -L)

Remote attach syntax

When to use Command
Reattach to screen on another host screen -r [email protected]

screen — command syntax

Synopsis from screen -h on Ubuntu 25.04 (GNU screen 4.09.01):

text
screen [-opts] [cmd [args]]
or: screen -r [host.tty]

screen stores session sockets under /run/screen/S-$USER/ (or legacy /var/run/screen). It does not edit system account files. Install the package if the binary is missing: sudo apt install screen.


screen — command examples

Essential Start a named detached session

Long jobs survive SSH drops when they run inside a detached screen you can reattach later.

Run the command:

bash
screen -dmS backup-job bash -c 'sleep 300; echo done'
screen -ls

Sample output:

text
There is a screen on:
	45516.backup-job	(07/01/2026 02:43:00 PM)	(Detached)
1 Socket in /run/screen/S-root.

(Detached) means the session runs in the background. Reattach with screen -r backup-job when you need the shell again. Kill test sessions with screen -S backup-job -X quit.

Essential List sessions and reattach by name

After logging back in over SSH, find your session and attach.

List sessions:

bash
screen -ls

Sample output:

text
There are screens on:
	45516.backup-job	(07/01/2026 02:43:00 PM)	(Detached)
	44930.demo	(07/01/2026 02:40:48 PM)	(Detached)
2 Sockets in /run/screen/S-root.

Reattach to one session:

bash
screen -r backup-job

Your shell prompt returns inside screen. Detach without stopping work: press Ctrl+a, then d.

Common Detach a session from another login

If you left screen attached on another terminal, detach it remotely by name.

Run the command:

bash
screen -d backup-job
screen -ls

Sample output:

text
There is a screen on:
	45516.backup-job	(07/01/2026 02:43:00 PM)	(Detached)
1 Socket in /run/screen/S-root.

screen -dr backup-job detaches elsewhere and attaches on the current terminal in one step.

Common Multiple windows in one session

Run parallel tasks in one SSH connection — database tail in one window, deploy script in another.

Inside an attached session:

  1. Press Ctrl+a, then c to open a new window.
  2. Press Ctrl+a, then n or p to move next or previous.
  3. Press Ctrl+a, then w to list windows.

Sample window list (after Ctrl+a w):

text
0$ bash  1-$ bash  2$ top

The $ marker shows the active window. Number keys Ctrl+a 09 jump directly.

Common Lock a screen session

Step away from a shared server without closing shells — screen prompts for your Linux password.

Inside the session, press Ctrl+a, then x.

Sample output:

text
Screen used by root <server1> on server1.
Password:

Enter your user password to unlock. This is screen's lock, not a TTY screensaver.

Common Kill the current window (confirm)

Close only the active window — other windows in the same session keep running.

Inside screen, press Ctrl+a, then K.

Sample prompt:

text
Really kill this window [y/n]

Press y to destroy the window. Ctrl+a K does not end the whole session unless it was the last window.

Advanced Send input to a detached session (-X)

Automation and cron jobs can inject keystrokes into a running screen without attaching.

Run the command:

bash
screen -S backup-job -X stuff 'echo injected\n'
screen -S backup-job -X hardcopy /tmp/screen-hardcopy.txt
tail -3 /tmp/screen-hardcopy.txt
rm /tmp/screen-hardcopy.txt

Sample output:

text
done
root@server1:~# echo injected
injected

stuff sends literal characters; \n acts like Enter. hardcopy dumps the visible window buffer for verification.

Advanced Clean up dead session sockets

After a crash or kill -9 on screen, stale sockets can clutter screen -ls. See kill and pkill when you need SIGKILL versus a normal TERM on the screen parent.

Run the command:

bash
screen -wipe
screen -ls

Sample output when nothing is dead:

text
No Sockets found in /run/screen/S-root.

When screen finds zombies it prints Removed dead lines and cleans /run/screen/.

Advanced Session logging to a file

Keep a transcript of everything printed in a new session.

Run the command:

bash
screen -L -Logfile /tmp/screen-demo.log -dmS logged-session bash -c 'echo hello-log; sleep 5'
sleep 6
cat /tmp/screen-demo.log
screen -S logged-session -X quit
rm /tmp/screen-demo.log

Sample output:

text
hello-log

-L enables logging; -Logfile sets the path. Default log name is screenlog.N in the working directory if you omit -Logfile.


screen — when to use / when not

Use screen when Use something else when
  • You need a long-running shell to survive SSH disconnects on a minimal install
  • The server already uses GNU screen and existing team habits
  • You want a lightweight multiplexer without extra configuration
  • You must detach from one terminal and resume from another on the same host
  • You want panes, plugins, and modern scripting → tmux
  • You only need one short command in the background → nohup or systemd service
  • You manage many hosts → configuration management or CI, not manual screen
  • You need graphical desktop persistence → desktop session tools, not screen

screen vs tmux

GNU screen tmux
Age Classic; widely pre-installed Newer; default on many admin workflows
Prefix key Ctrl+a Ctrl+b (configurable)
Panes Regions (basic splits) First-class panes and layouts
Scripting -X stuff and screen -Q tmux send-keys, capture-pane
Config file ~/.screenrc ~/.tmux.conf
Active development Maintenance mode Frequent feature releases

See the tmux for the modern multiplexer.


Tools for persistent shells, remote access, and file transfer.

Command One line
screen GNU terminal multiplexer (this page)
ssh Remote login (often paired with screen)
scp Copy files over SSH

Browse the full index in our Linux commands reference.


screen — interview corner

What is the screen command in Linux?

GNU screen is a terminal multiplexer. One physical terminal (or SSH session) can host multiple shell windows, and the whole session keeps running when you detach.

That matters on remote servers: start a compile or backup, detach with Ctrl+a d, close your laptop, SSH back later, run screen -r, and the job is still there.

A strong answer is:

"screen multiplexes terminals — multiple windows, detach and reattach, sessions survive SSH drops. I use screen -S name -dm to start background work and screen -r to resume."

What is the difference between detaching and closing SSH?

Detach (Ctrl+a d or screen -d) leaves the screen server and child processes running. Only your view disconnects.

Closing SSH without screen sends SIGHUP to the shell and usually kills child processes. screen catches that when you are attached; detached sessions are already safe.

A strong answer is:

"Detaching leaves screen and its programs running in the background. Closing SSH kills the login shell unless work runs inside a detached screen or nohup."

What is the default screen command prefix key?

The default prefix is Ctrl+a. Press and release it, then press the command key (for example c for a new window, d to detach).

Change it at startup with screen -e^Xx or in ~/.screenrc with escape ^Bb (example: move prefix to Ctrl+b).

A strong answer is:

"Default prefix is Ctrl+a — chord it before commands like c for new window, d for detach, or ? for help."

When would you pick screen over tmux?

Choose screen when the host already standardizes on it, the package is pre-installed on minimal images, or you only need basic detach/reattach without pane layouts.

Choose tmux for pane tiling, larger community configs, and richer scripting.

A strong answer is:

"screen is lighter and everywhere on legacy systems; tmux wins for panes, plugins, and modern admin ergonomics. I match whatever the team already documents."

How do you list and reattach screen sessions?
bash
screen -ls
screen -r session_name

-ls shows PID.name tags and (Attached) or (Detached). -r attaches by name or PID; use -dr if another terminal still shows attached.

A strong answer is:

"screen -ls lists sockets; screen -r name reattaches. If it's attached elsewhere I use screen -dr to steal the session to my terminal."


Troubleshooting

Symptom Likely cause Fix
screen: command not found Package not installed sudo apt install screen
There is no screen to be resumed Wrong name or session ended screen -ls; start with screen -S name
Attached but you need the shell Session open on another TTY screen -dr name
Dead sessions in -ls Unclean exit screen -wipe
Garbled terminal after attach Size or term mismatch screen -A -r name; set screen -T screen-256color
Cannot open your terminal /dev/pts/... Permissions on TTY Log in again; avoid attaching from broken su chains

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.