How To Use Linux Screen

How To Use Linux Screen

Reading time1 min
#Linux#Terminal#SSH#Screen#Multiplexer#Sysadmin

Mastering Linux Screen: Efficient Multi-Session Terminal Management

Long-running jobs over SSH that evaporate due to a lost Wi-Fi connection. Dozens of local terminal tabs, barely distinguishable, crawling across your workspace. Classic pitfalls for anyone interacting heavily with remote Linux systems. The solution? Screen—a terminal multiplexer that reliably preserves and organizes shell sessions, regardless of network volatility.


What is Linux Screen?

Screen (GNU Screen, latest stable 4.9.1 as of Q2 2024) is a mature terminal multiplexer. It enables:

  • Multiple concurrent shell windows in a single terminal instance
  • Session survivability across SSH disconnects
  • Detach/reattach functionality (resume work exactly where left off)
  • Session/window sharing for real-time collaboration
  • Persistent scrollback for audit or review

Architecturally, Screen creates virtual PTYs, multiplexing user input/output across them. The workflow parallels tmux but with distinct command semantics and a slightly different feature set.


Installation

Most distributions bundle Screen by default. If not, install via package manager:

sudo apt-get install screen         # Debian/Ubuntu
sudo dnf install screen             # Fedora 37+/RHEL 9
sudo yum install screen             # RHEL 7/8, CentOS 7

Verify installation and version:

screen --version
# Output: Screen version 4.09.01 (GNU) 20-Feb-24

Core Usage Patterns

1. Starting and Naming Sessions

To avoid ambiguity when juggling multiple sessions:

screen -S infra-upgrade

This initializes a session labeled infra-upgrade. If you simply type screen, the session receives a numeric process-based identifier.

2. Detaching Sessions

Detachment is the practical backbone:
Ctrl+a then d

This immediately returns control to your original shell while all foreground/background processes in Screen continue—whether it’s a database migration or a tail of /var/log/syslog.

3. Listing & Resuming

Enumerate active sessions:

screen -ls
# There is a screen on:
#     20288.infra-upgrade  (Detached)
# 1 Socket in /run/screen/S-user.

Resume a specific session:

screen -r infra-upgrade

Or, by numeric PID if name is forgotten:

screen -r 20288

4. Exiting & Killing Sessions

Inside any Screen window, exit with:

exit

Or detach then explicitly kill from outside:

screen -X -S infra-upgrade quit

Caution: screen -wipe may be needed to clean up “ghost” sockets if the process was SIGHUPed abnormally.


Multi-Window Workflows

Screen’s window layering removes the need for multiple terminal tabs.

Key PressAction
Ctrl+a, cCreate new window
Ctrl+a, nNext window
Ctrl+a, pPrevious window
Ctrl+a, "Interactive window list
Ctrl+a, [0-9]Direct jump to window by index

Tip: Name each window (Ctrl+a, A), e.g. “app-log”, “db-shell”, for rapid navigation. Your future self will appreciate the clarity after twelve hours at the keyboard.


Visual Multiplexing: Splitting Screens

Screen supports pane splits, though not as visually advanced as tmux. For legacy environments:

  • Horizontal split:
    Ctrl+a then S (uppercase S)

  • Vertical split (if compiled with patch):
    Ctrl+a then |

Switch region focus:
Ctrl+a then Tab

To close a region:
Ctrl+a then X (uppercase X)

Note: Vertical splitting is not enabled in all package builds. If <Ctrl+a> | yields “Unknown command: |”, your distro’s binary lacks this patch—compile from source or use tmux instead.


Long-Running Process Survival

Any command vulnerable to SSH session drops—database restores, large rsync jobs, Jenkins script debugging—benefits from running in Screen. Example:

screen -S batch-update
rsync -azP /mnt/media1/ remote:/backup/
# Detach (Ctrl+a d), later reattach to monitor progress or check output.

Gotcha: Processes started outside screen, then “brought in” with reptyr or disown, behave unpredictably. Always begin critical workloads inside an attached screen session.


Collaborative Session Sharing

For real-time support or pair debugging:

  1. Enable multiuser mode inside your Screen:
    Ctrl + a :
    multiuser on
    acladd opsmate
    
  2. Ensure /run/screen/S-<user>/ is world-accessible or SSH into the same user account from another terminal.

Colleagues can now join using:

screen -x <your-session>

Security note: Be explicit with ACLs. aclchg can revoke access when done.


Customization & Persistent Scrollback

Extend Screen’s utility with a personal .screenrc:

defscrollback 15000    # Increase buffer for deep audit traces
startup_message off    # Skip the banner
caption always "%{= kw}%-w%{=b bw}%n %t%{-}%+w"  # Informative window list

Non-obvious tip: You can remap the default command key if it conflicts with readline or other tools:

escape ^Bb
# Now commands use Ctrl+b as the prefix (as in tmux)

Example: Practical, Not Perfect

Here’s a typical workflow during a system upgrade window, split by function:

  1. Start: screen -S sysupgrade
  2. Windows:
    • “logs” (tail -f /var/log/syslog)
    • “shell-main” (interactive command)
    • “watch-apache” (watch systemctl status apache2)
  3. While monitoring, unexpected network drop
  4. Later, SSH back, screen -r sysupgrade — everything in-place, less still paged, background rsync untouched
  5. Hand off to teammate: enable multiuser, acladd

Downside: Pane splits are modal and lose state after terminal resize events. Full tmux-style layouts aren’t supported. Still, for vanilla setups or legacy systems, Screen remains less intrusive and broadly available.


Conclusion

Screen endures in production environments for good reason: predicting and surviving connectivity interruptions, reducing tab clutter, and enabling real-time handover. Its workflow is fast, its learning curve gentle for the Unix-literate, and its dependence footprint minimal. For ephemeral test servers, persistent build pipelines, or disaster recovery emergencies, Screen deserves a permanent slot in your sysadmin toolbox.

Consider compiling from source if you want bleeding-edge features or patched functionality. For shared persistent sessions in modern dev workflows, weigh tmux as well—some projects standardize on it. But when you SSH to a 2012-era RHEL host and screen is all that’s there, you’ll be glad to have these techniques up front.


If it’s not robust enough for your usage, try tmux. For everyone else, Screen delivers stability without fuss.