Post

screen

GNU Screen Complete Cheat Sheet for Sysadmins

Overview

GNU Screen is a terminal multiplexer that allows you to manage multiple terminal sessions within a single window. It’s an essential tool for sysadmins to:

  • Run long-running processes that persist after disconnection
  • Manage multiple remote sessions simultaneously
  • Split terminal views for monitoring multiple tasks
  • Share terminal sessions with team members
  • Maintain scrollback history and copy/paste between windows

Default escape key: Ctrl+a (all screen commands start with this)


Quick Start

Starting Screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Start a new named session (recommended)
screen -S mysession

# Start a detached session (runs in background)
screen -dmS mysession command-to-run

# List all screen sessions
screen -ls

# Attach to a session
screen -r mysession

# Attach to a detached session (if multiple, specify PID)
screen -r <PID>

# Reattach or create if doesn't exist
screen -xRR mysession

# Force detach and attach (when session is attached elsewhere)
screen -d -r mysession

Session Management

Basic Session Operations

Command Description
Ctrl+a d Detach current session (keeps processes running)
Ctrl+a D D Detach and logout (quick exit)
Ctrl+a :sessionname name Rename current session
Ctrl+a :sessionname Show current session name
Ctrl+a ? Display help with all key bindings

Session Commands (Command Mode)

Press Ctrl+a : to enter command mode, then type:

1
2
3
4
5
6
7
8
9
10
11
12
:quit                    # Close all windows and exit screen
:sessionname name        # Rename session
:source ~/.screenrc      # Reload configuration file
:hardcopy -h             # Copy scrollback buffer to file
:log                     # Toggle logging for current window
:logfile filename        # Set log file name
:loglevel [0-8]          # Set log verbosity (0=off, 8=debug)
:time                    # Show system time in caption
:caption always          # Always show caption bar
:caption splitonly       # Show caption only in split mode
:bell_msg "message"      # Custom bell message
:vbell off               # Disable visual bell

Window Management

Copy and Paste Operations

Command Description
Enter (in copy mode) Start selection at cursor
Space (in copy mode) Toggle selection, mark start/end
Enter (after selection) Copy selected text to buffer
Ctrl+a ] Paste from buffer
Ctrl+a > Write paste buffer to file
Ctrl+a < Read paste buffer from file
Ctrl+a :buffer List paste buffers
Ctrl+a :clear Clear paste buffer

Copy/Paste Workflow:

  1. Ctrl+a [ - Enter copy mode
  2. Navigate to start position
  3. Space - Start selection
  4. Navigate to end position
  5. Space or Enter - Copy selection to buffer
  6. Ctrl+a ] - Paste at cursor position

Logging and Monitoring

Session Logging

Command Description
Ctrl+a H Start/stop logging current window to screenlog.0
Ctrl+a :logfile filename Set custom log filename
Ctrl+a :log on/off Toggle logging
Ctrl+a :loglevel N Set log level (0-8)

Window Monitoring

Command Description
Ctrl+a M Monitor current window for activity (bell on output)
Ctrl+a _ Monitor current window for silence (bell if no output)
Ctrl+a :silence N Set silence timeout in seconds
Ctrl+a :activity Toggle activity monitoring

Advanced Features

Nested Screen Sessions

When running screen inside another screen session:

Command Description
Ctrl+a a Send escape key to inner screen
Ctrl+a a c Create window in inner screen
Ctrl+a a d Detach inner screen session
Ctrl+a a k Kill inner screen session

Tip: Change escape key in outer or inner screen to avoid confusion:

1
2
3
4
5
# In ~/.screenrc for outer screen
escape ^Jj  # Use Ctrl+j instead of Ctrl+a

# In ~/.screenrc for inner screen  
escape ^Tt  # Use Ctrl+t instead of Ctrl+a

Screen with SSH

1
2
3
4
5
6
7
8
# Create screen session before SSH disconnect
ssh user@host
screen -S work
# Run commands...
Ctrl+a d  # Detach, disconnect SSH, processes keep running
# Later...
ssh user@host
screen -r work  # Reattach to session

Locking Screen

Command Description
Ctrl+a x Lock current screen session (password protected)
Ctrl+a :password Set screen password (encrypted in ~/.screenrc)

Configuration (.screenrc)

Location and Loading

  • User config: ~/.screenrc
  • System config: /etc/screenrc or /usr/local/etc/screenrc
  • Load config on the fly: Ctrl+a :source ~/.screenrc

