Complete GNU Screen Command Cheatsheet for Linux System Administrators
GNU Screen is a powerful terminal multiplexer that allows you to create, manage, and navigate between multiple terminal sessions within a single SSH connection. This comprehensive guide provides everything you need to master Screen for efficient Linux system administration.
What is GNU Screen?
GNU Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. It enables you to:
- Run multiple terminal sessions simultaneously
- Detach and reattach sessions without losing running processes
- Share terminal sessions between multiple users
- Maintain persistent sessions across network disconnections
- Split terminal windows horizontally and vertically
Screen is particularly valuable for system administrators managing remote servers, as it prevents losing work when SSH connections drop unexpectedly.
Screen vs. tmux
While both Screen and tmux are terminal multiplexers, Screen is older and more widely available by default on most Linux distributions. If you're new to terminal multiplexers or working on systems where tmux isn't available, Screen is an excellent choice.
Installing GNU Screen
Most Linux distributions include Screen by default. If it's not installed on your system, use the following commands:
Ubuntu/Debian Systems
sudo apt update
sudo apt install screen -y
RHEL/CentOS/AlmaLinux/Rocky Linux
# For RHEL 8+ and derivatives
sudo dnf install screen -y
# For older CentOS 7 systems
sudo yum install screen -y
Arch Linux
sudo pacman -S screen
Verify Installation
screen --version
Essential Screen Commands
Starting Screen Sessions
Create a New Session
# Start a new unnamed session
screen
# Start a named session (recommended)
screen -S session_name
# Start a session with a specific command
screen -S backup_session -dm bash -c 'rsync -av /home/ /backup/'
Session Management Examples
# Start a web server monitoring session
screen -S webserver
# Start a database maintenance session
screen -S db_maintenance
# Start a log monitoring session
screen -S logs
Detaching and Reattaching Sessions
Detach from Current Session
- Keyboard shortcut:
Ctrl + A
, thenD
- Command:
screen -d
(detaches the current session)
Reattach to Sessions
# Reattach to the most recent session
screen -r
# Reattach to a specific session by name
screen -r session_name
# Reattach to a session by PID
screen -r 12345
# Force reattach (if session is attached elsewhere)
screen -dr session_name
# Reattach and share session (multiuser mode)
screen -x session_name
Listing and Managing Sessions
List All Sessions
# List all screen sessions
screen -ls
# Alternative command
screen -list
Example output:
There are screens on:
12345.webserver (Detached)
12346.db_maintenance (Attached)
12347.logs (Detached)
3 Sockets in /var/run/screen/S-username.
Kill Sessions
# Kill a specific session
screen -S session_name -X quit
# Kill all detached sessions
screen -wipe
Complete Keyboard Shortcuts Reference
All Screen commands begin with the escape sequence Ctrl + A
. After pressing Ctrl + A
, release both keys and press the command key.
Session and Window Management
Shortcut | Action | Description |
---|---|---|
Ctrl + A , D |
Detach | Detach from current session |
Ctrl + A , C |
Create | Create a new window |
Ctrl + A , N |
Next | Switch to next window |
Ctrl + A , P |
Previous | Switch to previous window |
Ctrl + A , 0-9 |
Select | Switch to window number 0-9 |
Ctrl + A , " |
List | Show window list for selection |
Ctrl + A , ' |
Select | Prompt for window number/name |
Ctrl + A , K |
Kill | Kill current window |
Ctrl + A , \ |
Quit | Kill all windows and terminate session |
Window Splitting and Navigation
Shortcut | Action | Description |
---|---|---|
Ctrl + A , S |
Split Horizontal | Split current window horizontally |
Ctrl + A , ` |
` | Split Vertical |
Ctrl + A , Tab |
Focus Next | Move focus to next split region |
Ctrl + A , Q |
Unsplit | Remove all splits except current |
Ctrl + A , X |
Remove Split | Remove current split region |
Information and Help
Shortcut | Action | Description |
---|---|---|
Ctrl + A , ? |
Help | Show key bindings |
Ctrl + A , I |
Info | Show window information |
Ctrl + A , T |
Time | Show system time |
Ctrl + A , V |
Version | Show Screen version |
Ctrl + A , W |
Windows | Show window list in status line |
Copy and Scroll Mode
Shortcut | Action | Description |
---|---|---|
Ctrl + A , [ |
Copy Mode | Enter copy/scroll mode |
Ctrl + A , Esc |
Copy Mode | Alternative to enter copy mode |
Space |
Mark Start | Start text selection (in copy mode) |
Space |
Mark End | End text selection (in copy mode) |
Ctrl + A , ] |
Paste | Paste copied text |
Advanced Commands
Shortcut | Action | Description |
---|---|---|
Ctrl + A , : |
Command | Enter command mode |
Ctrl + A , A |
Rename | Rename current window |
Ctrl + A , H |
Log | Toggle logging for current window |
Ctrl + A , M |
Monitor | Monitor window for activity |
Ctrl + A , _ |
Silence | Monitor window for silence |
Advanced Screen Techniques
Configuration File (.screenrc)
Create a ~/.screenrc
file to customize Screen behavior:
# ~/.screenrc configuration example
# Set default shell
shell -$SHELL
# Disable startup message
startup_message off
# Set scrollback buffer
defscrollback 10000
# Enable mouse scrolling
termcapinfo xterm* ti@:te@
# Status line configuration
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}]'
# Window titles
shelltitle "$ |bash"
# Key bindings
bind ^k
bind ^j
bind ^\
bind \\
# Split navigation
bind j focus down
bind k focus up
bind h focus left
bind l focus right
# Window management
bind = resize =
bind + resize +1
bind - resize -1
Logging and Monitoring
Enable Logging
# Start logging for current window
Ctrl + A, H
# Or use command mode
Ctrl + A, :
log on
Monitor Window Activity
# Monitor for activity
Ctrl + A, M
# Monitor for silence (30 seconds default)
Ctrl + A, _
Multiuser Sessions
Enable Multiuser Mode
# In Screen command mode (Ctrl + A, :)
multiuser on
acladd username
Share Session
# Other user can attach with:
screen -x original_user/session_name
Practical Use Cases and Examples
1. Long-Running Tasks
# Start a backup session
screen -S backup
rsync -av --progress /home/ /backup/
# Detach with Ctrl + A, D
2. Server Monitoring
# Create monitoring session with multiple windows
screen -S monitoring
# Window 0: System resources
htop
# Ctrl + A, C (new window)
# Window 1: Log monitoring
tail -f /var/log/syslog
# Ctrl + A, C (new window)
# Window 2: Network monitoring
iftop
3. Development Environment
# Start development session
screen -S development
# Window 0: Code editor
vim project.py
# Ctrl + A, C
# Window 1: Testing
python -m pytest
# Ctrl + A, C
# Window 2: Server
python manage.py runserver
4. Split Screen Setup
# Start session and split horizontally
screen -S workspace
# Ctrl + A, S (split horizontal)
# Ctrl + A, Tab (move to new region)
# Ctrl + A, C (create window in new region)
Troubleshooting Common Issues
Session Not Found
# If screen -r fails, list sessions first
screen -ls
# Force reattach if session appears attached
screen -dr session_name
Permission Denied
# Check screen directory permissions
ls -la /var/run/screen/
# Clean up dead sessions
screen -wipe
Terminal Size Issues
# Resize screen to current terminal size
Ctrl + A, :
fit
Escape Key Conflicts
# Change escape key in .screenrc
escape ^Bb # Changes escape to Ctrl + B
Screen vs. Modern Alternatives
Feature | GNU Screen | tmux | Advantages |
---|---|---|---|
Availability | Pre-installed on most systems | Requires installation | Screen: Universal availability |
Configuration | Simple .screenrc | Advanced tmux.conf | tmux: More flexible |
Vertical Splits | Limited support | Native support | tmux: Better window management |
Scripting | Basic | Advanced | tmux: Better automation |
Learning Curve | Gentle | Steeper | Screen: Easier to start |
Best Practices
1. Use Descriptive Session Names
# Good
screen -S web_deployment
screen -S database_backup
screen -S log_analysis
# Avoid
screen -S session1
screen -S temp
2. Regular Session Cleanup
# Weekly cleanup of dead sessions
screen -wipe
# List and review active sessions
screen -ls
3. Configure Status Line
Add a status line to your .screenrc
for better session awareness:
hardstatus alwayslastline "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= @%H - %LD %d %LM - %c"
4. Use Logging for Important Sessions
# Enable automatic logging for critical sessions
logfile /var/log/screen/screen-%S-%n.log
log on
Conclusion
GNU Screen is an essential tool for Linux system administrators and developers who work with remote servers. Its ability to maintain persistent sessions, manage multiple terminals, and recover from network disconnections makes it invaluable for production environments.
While newer alternatives like tmux offer more advanced features, Screen's simplicity, universal availability, and proven reliability make it an excellent choice for both beginners and experienced users. Master these commands and shortcuts to significantly improve your terminal productivity and remote server management capabilities.
Start with basic session management, gradually incorporate keyboard shortcuts into your workflow, and customize your .screenrc
configuration to match your specific needs. With practice, Screen will become an indispensable part of your Linux toolkit.