screen Command

Create and manage multiple terminal sessions within a single terminal window using the screen terminal multiplexer.

Syntax

screen [OPTIONS] [COMMAND [ARGS]] screen -r [SESSION_NAME] screen -list

The screen command allows you to run multiple terminal sessions, detach from them, and reattach later, making it ideal for long-running processes and remote work.

Common Options

Option Description
-S name Create a new session with specified name
-r [name] Reattach to a detached session
-d Detach the session (force detach if attached elsewhere)
-D Detach and logout remote session
-list List all screen sessions
-ls Same as -list
-x Attach to session without detaching other users
-R Reattach if possible, otherwise create new session
-c file Use specified config file
-m Force creation of new session

Basic Usage

Starting and managing sessions

# Start a new screen session screen # Start a named session screen -S mysession # Start session with a command screen -S backup tar -czf backup.tar.gz /home/user/ # List all sessions screen -list screen -ls

Create and list screen sessions

Detaching and reattaching

# Detach from current session (inside screen) # Press: Ctrl+A then D # Reattach to the last session screen -r # Reattach to specific session screen -r mysession # Reattach to session by PID screen -r 12345 # Force reattach (detach others first) screen -d -r mysession

Detach from and reattach to screen sessions

Session management

# Create new session or reattach if exists screen -R mysession # Attach without detaching others (multiuser) screen -x mysession # Kill a detached session screen -S mysession -X quit # Send command to detached session screen -S mysession -X stuff "ls -la\n"

Advanced session management operations

Screen Key Bindings

Essential key combinations (Ctrl+A prefix)

Key Combination Action
Ctrl+A, D Detach from session
Ctrl+A, C Create new window
Ctrl+A, N Switch to next window
Ctrl+A, P Switch to previous window
Ctrl+A, 0-9 Switch to window number
Ctrl+A, " List all windows
Ctrl+A, A Rename current window
Ctrl+A, K Kill current window
Ctrl+A, S Split screen horizontally
Ctrl+A, | Split screen vertically
Ctrl+A, Tab Switch between split regions
Ctrl+A, X Close current split region
Ctrl+A, Q Close all split regions except current
Ctrl+A, ? Show help (key bindings)

Window Management

Creating and switching windows

# Inside screen session: # Create new window: Ctrl+A, C # Switch to next window: Ctrl+A, N # Switch to previous window: Ctrl+A, P # Switch to window 0: Ctrl+A, 0 # Switch to window 1: Ctrl+A, 1 # List windows: Ctrl+A, " # From command line: # Create session with multiple windows screen -S multiwin # Then inside screen, create more windows with Ctrl+A, C # Start session and immediately create named window screen -S work -t editor vim

Manage multiple windows within a screen session

Window naming and organization

# Rename current window (inside screen) # Press: Ctrl+A, A # Then type new name # Create window with specific name # Press: Ctrl+A, C # Then: Ctrl+A, A to rename # Example session setup: screen -S development # Window 0: editor (Ctrl+A, A, type "editor") # Window 1: server (Ctrl+A, C, Ctrl+A, A, type "server") # Window 2: logs (Ctrl+A, C, Ctrl+A, A, type "logs")

Organize and name windows for better workflow

Screen Splitting

Horizontal and vertical splits

# Split screen horizontally # Press: Ctrl+A, S # Split screen vertically (if supported) # Press: Ctrl+A, | # Switch between split regions # Press: Ctrl+A, Tab # Create new window in current region # Press: Ctrl+A, C # Close current split region # Press: Ctrl+A, X # Close all splits except current # Press: Ctrl+A, Q

Split screen to view multiple windows simultaneously

Practical splitting workflow

# Example workflow: # 1. Start screen session screen -S coding # 2. Split horizontally # Ctrl+A, S # 3. Move to bottom region # Ctrl+A, Tab # 4. Create new window for logs # Ctrl+A, C # 5. Start log monitoring tail -f /var/log/app.log # 6. Switch back to top region # Ctrl+A, Tab # 7. Start editor vim mycode.py

