Where to start First steps track · 15 stages
Linux Labs

screen — terminal multiplexer

4 min GNU screen

Keeps a terminal session alive on the server after an SSH drop; sessions live in memory and don't survive a reboot.

Cheat sheet

6 commands Printable version
  • screen -S name new named session
  • screen -ls list sessions
  • screen -r name attach to a session
  • screen -d -r name detach from another terminal and attach here
  • Ctrl+a d detach — the session keeps running
  • screen -dmS job cmd start detached right away, for scripts

Overview

screen is like tmux: it keeps a terminal session alive on the server, surviving an SSH drop. Older and simpler; often already installed. If you have a choice, tmux is more convenient, but screen shows up everywhere.

bash
screen                            # new session
screen -S work                    # named
screen -ls                        # list sessions
screen -r work                    # attach
screen -d -r work                 # detach from another terminal and attach here

Options

6

-S gives the session a name instead of a pid number — without it, screen -ls shows hard-to-read identifiers. -d -r solves the common "session busy on another terminal" problem: it detaches it there first, then attaches it here.

Option What it does Example
-S name Name the session instead of using its pid — easier to find later. screen -S work
-ls List running sessions and their state (Attached/Detached). screen -ls
-r name Reattach to a detached session by name. screen -r work
-d Detach the session from its current terminal (combine with -r to steal it). screen -d -r work
-dmS name Start a command directly in a detached session — handy for scripts and cron. screen -dmS job long-running-command
-X command Send a control command to a named session without attaching to it. screen -X -S job quit

Nothing found. Try a shorter query.

Recipes

2

Hotkeys

The prefix is Ctrl+a, then a key:

Ctrl+a  d        detach — the session keeps running
Ctrl+a  c        new window
Ctrl+a  n / p    next / previous window
Ctrl+a  0..9     jump to window by number
Ctrl+a  "        list windows (pick one)
Ctrl+a  A        rename the window
Ctrl+a  S        split the screen horizontally (Tab to switch, Ctrl+a X to close a region)
Ctrl+a  |        split vertically
Ctrl+a  [        scroll/copy mode (Esc to exit)
Ctrl+a  k        kill the window
Ctrl+a  ?        help for the key bindings

Useful

bash
screen -S log -L -Logfile out.log cmd   # write the session's output to a file (logging)
screen -dmS job cmd                       # start detached right away (for scripts)
screen -X -S job quit                     # kill a session by name

~/.screenrc (common settings):

defscrollback 10000      # scrollback length
startup_message off

Troubleshooting

4

screen -r: There is no screen to be resumed

No such session (closed/rebooted). screen -ls shows what exists. Sessions live in memory — a reboot kills them (for services that must survive, use systemd).

Attached, can't detach ("There is a screen on ... Attached")

The session is "stuck" to another terminal: screen -d -r work (detach and attach).

Garbled/broken display

Ctrl+a then C (clear) or reconnect. Check $TERM (xterm-256color).

Mouse scrolling doesn't work

screen scrolls through copy mode by default (Ctrl+a [). Increase the defscrollback buffer in ~/.screenrc. For mouse/panes, tmux is more convenient.

See also

  • tmux — Sessions that survive an SSH drop; windows and panes.
  • ssh-client — Connecting over SSH, copying with scp/rsync, ~/.ssh/config, port forwarding.
  • background-jobs — Running in the background, managing shell jobs so a process survives logout.
Loading…