GNU Screen

GNU screen cheatsheet — terminal multiplexer for persistent sessions. screen -S name, Ctrl+A D to detach, screen -r to reattach. Keep processes running after SSH disconnect.

7 min read

What it is

GNU Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. It’s useful for keeping processes running after you disconnect from SSH, or for managing multiple terminals within a single SSH session.

Installation

Linux

sudo apt update && sudo apt install screen # Debian/Ubuntu
sudo yum install screen # CentOS/RHEL/Fedora
sudo dnf install screen # Fedora

macOS

brew install screen

Windows

Screen is not natively available on Windows. For Windows Subsystem for Linux (WSL), follow the Linux instructions. Alternatively, consider tools like tmux or PuTTY’s session management.

Core Concepts

  • Session: A Screen session is a container for one or more windows. When you start screen, you create a new session.
  • Window: Within a session, you can create multiple windows. Each window runs a separate process (usually a shell). You can switch between windows.
  • Detaching: You can detach from a Screen session, leaving all the processes running inside it. You can then reattach to the session later from the same or a different terminal.
  • Terminals: Screen provides a virtual terminal for each window, allowing you to interact with the processes running within them.

Commands / Usage

Starting and Managing Sessions

  • Start a new session:

    screen
    

    Starts a new Screen session with a single window.

  • Start a new session with a name:

    screen -S my_session_name
    

    Starts a new Screen session named my_session_name. This makes it easier to find and reattach later.

  • List running sessions:

    screen -ls
    

    Lists all currently running Screen sessions, showing their PIDs and names.

    12345.my_session_name (Detached)
    67890.another_session (Attached)
    2 Sockets in /var/run/screen/S-yourusername.
    
  • Reattach to the most recent session:

    screen -r
    

    Reattaches to the last Screen session you were attached to.

  • Reattach to a specific session by PID:

    screen -r 12345
    

    Reattaches to the Screen session with PID 12345.

  • Reattach to a specific session by name:

    screen -r my_session_name
    

    Reattaches to the Screen session named my_session_name.

  • Reattach to a detached session (even if another client is attached):

    screen -x my_session_name
    

    Attaches to a detached session. If the session is already attached elsewhere, -x allows multiple clients to connect to the same session concurrently.

  • Detach from the current session: Press Ctrl+a followed by d. The session remains running in the background.

  • Kill a session: Inside the session, type exit or press Ctrl+d. Or, from outside the session:

    screen -X -S my_session_name quit
    

    This sends the quit command to the specified session.

Managing Windows within a Session

  • Create a new window: Press Ctrl+a followed by c. A new shell prompt appears in a new window.

  • Switch to the next window: Press Ctrl+a followed by n.

  • Switch to the previous window: Press Ctrl+a followed by p.

  • Switch to a specific window by number: Press Ctrl+a followed by the window number (e.g., Ctrl+a 0 for the first window, Ctrl+a 1 for the second).

  • List all windows in the current session: Press Ctrl+a followed by w. A list of windows appears at the bottom of the screen, e.g., 0$ bash 1*bash 2-htop. The * indicates the current window.

  • Rename the current window: Press Ctrl+a followed by , (comma). Enter the new name at the prompt.

  • Kill the current window: Press Ctrl+a followed by k. You will be prompted to confirm.

Splitting the Screen (Regions)

  • Split the current region horizontally: Press Ctrl+a followed by S (uppercase S). This splits the current window into two regions, one above the other.

  • Split the current region vertically: Press Ctrl+a followed by | (pipe). This splits the current window into two regions, side-by-side.

  • Switch focus between regions: Press Ctrl+a followed by Tab. Cycles focus through the available regions.

  • Close the current region: Press Ctrl+a followed by X (uppercase X). This closes the region that currently has focus.

  • Remove all regions except the current one: Press Ctrl+a followed by Q (uppercase Q).

Copy and Paste

  • Enter copy mode: Press Ctrl+a followed by [ (left bracket). You can now move the cursor using arrow keys or vi-like commands (h, j, k, l, w, b, etc.).

  • Start selection: In copy mode, move the cursor to the beginning of the text you want to copy and press Space.

  • End selection and copy: Move the cursor to the end of the text and press Space again. The selected text is now copied to Screen’s buffer.

  • Paste from the buffer: Press Ctrl+a followed by ] (right bracket). The content of the buffer is pasted at the current cursor position.

Other Useful Commands

  • Lock the screen: Press Ctrl+a followed by x. You’ll be prompted for your login password to unlock.

  • Show command status line: Press Ctrl+a followed by _ (underscore). Displays the name and PID of the process running in the current window.

  • Send a command to all windows: Press Ctrl+a followed by : (colon) to enter command mode, then type command <your_command> and press Enter. Example: Ctrl+a : command echo "Hello all"

  • Execute a command in a new window: Press Ctrl+a followed by :. Type screen -t "My Command" -- <your_command> and press Enter. Example: Ctrl+a : screen -t "Ping Google" -- ping google.com

Key Bindings Summary (Command Prefix Ctrl+a)

Key Action
d Detach session
c Create new window
n Next window
p Previous window
0-9 Switch to window number
w List windows
, Rename current window
k Kill current window
S Split region horizontally
` `
Tab Switch focus to next region
X Close current region
Q Remove all regions except current
[ Enter copy/scrollback mode
] Paste from buffer
x Lock screen
_ Show window status
: Enter command mode
? Display help/keybindings

Common Patterns

  • Keep a long-running process alive:

    screen -S my_process_session
    # Inside screen:
    ./my_long_running_script.sh
    # Press Ctrl+a, then d to detach
    

    You can now log out, and my_long_running_script.sh will continue to run. Reattach later with screen -r my_process_session.

  • Manage multiple development servers/tasks:

    screen -S dev_env
    # Inside screen:
    # Window 0:
    npm start
    # Press Ctrl+a, then c
    # Window 1:
    python manage.py runserver
    # Press Ctrl+a, then c
    # Window 2:
    tail -f logs/development.log
    # Press Ctrl+a, then w to see all windows
    
  • Share a terminal session (advanced): Start a session and then use screen -x <session_name> from another terminal. Both terminals will see the same output and can type commands. Be cautious, as simultaneous input can lead to unexpected results.

  • Execute a command and detach automatically:

    screen -S temp_task -d -m bash -c "./my_script.sh; exec bash"
    

    This starts a detached session, runs my_script.sh, and then leaves you with a bash prompt in that detached session. The exec bash is important to keep the screen session alive after the script finishes.

Gotchas

  • Scrollback Buffer: When you detach, the terminal scrolls up. To see previous output, you need to enter copy mode (Ctrl+a [) and scroll up.
  • Ctrl+a is the command prefix: If you want to type Ctrl+a literally into a running program, you need to press Ctrl+a twice.
  • Killing a session vs. Detaching: Ctrl+a d detaches, leaving processes running. Ctrl+a k (and confirming) or typing exit kills the current window/session. Ensure you detach if you want the processes to continue.
  • Reattaching to a "dead" session: If a Screen session process died unexpectedly, screen -r might fail. You might need to manually remove the stale socket file (e.g., /var/run/screen/S-yourusername/12345.my_session_name.pty) before you can reattach or start a new session with that name.
  • Permissions and Environment: When you reattach to a session, it inherits the environment and permissions of the user who started it. If you started a session as root and reattach as a regular user, you’ll still have root privileges within that session.
  • Screen vs. Tmux: tmux is a modern alternative to screen with a more flexible pane management system and a different command prefix (Ctrl+b by default). Many users prefer tmux for its features.