Mastering the Linux screen Command - Ultimate Guide
Learn how to use the Linux screen command for terminal multiplexing, managing multiple sessions, and keeping processes running remotely.
Introduction
The screen command is a terminal multiplexer that allows you to run multiple shell sessions within a single window and manage them from the command line. Whether you're working on a remote server, running long-running processes, or organizing multiple tasks, screen is an essential tool for Linux developers and system administrators.
Think of screen as a window manager for your terminal - it lets you create multiple virtual terminals within one physical terminal session, detach from them without losing your work, and reattach later.
Why Use screen?
Key Benefits
- Persistent Sessions: Keep processes running even after you disconnect from SSH
- Terminal Multiplexing: Run multiple shells/programs simultaneously
- Detach & Reattach: Disconnect and reconnect without losing work
- Window Management: Organize work across multiple virtual terminals
- Server Independence: Perfect for remote work and long-running tasks
- Session Sharing: Share terminal sessions with other users
Installation
Linux
Most Linux distributions come with screen pre-installed. If not:
# Ubuntu/Debian
sudo apt-get install screen
# Fedora/CentOS
sudo yum install screen
# macOS
brew install screenVerify Installation
screen --versionBasic Usage
Starting a New Session
# Start a basic session
screen
# Start with a named session
screen -S my-session
# Start session with a specific command
screen -S backup -c /path/to/backup/script.shSession Management Keybindings
All commands in screen start with the prefix key (default: Ctrl+A), followed by another key:
| Command | Keybinding | Description |
|---|---|---|
| Help | Ctrl+A then ? | Display all keybindings |
| Create new window | Ctrl+A then c | Open a new shell window |
| Next window | Ctrl+A then n | Switch to next window |
| Previous window | Ctrl+A then p | Switch to previous window |
| Detach session | Ctrl+A then d | Disconnect from session |
| List windows | Ctrl+A then " | Show all open windows |
| Rename window | Ctrl+A then A | Rename current window |
| Kill window | Ctrl+A then k | Close current window |
| Split horizontally | Ctrl+A then | | Split screen side-by-side |
| Split vertically | Ctrl+A then S | Split screen top-bottom |
| Switch focus | Ctrl+A then Tab | Move to next pane |
Practical Examples
Example 1: Remote Server Development
# SSH into server
ssh user@remote-server
# Create a named development session
screen -S dev
# Inside screen, navigate to your project
cd /var/www/myapp
# Start your development server
npm run dev
# Press Ctrl+A then D to detach
# Now you can close SSH without stopping the server
# Later, reconnect and reattach
screen -r devExample 2: Long-Running Backup Process
# Create a backup session
screen -S backup
# Start your backup process
tar -czf backup.tar.gz /important/data
# Detach with Ctrl+A then D
# Check status from another terminal
screen -ls
# Reattach when ready
screen -r backupExample 3: Multiple Development Tasks
# Create main session
screen -S work
# Press Ctrl+A then C to create new windows
# Window 0: Text editor (vim)
vim main.py
# Press Ctrl+A then C again
# Window 1: Git commands
git status
# Press Ctrl+A then C again
# Window 2: Test runner
pytest tests/
# Switch between windows with Ctrl+A then N/P
# List windows with Ctrl+A then "Advanced Features
Session Naming and Organization
# List all sessions
screen -ls
# Create multiple related sessions
screen -S frontend
screen -S backend
screen -S database
# List sessions with names
screen -ls
# Output:
# There are screens on:
# 12345.backend (11/18/25 10:30:45) (Detached)
# 12346.frontend (11/18/25 10:31:20) (Detached)
# 12347.database (11/18/25 10:32:15) (Detached)Split Screen Management
# Inside screen session:
# Ctrl+A then S - split horizontally
# Ctrl+A then | - split vertically
# Ctrl+A then Tab - switch focus between panes
# Ctrl+A then X - close current paneScrollback Buffer
# Enter copy mode (scroll mode)
# Ctrl+A then [
# Navigate with arrow keys or Page Up/Down
# Press Space to start selection, then Space again to copy
# Exit copy mode
# Press Escape
# Paste copied text
# Ctrl+A then ]Screen Configuration (.screenrc)
Create ~/.screenrc for persistent settings:
# Set default shell
shell -$SHELL
# Set scrollback buffer
defscrollback 10000
# Enable visual bell
vbell on
# Set status bar
hardstatus alwayslastline "%{= b}%H%{= u} %?%-Lw%?%{= B}%n*%f %t%?%?%{= u}%+Lw%? %{= b}| %{= u}%D %c"
# Start with numbered windows
screen -t editor 0
screen -t shell 1Common Workflows
Deploy with screen
# Create deployment session
screen -S deploy
# Run deployment script
./deploy.sh
# Monitor progress, then detach with Ctrl+A then D
# Check logs from anywhere
screen -r deployRemote Development
# Local machine
ssh dev@server
screen -S coding
# On server - run IDE/editor
code .
# or
vim
# Detach and close SSH
# Ctrl+A then D, then exit SSH
# Later, reconnect
ssh dev@server
screen -r codingTesting Marathon
# Create test session
screen -S testing
# Window 1: Run unit tests
npm test
# Ctrl+A then C
# Window 2: Check test coverage
npm run coverage
# Ctrl+A then C
# Window 3: Run integration tests
npm run test:integration
# Switch between windows to monitor progressTroubleshooting
Session Already Attached
# If session is already attached elsewhere
screen -r -d session-name
# This will detach the other connection and attach youLost Session
# Find detached sessions
screen -ls
# Reattach to a specific session
screen -r session-id
# If only one session exists
screen -rScreen Not Responding
# Press Ctrl+A then Q to reset
# Or from outside:
screen -X -S session-name quitPermission Issues
# Check screen permissions
ls -la ~/.screen/S-*
# Kill all sessions forcefully
killall screen
# Remove screen lockfiles if needed
rm /tmp/screens -rfTips & Tricks
1. Auto-Run Commands on Attach
screen -S work -X stuff "cd ~/projects && npm run dev$(echo -ne '\015')"2. Capture Session to File
# Inside screen: Ctrl+A then H
# Automatically logs to screenlog.0
# Or manually:
screen -S session -X hardcopy filename.log3. Send Commands to Detached Session
# From outside, send command to detached session
screen -S session-name -X stuff "npm test$(echo -ne '\015')"4. Create Session Template
#!/bin/bash
# setup-workspace.sh
screen -d -m -S workspace
# Create multiple windows
screen -S workspace -X screen -t editor
screen -S workspace -X screen -t git
screen -S workspace -X screen -t deploy
# Run commands in each window
screen -S workspace -p editor -X stuff "vim$(echo -ne '\015')"
screen -S workspace -p git -X stuff "cd ~/project$(echo -ne '\015')"
screen -S workspace -p deploy -X stuff "ssh user@server$(echo -ne '\015')"
# Attach to workspace
screen -r workspacescreen vs tmux
While screen is older and simpler, you might also consider tmux:
| Feature | screen | tmux |
|---|---|---|
| Learning Curve | Easier | Steeper |
| Configuration | Simple | Complex but powerful |
| Key Bindings | Default Ctrl+A | Default Ctrl+B |
| Pane Management | Basic | Advanced |
| Plugin Support | Limited | Extensive |
| Active Development | Maintenance mode | Active |
Both are excellent. screen is lighter and great for simple tasks, while tmux offers more advanced features.
Conclusion
The screen command is an indispensable tool for:
- Remote server management
- Running persistent background jobs
- Organizing multiple development tasks
- Collaboration and session sharing
- Learning Linux terminal multiplexing
Master screen and you'll significantly improve your workflow efficiency, especially when working with remote servers. Start with the basics, gradually explore advanced features, and customize it to match your workflow.
Quick Reference
# Create/Attach
screen -S name # Create new session
screen -r name # Reattach to session
screen -ls # List all sessions
# Inside screen (Ctrl+A prefix)
c - Create new window
n - Next window
p - Previous window
" - List windows
A - Rename window
k - Kill window
d - Detach session
[ - Enter copy mode
] - Paste
# From command line
screen -S name -X stuff "command\015" # Send command
screen -X -S name quit # Kill sessionStart using screen today and take your terminal productivity to the next level!