DEV Community

Cover image for How to Use the Linux screen Command: Sessions That Survive SSH Drops
FlatlinerAtheist | CLI-NERDS
FlatlinerAtheist | CLI-NERDS

Posted on Originally published at clinerds.com

How to Use the Linux screen Command: Sessions That Survive SSH Drops

You're forty minutes into a database migration on a remote server when your Wi-Fi drops. Without protection, the SSH session dies and takes the migration with it. GNU Screen exists precisely for this: it keeps your terminal session alive on the server, so you can disconnect, reconnect, and pick up exactly where you left off — even from a different machine.

It's installed almost everywhere, or one apt/dnf away:

sudo apt install screen    # Debian/Ubuntu
sudo dnf install screen    # RHEL/CentOS
Enter fullscreen mode Exit fullscreen mode

The workflow that matters

Start a session before any long-running task:

screen -S migration
Enter fullscreen mode Exit fullscreen mode

A fresh shell opens, now inside Screen. Run your long command. Then detach with the key sequence Ctrl+A, then D (press Ctrl+A, release, press D). You're back at your normal prompt, but everything keeps running on the server.

Reattach later — from the same or another machine:

screen -ls            # list sessions
screen -r migration   # reattach by name
Enter fullscreen mode Exit fullscreen mode

Your command, output, and scrollback are all still there. If your connection died instead of a clean detach, screen -d -r migration force-detaches the old session and reattaches.

Multiple windows in one session

Screen can hold many shells in one session, which is great for watching logs while you work:

  • Ctrl+A C — create a new window
  • Ctrl+A N / Ctrl+A P — next / previous window
  • Ctrl+A " — list all windows and jump between them
  • Ctrl+A S — split horizontally, Ctrl+A | split vertically, Ctrl+A Tab switch regions

Every command starts with Ctrl+A (the command prefix), then a single key.

Scrolling and copying

Inside Screen, your normal terminal scrollback doesn't work the way you expect. Enter copy mode with Ctrl+A Esc: arrow keys scroll, Space starts a selection, Enter copies it, and Ctrl+A ] pastes. Press Esc again to leave copy mode.

Screen vs tmux

People always ask. tmux is the newer tool with nicer defaults, a status bar, and saner configuration; screen is the one already installed on ancient production servers. The workflow is identical — sessions, detach, reattach — so learning screen's keybindings means you're never stuck on a minimal box. If you get to choose, try tmux (Ctrl+B prefix); if the server only has screen, you're covered.

The habit worth building

Any command that would hurt if interrupted — upgrades, migrations, big downloads, long builds — runs inside a named screen session. Make it a reflex:

screen -S work
# ... do the risky thing ...
# Ctrl+A D and go home
Enter fullscreen mode Exit fullscreen mode

Your future self, reconnecting from the couch to a still-running job, will be very pleased.

Top comments (0)