Linux screen: Terminal Session Manager
• GNU Screen prevents SSH disconnections from killing your long-running processes by maintaining persistent terminal sessions that survive network interruptions and can be reattached from anywhere.
Key Insights
• GNU Screen prevents SSH disconnections from killing your long-running processes by maintaining persistent terminal sessions that survive network interruptions and can be reattached from anywhere. • Screen’s window management lets you run multiple terminal sessions within a single connection, eliminating the need for multiple SSH sessions and reducing resource overhead on remote servers. • Session sharing capabilities make Screen invaluable for pair programming and remote troubleshooting, allowing multiple users to view and control the same terminal session simultaneously.
Introduction & Use Cases
GNU Screen is a terminal multiplexer that solves one of the most frustrating problems in remote server management: losing work when your SSH connection drops. Beyond this fundamental use case, Screen transforms how you interact with terminal sessions by enabling persistent, multi-window environments that outlive individual connections.
Consider this common scenario without Screen:
# SSH into a server
ssh user@production-server
# Start a long-running database migration
./migrate-production-db.sh
# Your WiFi drops after 30 minutes
# Migration fails, database left in inconsistent state
With Screen, the same workflow becomes resilient:
# SSH into server and start a screen session
ssh user@production-server
screen -S db-migration
# Start the migration
./migrate-production-db.sh
# Detach with Ctrl+a d
# Close your laptop, go home, reconnect from anywhere
ssh user@production-server
screen -r db-migration
# Migration still running perfectly
Screen excels in several scenarios: running deployment scripts that take hours, maintaining development environments with multiple processes (web server, database, build watcher), monitoring log files while simultaneously debugging, and enabling remote pair programming sessions. If you regularly work with remote servers, Screen becomes indispensable.
Installation & Basic Commands
Screen is available in default repositories for all major Linux distributions. Installation is straightforward:
# Debian/Ubuntu
sudo apt update
sudo apt install screen
# RHEL/CentOS/Fedora
sudo dnf install screen
# or on older systems
sudo yum install screen
# Arch Linux
sudo pacman -S screen
# Verify installation
screen --version
Starting a Screen session is simple, but naming your sessions makes management far easier:
# Start anonymous session (not recommended)
screen
# Start named session (best practice)
screen -S development
# Start session and immediately run a command
screen -S monitoring top
The most critical Screen command is detaching. Press Ctrl+a (the default command prefix), then d. This detaches your session while keeping everything running. You can now safely close your SSH connection.
Reattaching to sessions:
# List all screen sessions
screen -ls
# Output shows:
# There are screens on:
# 12345.development (Detached)
# 12346.monitoring (Detached)
# Reattach to specific session
screen -r development
# If only one session exists, simply:
screen -r
# Force reattach (if session shows as Attached elsewhere)
screen -r -d development
The -d -r combination detaches the session from wherever it’s currently attached and reattaches it to your current terminal—useful when you forgot to detach before closing your previous SSH connection.
Managing Multiple Windows
Screen’s window management eliminates the need for multiple SSH connections. Within a single Screen session, you can create dozens of windows, each running independent processes.
Create and navigate windows with these commands (remember, Ctrl+a first):
# Inside a screen session:
# Ctrl+a c - Create new window
# Ctrl+a n - Next window
# Ctrl+a p - Previous window
# Ctrl+a 0-9 - Jump to window 0-9
# Ctrl+a " - List all windows (interactive selection)
# Ctrl+a ' - Prompt for window number/name
Practical workflow example:
# Start screen session
screen -S webdev
# Window 0: Run development server
npm run dev
# Ctrl+a c (create new window)
# Window 1: Watch test suite
npm run test:watch
# Ctrl+a c (create new window)
# Window 2: Monitor application logs
tail -f /var/log/app/production.log
# Ctrl+a c (create new window)
# Window 3: Keep a shell available for git commands
Naming windows improves navigation significantly:
# Ctrl+a A (capital A) - Rename current window
# Type: "server" and press Enter
# Now when you press Ctrl+a " you see:
# 0 server npm run dev
# 1 tests npm run test:watch
# 2 logs tail -f production.log
# 3 shell bash
Kill windows you no longer need with Ctrl+a k, then confirm. When you kill the last window, the entire Screen session terminates.
Session Sharing & Collaboration
Screen’s multi-attach capability enables real-time collaboration. Multiple users can connect to the same session simultaneously, seeing identical output and sharing control.
Basic session sharing:
# User 1: Start a shared session
screen -S pairing
# User 2: Attach to the same session
screen -x pairing
Both users now see the same terminal. When one types, the other sees it immediately. This is invaluable for pair programming, remote troubleshooting, or training.
For more controlled sharing with different users, enable multiuser mode:
# Inside screen session, enter command mode with Ctrl+a :
# Then type:
multiuser on
acladd teammate_username
# teammate_username can now attach with:
screen -x your_username/pairing
This approach provides granular control over who can access your sessions, essential in shared server environments.
Split Screen & Advanced Features
Screen supports splitting your terminal into multiple regions, each displaying different windows simultaneously.
# Horizontal split
# Ctrl+a S (capital S)
# Move to new region
# Ctrl+a Tab
# Open a window in new region
# Ctrl+a c
# or switch to existing window: Ctrl+a 0-9
# Vertical split (if supported in your version)
# Ctrl+a |
# Remove current region
# Ctrl+a X (capital X)
# Remove all regions except current
# Ctrl+a Q (capital Q)
Scrollback buffer access is crucial for reviewing command output:
# Enter copy/scrollback mode
# Ctrl+a [
# Now use:
# - Arrow keys or j/k to navigate
# - Ctrl+u / Ctrl+d for page up/down
# - / to search forward, ? to search backward
# - Press Space to start selection, Space again to copy
# - ESC to exit scrollback mode
# Paste copied text
# Ctrl+a ]
Enable session logging to capture everything displayed:
# Toggle logging on/off
# Ctrl+a H (capital H)
# Log file appears as: screenlog.0 in current directory
This creates a complete transcript of your session, useful for documentation or auditing.
Configuration & Customization
Create ~/.screenrc to customize Screen’s behavior:
# ~/.screenrc
# Disable startup message
startup_message off
# Increase scrollback buffer
defscrollback 10000
# Use 256 colors
term screen-256color
# Status line at bottom
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
# Start windows automatically
screen -t shell 0 bash
screen -t editor 1
screen -t logs 2
# Select first window
select 0
# Enable mouse scrolling (if terminal supports it)
termcapinfo xterm* ti@:te@
# Auto-detach on hangup
autodetach on
# Visual bell instead of audio
vbell on
vbell_msg "Bell!"
# Message display time (seconds)
msgwait 2
This configuration creates a productive environment with informative status line, ample scrollback, and pre-configured windows.
Practical Workflow Examples
Deployment workflow:
# Start deployment session
screen -S deploy-v2.4
# Window 0: Run deployment
./deploy-to-production.sh
# Ctrl+a c - Window 1: Monitor application logs
ssh app-server-1 "tail -f /var/log/app/error.log"
# Ctrl+a c - Window 2: Monitor database
mysql -u admin -p -e "SHOW PROCESSLIST;" --auto-vertical-output
# Ctrl+a c - Window 3: Keep shell ready for rollback
# Detach and go to lunch - deployment continues
# Ctrl+a d
Development environment:
screen -S project-alpha
# Window 0: Backend server
cd ~/projects/alpha/backend && npm run dev
# Window 1: Frontend server
cd ~/projects/alpha/frontend && npm start
# Window 2: Database
docker-compose up postgres
# Window 3: Git operations
cd ~/projects/alpha
# Window 4: System monitoring
htop
Long-running data processing:
screen -S data-import
# Enable logging
# Ctrl+a H
# Run import
python import_million_records.py
# Detach and continue other work
# Check progress later by reattaching
screen -r data-import
Screen has been a Unix staple since 1987 for good reason. While newer alternatives like tmux exist, Screen remains ubiquitous, reliable, and perfectly suited for its core mission: ensuring your work persists regardless of connection stability. Master these commands, customize your .screenrc, and you’ll wonder how you ever managed remote servers without it.