Welcome!

Slide to unlock and explore

Slide to unlock

Command Palette

Search for a command to run...

0
Blog
PreviousNext

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 screen

Verify Installation

screen --version

Basic 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.sh

Session Management Keybindings

All commands in screen start with the prefix key (default: Ctrl+A), followed by another key:

CommandKeybindingDescription
HelpCtrl+A then ?Display all keybindings
Create new windowCtrl+A then cOpen a new shell window
Next windowCtrl+A then nSwitch to next window
Previous windowCtrl+A then pSwitch to previous window
Detach sessionCtrl+A then dDisconnect from session
List windowsCtrl+A then "Show all open windows
Rename windowCtrl+A then ARename current window
Kill windowCtrl+A then kClose current window
Split horizontallyCtrl+A then |Split screen side-by-side
Split verticallyCtrl+A then SSplit screen top-bottom
Switch focusCtrl+A then TabMove 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 dev

Example 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 backup

Example 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 pane

Scrollback 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 1

Common 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 deploy

Remote 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 coding

Testing 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 progress

Troubleshooting

Session Already Attached

# If session is already attached elsewhere
screen -r -d session-name
 
# This will detach the other connection and attach you

Lost Session

# Find detached sessions
screen -ls
 
# Reattach to a specific session
screen -r session-id
 
# If only one session exists
screen -r

Screen Not Responding

# Press Ctrl+A then Q to reset
# Or from outside:
screen -X -S session-name quit

Permission Issues

# Check screen permissions
ls -la ~/.screen/S-*
 
# Kill all sessions forcefully
killall screen
 
# Remove screen lockfiles if needed
rm /tmp/screens -rf

Tips & 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.log

3. 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 workspace

screen vs tmux

While screen is older and simpler, you might also consider tmux:

Featurescreentmux
Learning CurveEasierSteeper
ConfigurationSimpleComplex but powerful
Key BindingsDefault Ctrl+ADefault Ctrl+B
Pane ManagementBasicAdvanced
Plugin SupportLimitedExtensive
Active DevelopmentMaintenance modeActive

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 session

Start using screen today and take your terminal productivity to the next level!