Example workflow using screen splitting for development

Practical Examples

Long-running processes

# Start backup process in screen screen -S backup rsync -av /home/user/ user@backup-server:/backups/ # Detach: Ctrl+A, D # Check progress later screen -r backup # Start compilation in background screen -S compile -d -m make -j4 # Monitor system updates screen -S updates sudo apt update && sudo apt upgrade # Detach and check later

Use screen for long-running processes that shouldn't be interrupted

Development environment

# Create development session screen -S dev # Window 0: Editor vim project.py # Ctrl+A, A, rename to "editor" # Window 1: Server # Ctrl+A, C python manage.py runserver # Ctrl+A, A, rename to "server" # Window 2: Database # Ctrl+A, C mysql -u user -p database # Ctrl+A, A, rename to "database" # Window 3: Logs # Ctrl+A, C tail -f /var/log/app.log # Ctrl+A, A, rename to "logs"

Set up a complete development environment with multiple windows

Remote server management

# SSH to server and start screen ssh [email protected] screen -S admin # Window 0: System monitoring htop # Window 1: Log monitoring # Ctrl+A, C tail -f /var/log/syslog # Window 2: Service management # Ctrl+A, C sudo systemctl status nginx # Detach and logout # Ctrl+A, D exit # Later, reconnect and reattach ssh [email protected] screen -r admin

Manage remote servers with persistent screen sessions

Configuration

Screen configuration file (.screenrc)

# ~/.screenrc example configuration # Change escape key from Ctrl+A to Ctrl+J escape ^Jj # Disable startup message startup_message off # Set scrollback buffer defscrollback 10000 # Enable visual bell vbell on vbell_msg "Bell in window %" # Set default shell shell -$SHELL # Window numbering starts at 1 bind c screen 1 bind 0 select 10 # Status line hardstatus alwayslastline hardstatus string '%{= kG}[%{G}%H%? %1`%?%{g}][%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}][%{B}%m/%d %{W}%C%A%{g}]' # Enable mouse scrolling termcapinfo xterm* ti@:te@ # Auto-detach on hangup autodetach on

Customize screen behavior with configuration file

Advanced configuration options

# ~/.screenrc advanced settings # Logging deflog on logfile /tmp/screen-%Y%m%d-%n.log # Activity monitoring activity "Activity in window %n" # Silence monitoring silence on silencewait 15 # Password protection password ODSJQf.4IJN7E # Multi-user support multiuser on acladd john aclchg john +rwx "#?" # Key bindings bind r source ~/.screenrc bind R eval "source ~/.screenrc" "echo '.screenrc reloaded'" bind x remove bind j focus down bind k focus up bind h focus left bind l focus right

Advanced screen configuration for power users

Troubleshooting

Common Issues and Solutions
  • Cannot reattach to session - Check if session exists with screen -ls
  • Session appears attached elsewhere - Use screen -d -r sessionname to force reattach
  • Screen freezes - Press Ctrl+Q to unfreeze (Ctrl+S freezes)
  • Cannot create new window - Check if you've reached the window limit
  • Lost session after network disconnect - Sessions should persist; check with screen -ls

Session recovery and cleanup

# List all sessions including dead ones screen -ls # Clean up dead sessions screen -wipe # Force kill a session screen -S sessionname -X quit # Kill all detached sessions screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill # Recover from corrupted session screen -D -R sessionname # Check screen processes ps aux | grep screen

Recover and clean up screen sessions

Best Practices

Screen Usage Best Practices
  • Always use named sessions for better organization
  • Name your windows descriptively for easy identification
  • Regularly clean up unused sessions with screen -wipe
  • Use configuration file to customize key bindings and behavior
  • Document your screen setup for team members
  • Consider using tmux for more advanced features
Security Considerations
  • Be careful with multi-user sessions and permissions
  • Use password protection for sensitive sessions
  • Regularly audit active sessions
  • Avoid running privileged commands in shared sessions
  • Log out properly to prevent session hijacking

See also