GNU Screen

Also known as: screen

On this page 7

GNU Screen is a terminal multiplexer for Linux and other Unix-like systems that runs several shell sessions inside one terminal and keeps them alive when the user disconnects. In the Shell & terminal toolset, it sits between the terminal emulator and the programs it hosts, so a long build, a database migration or an interactive editor survives a dropped SSH connection or a closed laptop lid.

First released in 1987 and maintained by the GNU Project, Screen is often invoked simply as screen. It is installed by default or available as a standard package on nearly every Linux distribution, which makes it a dependable choice on servers where newer tools such as tmux may be missing.

How GNU Screen keeps sessions alive

Screen works by splitting the terminal experience into a persistent server process and a disposable client attachment. When a user starts screen, the program creates a session server that owns one or more pseudo-terminals (PTYs), each running its own shell, and a client that draws the active window on the real TTY.

Because the shells are children of the Screen server rather than of the login session, closing the terminal does not send SIGHUP to them. The client can detach, leaving the server running in the background, and a later client can reattach from the same machine or from a new SSH login. The server and client communicate through a named socket, usually under /run/screen/S-username or a directory set by the SCREENDIR variable.

Screen also maintains its own virtual terminal emulation, based on the VT100 and ANSI standards. It interprets the escape sequences that programs send, stores the resulting screen state and redraws it on attach, which is why the window content reappears intact after reconnection.

The table below summarizes the main components and the role each one plays.

ComponentWhat it isRole
SessionA named Screen server processOwns all windows and survives detach
WindowOne pseudo-terminal with a shell or programRuns a single job, numbered from 0
RegionA visible area of the display after a splitShows one window at a time
SocketA named pipe or Unix socket fileConnects clients to the session server
Scrollback bufferStored lines of past output per windowAllows review and copy of history
Core components of a Screen session

Essential commands and key bindings

Screen is controlled through a command prefix, by default Ctrl-a, followed by a single key. This prefix is captured by Screen and never reaches the shell, which is the reason that the Bash shortcut for moving to the start of a line must be typed as Ctrl-a a inside a session.

The following table lists the commands that cover most daily work, from starting a session to sharing it.

ActionCommand or keysEffect
Start a named sessionscreen -S buildCreates a session called build
List sessionsscreen -lsShows session IDs and attached or detached state
DetachCtrl-a dLeaves the session running in the background
Reattachscreen -r buildConnects to a detached session
Force reattachscreen -d -r buildDetaches another client first, then attaches
New windowCtrl-a cOpens another shell in the session
Switch windowCtrl-a n or Ctrl-a 0-9Moves to the next or a numbered window
Split horizontallyCtrl-a SDivides the display into two regions
Copy modeCtrl-a [Scrolls back and selects text
Log outputCtrl-a HWrites window output to a screenlog file
Frequently used Screen commands

Configuration lives in `~/.screenrc` for each user and /etc/screenrc system-wide. Common settings include a larger `defscrollback` value, a `hardstatus` line that shows window names and the clock, and startup_message off to skip the banner.

GNU Screen compared with tmux and other approaches

The closest neighbor of Screen is tmux, a newer terminal multiplexer that solves the same problem with a client-server design built from the start, a scriptable command interface and more flexible pane layouts. The choice between them is usually a matter of availability, habit and scripting needs rather than capability.

The comparison below covers the alternatives that administrators most often weigh against Screen.

ToolPersistence after disconnectSplit viewsBest fit
GNU ScreenYes, via detach and reattachHorizontal and verticalLegacy servers and serial consoles
tmuxYes, via detach and reattachFlexible panes and layoutsDaily multiplexing and scripting
nohupYes, but no interactive accessNoOne unattended background command
systemd-runYes, as a transient unitNoManaged services with logging
Terminal tabsNo, they close with the clientDepends on emulatorLocal desktop work only
Screen and related ways to keep work running

Screen keeps a few distinct strengths. It can attach directly to a serial device with a command such as screen /dev/ttyUSB0 115200, which makes it a lightweight console for routers, embedded boards and server management ports. It also offers multiuser mode, in which several accounts can attach to one session with access control lists.

Practical use on Linux servers

The typical workflow is simple: start a named session before any long task, run the job, detach and reconnect later. Naming sessions avoids the confusion of numeric IDs when several sessions exist on one host.

The decision table below maps common situations to a recommended approach.

SituationRecommended approachReason
Kernel compile over unstable SSHRun inside a named sessionSurvives connection drops
Unattended script at bootscreen -dmS name commandStarts detached without a terminal
Pair troubleshootingMultiuser mode with acladdTwo users see the same shell
Router console over USBscreen /dev/ttyUSB0 9600Acts as a serial terminal
Production serviceUse a systemd unit insteadGives restart policy and journal logs
Choosing how to use Screen
  • Use `screen -dmS` to launch a detached job from a script or cron entry.
  • Use `screen -X -S name` followed by a Screen command to control a running session remotely.
  • Use `screen -x` to attach a second client to an already attached session for shared viewing.

Limitations and common mistakes

Screen is reliable, but its age shows. Development was slow for many years, so features such as true color support, vertical splits and Unicode handling arrived late, and some distributions shipped old versions. Several Linux distributions now install tmux by default and treat Screen as optional.

Most problems that users report come from a small set of causes, listed below.

SymptomCauseFix
Session lost after rebootScreen keeps sessions only in memoryUse a service manager for durable jobs
Shell ignores Ctrl-aPrefix captured by ScreenType Ctrl-a a or remap with escape
Session marked deadServer process was killedRemove the stale socket with screen -wipe
Cannot reattachSession still attached elsewhereUse screen -d -r to take it over
Jobs killed on logoutsystemd-logind KillUserProcesses enabledEnable lingering or adjust the setting
Common Screen problems and fixes

A frequent misconception is that Screen makes processes permanent. It only protects them from terminal disconnection; a reboot, an out-of-memory kill or a session cleanup policy still ends them. Multiuser mode also requires care, because a misconfigured access list can give another account full shell access with your privileges.

Put simply

GNU Screen is like a waiting room for your command line. You open your work inside it, and if your connection breaks or you close the window, the work keeps running in the room. When you come back, you step in again and everything is exactly where you left it.

It also lets one window hold several command lines at once, so you can switch between them with a keystroke. The key takeaway is that Screen protects long tasks from dropped connections, but it does not protect them from a computer restart.

In practice

A systems administrator needs to copy 2 TB of archive data between two storage servers in a data center. The transfer with rsync is expected to take about nine hours, and the administrator works from a laptop over a VPN that disconnects every evening.

She logs in over SSH, types screen -S archive-sync, starts the rsync command and presses Ctrl-a d to detach the session. She also opens a second window with Ctrl-a c to watch disk usage with df -h. When the VPN drops at 18:00, the transfer continues untouched on the server.

The next morning she reconnects, runs screen -r archive-sync and sees the final rsync summary on screen. The copy finished without a single restart, and she closes the session with exit in each window.