How to use the screen command in Linux?

How to use the screen command in Linux?

After another SSH login, `screen -r deploy` brings the named session into your current terminal. I’m intrigued that Screen can show different windows side by side in separate regions.

Compare the session with the terminal displaying it. Now inspect what remains running inside the session.

What Screen keeps running in a terminal

A Screen session holds virtual-terminal windows. Those windows can keep their programs running after you detach the session from its controlling terminal, then let you reconnect while the host and processes remain running.

A session, a window, and a region are different Screen objects. The session groups windows, and a region is a visible area that displays one window.

Screen objectWhat it holds or displays
SessionA group of Screen windows that can be attached to or detached from a terminal.
WindowA virtual terminal running a shell or another command.
RegionAn area of the display that shows a Screen window. Splitting creates regions, not windows.

The view below shows Screen windows 0 and 1 in side-by-side regions.

Two GNU Screen regions displaying separate terminal windows
Each visible region here displays a different Screen window.

Each region displays one of the two Screen windows, so both shells remain independent.

Install Screen on your Linux distribution

Install the screen package from your distribution’s configured repositories. Ubuntu uses apt, while Fedora provides the package through dnf.

DistributionCommandPackage source
Ubuntusudo apt install screenUbuntu package details
Fedorasudo dnf install screenFedora package details

Fedora’s package page lists Screen in Fedora repositories, so Fedora does not need the EPEL setup shown in some Enterprise Linux instructions. If the command is missing after installation, check that your distribution’s repository configuration is enabled.

Start a named Screen session

Give a session a short name so you can identify it when reconnecting. This starts a new Screen session and opens its first shell window.

screen -S deploy

Run the command you want to keep inside that shell. Screen sends your keystrokes to the selected window, and the window remains available while the session is detached.

A long file transfer is one example, and our curl download tutorial shows that workflow.

  1. Start the session with a name such as deploy.
  2. Run the shell command inside the Screen window.
  3. Press Ctrl+A, then lowercase c, to open another window in the same session.
  4. Press Ctrl+A, then a double quote, to open the window list and select a window.

The window list shows each virtual terminal by number and title. Press Enter after selecting a row to switch to that window.

GNU Screen window list with the second shell selected
The selected row is the Screen window that receives focus after you press Enter.

Screen assigns the highlighted row to the next window receiving input, and this example has window 1 selected.

A short title helps identify each shell, including when you keep a terminal editor in a separate window, as in our guide to terminal-based text editors for Linux. Press Ctrl+A, then uppercase A, to name the current window.

GNU Screen prompt for naming the current window
Screen prompts for the title before applying it to the current window.

Type a short label at this prompt and press Enter, then verify the title in the window list.

Detach and reattach the same session

Press Ctrl+A, then lowercase d, to detach Screen and return to the shell that started it. The session and its windows stay available while the processes continue running on the host.

Screen detached from the named session and returned to a shell prompt
Screen prints a detached message and returns control to the shell.

The detached message confirms that Screen left the session running. From the shell, list sessions and reattach by the name you chose.

screen -ls
screen -r deploy

I ran a shell command inside a named Screen session, detached it, and reattached to it. I saw screen -ls mark the session Detached before the next attachment.

GNU Screen session listing with a named session marked Detached
screen -ls lists the named session and marks it Detached.

The session name in the screenshot is an example, and the process identifier changes on each start. Look for the Detached label before running screen -r with that session’s name.

If a session is attached in another terminal, use the detach-and-reattach form only when you intend to move control away from that terminal.

screen -d -r deploy

Switch windows and split the display

Screen uses Ctrl+A as its default command prefix, followed by one key. The screen(1) manual documents the default bindings, and our guide to reading the Linux man command explains how to use manual pages.

TaskDefault keys
Open a new windowCtrl+A, then lowercase c
Move to the next windowCtrl+A, then lowercase n
Show the window listCtrl+A, then a double quote
Rename the current windowCtrl+A, then uppercase A

The Screen key-binding list shows the default commands for creating windows, detaching, renaming, and splitting. Your configuration can differ, so use the Screen manual as the reference.

GNU Screen default key bindings with Ctrl+A as the command prefix
The default command prefix shown here is Ctrl+A. Your Screen configuration can change the bindings.

Screen’s split keys change the display layout, then you choose what to show in each region.

  1. Press Ctrl+A, then the vertical bar, to create a side-by-side region.
  2. Press Ctrl+A, then Tab, to move focus into the new region.
  3. Press Ctrl+A, then lowercase c, to create a window, or open the window list to select one.
  4. Press Ctrl+A, then uppercase S, to create a top-and-bottom split.

A new region starts blank until you select or create a Screen window for it.

GNU Screen display split into two side-by-side regions with the new region blank
A split creates a display region, but the blank region does not contain a new shell yet.

Press Ctrl+A, then Tab, to move focus to the blank region. Open a new window there or select an existing one from the window list.

Windows 0 and 1 remain separate shells in the side-by-side display shown above.

Ctrl+A, then uppercase S, splits the display into stacked regions. The layout below combines a vertical split with a horizontal split to show three windows at once.

Three GNU Screen windows shown in a mixed split-region layout
This mixed layout places windows 0, 1, and 2 in three regions.

Screen can show several regions, though the manual warns that scrolling output can appear much slower in a vertically split region. Keep a full-width window for output that you need to scan quickly.

Find the right session when reattach fails

An attached session still has a controlling terminal, while a detached session can be reattached from another terminal.

GNU Screen listing with one attached session and one detached session
screen -ls can show attached and detached sessions at the same time.

The listing shows an Attached session beside a Detached one, so choose the intended name or identifier before reattaching.

What screen -ls showsNext action
No sockets foundThere is no Screen session for this account on this host. Start a new named session.
Several detached sessionsUse screen -r followed by the exact session name or identifier.
The target session is Attached elsewhereUse screen -d -r with its name only if you intend to disconnect the other terminal.
A session is marked Dead or UnreachableCheck its host and process state before removing a stale socket.

The -d option disconnects the other terminal before attaching here. That moves the terminal connection, but the shell and processes inside the Screen session keep running while the host remains available.

I ran screen -d -r with that session attached elsewhere, and the shell command stayed available after the terminal connection moved.

If a job should run as a managed service after logout, use our guide to starting and stopping Linux services.

Close the session after its work is done

Exiting the shell closes its Screen window. If the session has multiple windows, close each window you no longer need or send Screen a quit command for the named session.

screen -S deploy -X quit

This command ends the named Screen session and its windows, so check that no command still needs to run before using it. A Screen session also ends when the host restarts, as explained in our guide to restarting Linux from the terminal.

GNU Screen session reports that it is terminating
Screen displays this message while it closes a session.

Use screen -ls again to confirm that a named session has closed. A detached session remains available, and a quit command ends the session and the work still running in its windows.

Screen command questions

The FAQ below covers the session questions that are not answered by the command sequences above.

Will Screen keep running after an SSH disconnect?

A detached Screen session can keep its windows and processes running after the SSH terminal disconnects, as long as the host and the processes remain running. Screen does not protect a process from a host shutdown or process failure.

How do I reattach to a named Screen session?

Run screen -r followed by the session name, such as screen -r deploy. Use screen -ls first if you need to find the name or identifier.

How do I detach from Screen without stopping a command?

Press Ctrl+A, then lowercase d. Screen returns you to the shell while keeping the session detached.

How do I close a Screen session?

Exit each shell you want to close, or run screen -S followed by the session name and -X quit. The quit command ends the session and its windows.