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:
screenStarts a new Screen session with a single window.
-
Start a new session with a name:
screen -S my_session_nameStarts a new Screen session named
my_session_name. This makes it easier to find and reattach later. -
List running sessions:
screen -lsLists 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 -rReattaches to the last Screen session you were attached to.
-
Reattach to a specific session by PID:
screen -r 12345Reattaches to the Screen session with PID
12345. -
Reattach to a specific session by name:
screen -r my_session_nameReattaches to the Screen session named
my_session_name. -
Reattach to a detached session (even if another client is attached):
screen -x my_session_nameAttaches to a detached session. If the session is already attached elsewhere,
-xallows multiple clients to connect to the same session concurrently. -
Detach from the current session: Press
Ctrl+afollowed byd. The session remains running in the background. -
Kill a session: Inside the session, type
exitor pressCtrl+d. Or, from outside the session:screen -X -S my_session_name quitThis sends the
quitcommand to the specified session.
Managing Windows within a Session
-
Create a new window: Press
Ctrl+afollowed byc. A new shell prompt appears in a new window. -
Switch to the next window: Press
Ctrl+afollowed byn. -
Switch to the previous window: Press
Ctrl+afollowed byp. -
Switch to a specific window by number: Press
Ctrl+afollowed by the window number (e.g.,Ctrl+a 0for the first window,Ctrl+a 1for the second). -
List all windows in the current session: Press
Ctrl+afollowed byw. 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+afollowed by,(comma). Enter the new name at the prompt. -
Kill the current window: Press
Ctrl+afollowed byk. You will be prompted to confirm.
Splitting the Screen (Regions)
-
Split the current region horizontally: Press
Ctrl+afollowed byS(uppercase S). This splits the current window into two regions, one above the other. -
Split the current region vertically: Press
Ctrl+afollowed by|(pipe). This splits the current window into two regions, side-by-side. -
Switch focus between regions: Press
Ctrl+afollowed byTab. Cycles focus through the available regions. -
Close the current region: Press
Ctrl+afollowed byX(uppercase X). This closes the region that currently has focus. -
Remove all regions except the current one: Press
Ctrl+afollowed byQ(uppercase Q).
Copy and Paste
-
Enter copy mode: Press
Ctrl+afollowed by[(left bracket). You can now move the cursor using arrow keys orvi-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
Spaceagain. The selected text is now copied to Screen’s buffer. -
Paste from the buffer: Press
Ctrl+afollowed by](right bracket). The content of the buffer is pasted at the current cursor position.
Other Useful Commands
-
Lock the screen: Press
Ctrl+afollowed byx. You’ll be prompted for your login password to unlock. -
Show command status line: Press
Ctrl+afollowed by_(underscore). Displays the name and PID of the process running in the current window. -
Send a command to all windows: Press
Ctrl+afollowed by:(colon) to enter command mode, then typecommand <your_command>and press Enter. Example:Ctrl+a : command echo "Hello all" -
Execute a command in a new window: Press
Ctrl+afollowed by:. Typescreen -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 detachYou can now log out, and
my_long_running_script.shwill continue to run. Reattach later withscreen -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. Theexec bashis 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+ais the command prefix: If you want to typeCtrl+aliterally into a running program, you need to pressCtrl+atwice.- Killing a session vs. Detaching:
Ctrl+a ddetaches, leaving processes running.Ctrl+a k(and confirming) or typingexitkills 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 -rmight 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:
tmuxis a modern alternative toscreenwith a more flexible pane management system and a different command prefix (Ctrl+bby default). Many users prefertmuxfor its features.