Essential Configuration Options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Change escape key (recommended: Ctrl+j or Ctrl+`)
escape ^Jj          # Use Ctrl+j as escape, Ctrl+j j sends literal Ctrl+j
# escape ^^^       # Use Ctrl+^ as escape
# escape ```       # Use backtick as escape

# Start windows at 1 instead of 0
screen 1
bind 0 select 10

# Always show window titles in status line
hardstatus alwayslastline

# Custom hardstatus line
hardstatus string "%{= kw}%-Lw%{= BW}%n*%f %t%{-}%+Lw %<"

# Enable 256 colors
term screen-256color

# Auto-detach on hangup
autodetach on

# Don't display copyright
startup_message off

# Scrollback buffer size (default 100 lines)
defscrollback 5000

# Enable alternate screen support
altscreen on

# Set default shell
shell /bin/bash

# Set terminal title
caption always "%{= kw}%n %t%{-}"

# Bind function keys for window switching
bindkey -k k1 select 1
bindkey -k k2 select 2
bindkey -k k3 select 3
bindkey -k k4 select 4
bindkey -k k5 select 5
bindkey -k k6 select 6
bindkey -k k7 select 7
bindkey -k k8 select 8
bindkey -k k9 select 9
bindkey -k k0 select 10

# Bind F11/F12 for previous/next window
bindkey -k F11 prev
bindkey -k F12 next

# Disable visual bell
vbell off

# Set UTF-8 encoding
defencoding utf8
encoding utf8

# Auto-window title from running command
shelltitle "$ |"

# Bind Ctrl+a a to send literal Ctrl+a
bind a

# Set border color for split regions
caption splitonly "%{= kw}%n %t%{-}"

Complete Example .screenrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ~/.screenrc - Comprehensive configuration

# Use Ctrl+j as escape key (easier than Ctrl+a)
escape ^Jj

# Start at window 1
screen 1
bind 0 select 10

# 5000 lines scrollback
defscrollback 5000

# Enable 256 colors
term screen-256color

# Always show status bar with window list
hardstatus alwayslastline
hardstatus string "%{= kw}%-Lw%{= BW}%n*%f %t%{-}%+Lw %<"

# Auto-detach on connection loss
autodetach on

# No startup message
startup_message off

# UTF-8 support
defencoding utf8
encoding utf8

# Function keys for window switching
bindkey -k k1 select 1
bindkey -k k2 select 2
bindkey -k k3 select 3
bindkey -k k4 select 4
bindkey -k k5 select 5
bindkey -k k6 select 6
bindkey -k k7 select 7
bindkey -k k8 select 8
bindkey -k k9 select 9
bindkey -k k0 select 10

# F11/F12 for prev/next window
bindkey -k F11 prev
bindkey -k F12 next

# Disable bell
vbell off

# Bind Ctrl+a a to send literal Ctrl+a
bind a

# Set shell
shell /bin/bash

Command-Line Options

Starting Screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Basic session creation
screen                          # Start new session
screen -S name                  # Start named session
screen -d -m command            # Start detached, run command
screen -dmS name command        # Start named detached session

# Session attachment
screen -r                       # Attach to single detached session
screen -r name                  # Attach to named session
screen -r PID                   # Attach by PID
screen -x                       # Attach without detaching others
screen -x name                  # Attach to named session (multi-attach)
screen -d -r name               # Force detach then attach
screen -D -RR                   # Smart attach (create if needed)

# Session listing
screen -ls                      # List all sessions
screen -list                    # Same as -ls

# Session control
screen -S name -X command       # Send command to session
screen -S name -X quit          # Kill named session
screen -S name -X detach        # Detach named session

# Advanced options
screen -c file                  # Use alternate config file
screen -e x y                   # Change escape keys (x=escape, y=literal)
screen -h lines                 # Set scrollback buffer size
screen -i                       # Enable flow control
screen -m                       # Ignore $STY, force new session
screen -p window                # Go to specific window on start
screen -t title                 # Set window title
screen -U                       # Enable UTF-8
screen -x                       # Multi-attach mode

Common Sysadmin Use Cases

1. Long-Running Processes

1
2
3
4
5
6
# Start a backup that survives SSH disconnect
screen -S backup
# Run your backup command
rsync -avz /data /backup/
# Detach with Ctrl+a d
# Reattach later: screen -r backup

2. Monitoring Multiple Servers

1
2
3
4
5
6
7
8
9
10
11
# Create session with multiple windows
screen -S monitoring
# Window 1: tail logs on server1
ssh user@server1 "tail -f /var/log/syslog"
# Ctrl+a c (new window)
# Window 2: monitor disk space
ssh user@server2 "watch df -h"
# Ctrl+a c (new window)
# Window 3: check processes
ssh user@server3 "top"
# Ctrl+a d to detach

3. Split Screen Monitoring

1
2
3
4
5
6
7
8
9
10
# Start screen
screen -S monitor
# Create first window with server1
ssh user@server1 "top"
# Ctrl+a S (horizontal split)
# Ctrl+a tab (switch to new region)
# Ctrl+a " (select window 2 or create new)
# Window 2: server2 monitoring
ssh user@server2 "tail -f /var/log/auth.log"
# Ctrl+a tab to switch between views

4. Shared Session for Collaboration

1
2
3
4
5
6
# User A creates shared session
screen -S shared -x  # -x allows multi-attach
# User B attaches to same session
ssh user@host
screen -x shared
# Both users see same screen, can collaborate

5. Running Commands in Background

1
2
3
4
5
6
7
8
# Start detached session running command
screen -dmS jobname long-running-command
# Check if running
screen -ls
# View output later
screen -r jobname
# Or just view logs if logging enabled
tail -f screenlog.0

6. Reattach After SSH Disconnect

Add to ~/.bashrc:

1
2
3
4
# Auto-attach to existing screen or create new
if [ -z "$STY" ]; then
    screen -xRR main 2>/dev/null || screen -S main
fi

Now every SSH login automatically attaches to “main” session or creates it.


Troubleshooting

Common Issues

Problem: There is a screen on: error when attaching

1
2
3
4
5
# Force detach and attach
screen -d -r sessionname

# Or kill the stale session
screen -S sessionname -X quit

Problem: Can’t type Ctrl+a in applications (like bash, vim)

1
2
# Send literal Ctrl+a to application
Ctrl+a a  # Then type your command (e.g., Ctrl+a a a goes to bash beginning-of-line)

Problem: Nested screen confusion

1
2
3
# Change escape key in one of the sessions
# In outer screen: Ctrl+a :escape ^Jj
# Now use Ctrl+j for outer, Ctrl+a for inner

Problem: No colors or 256-color support

1
2
3
4
5
# In ~/.screenrc:
term screen-256color
# Or set TERM before starting screen:
export TERM=screen-256color
screen

Problem: Scrollback not working

1
2
3
4
5
# Increase scrollback buffer
# In ~/.screenrc:
defscrollback 5000
# Or on command line:
screen -h 5000

Problem: Window titles not showing

1
2
3
# Enable hardstatus in ~/.screenrc:
hardstatus alwayslastline
hardstatus string "%{= kw}%-Lw%{= BW}%n*%f %t%{-}%+Lw %<"

Pro Tips for Sysadmins

  1. Always name your sessions: screen -S descriptive-name makes management easier
  2. Use a good .screenrc: Configure escape key, scrollback, and status bar for efficiency
  3. Log important sessions: Ctrl+a H to capture output for audit trails
  4. Split for monitoring: Use horizontal splits (Ctrl+a S) to watch multiple logs simultaneously
  5. Copy/paste between windows: Great for moving commands between sessions
  6. Monitor for silence: Ctrl+a _ alerts you if a process hangs (no output)
  7. Use with SSH: Never lose work due to network drops
  8. Share sessions: screen -x for pair programming or collaborative troubleshooting
  9. Auto-start: Add to .bashrc to always have a screen session available
  10. Window 0 is annoying: Configure screen 1 in .screenrc to start at window 1

Cheat Sheet Summary Table

Session Commands

Key Action
Ctrl+a d Detach
Ctrl+a D D Detach & logout
Ctrl+a ? Help
Ctrl+a : Command prompt

Window Commands

Key Action
Ctrl+a c New window
Ctrl+a n Next window
Ctrl+a p Previous window
Ctrl+a 0-9 Switch to window #
Ctrl+a " Window list
Ctrl+a A Rename window
Ctrl+a w Show windows
Ctrl+a Ctrl+a Toggle windows
Ctrl+a k Kill window
Ctrl+a \ Kill all

Split Screen

Key Action
Ctrl+a S Split horizontally
Ctrl+a \| Split vertically
Ctrl+a tab Next region
Ctrl+a X Remove region
Ctrl+a Q Remove all but current

Copy/Paste

Key Action
Ctrl+a [ Copy mode
Space Start/end selection
Enter Copy selection
Ctrl+a ] Paste
Ctrl+a > Save buffer
Ctrl+a < Load buffer

Logging & Monitoring

Key Action
Ctrl+a H Toggle logging
Ctrl+a M Monitor activity
Ctrl+a _ Monitor silence
Ctrl+a x Lock screen

Additional Resources

  • Man page: man screen
  • Info documentation: info screen
  • Official GNU Screen manual: https://www.gnu.org/software/screen/manual/
  • Configuration examples: /usr/share/doc/screen/examples/
  • Alternative: Consider tmux for more modern features (similar functionality)
This post is licensed under CC BY 4.0 by the author.