GNU Screen
Screen is a terminal multiplexer — it lets a single terminal window run multiple shell sessions, and, crucially, keeps them alive after you disconnect. Start a long job over SSH, detach, close your laptop, reconnect from anywhere, and reattach exactly where you left off. For anyone administering remote servers, it's the difference between a dropped connection killing a six-hour job and simply picking it back up. This guide covers installation, the detach/reattach workflow that makes it indispensable, and the window and split-screen features underneath.
A normal shell is bound to its terminal: close the terminal or drop the SSH link, and every process started from it receives a hangup and dies. Screen breaks that link. It runs a persistent session on the server that your terminal merely attaches to — detaching leaves everything running on the server, untouched.
| Problem | How Screen solves it |
|---|---|
| SSH drops mid-job | The job runs inside Screen on the server, unaffected — reattach and it's still going |
| One terminal, many tasks | Multiple windows inside one session; switch between them with a keystroke |
| Long tasks on a remote box | Start it, detach, log off entirely; check back hours later |
| Sharing a live session | Two people can attach to the same session and see the same screen |
Screen is in every mainstream distribution's default repositories. Pick the line for your package manager.
/usr/bin/screen; the Homebrew build is newer if you need current features. On minimal container images Screen usually isn't present — the one-line install above adds it.Screen listens for commands after a prefix key: Ctrl-a. Every Screen command is that prefix, released, then a second key. Throughout this guide Ctrl-a d means "press Ctrl and a together, let go, then press d."
| Notation | Means |
|---|---|
| Ctrl-a d | Prefix, then d — detach |
| Ctrl-a ? | Prefix, then ? — show the built-in key bindings help |
| Ctrl-a Ctrl-a | Prefix twice — jump to the previous window |
This is the feature you came for. Name your sessions so you can find them again — an unnamed session is fine for one, painful for several.
You're now inside Screen — it looks like an ordinary shell. Start something long-running (a backup, a build, a download), then detach and the session keeps running on the server without you.
screen -r deploy, and you're back as if you never left.screen -d -r deploy detaches it elsewhere and reattaches here. screen -x deploy instead attaches alongside — both terminals share one live view.One session can hold many windows — independent shells you flip between without opening new SSH connections. Each is numbered from 0.
| Keys | Action |
|---|---|
| Ctrl-a c | Create a new window |
| Ctrl-a n | Next window |
| Ctrl-a p | Previous window |
| Ctrl-a 0…9 | Jump straight to window by number |
| Ctrl-a " | Show a selectable list of all windows |
| Ctrl-a A | Rename the current window |
| Ctrl-a w | Show the window list in the status line |
| Ctrl-a k | Kill the current window (asks to confirm) |
Beyond switching windows, Screen can divide the terminal into regions so you see multiple windows at once — log on top, shell below, for instance.
| Keys | Action |
|---|---|
| Ctrl-a S | Split horizontally (top / bottom) — capital S |
| Ctrl-a | | Split vertically (left / right) |
| Ctrl-a Tab | Move focus to the next region |
| Ctrl-a X | Close the focused region (capital X) |
| Ctrl-a Q | Close all regions but the focused one |
Screen keeps its own scrollback buffer (your terminal's scroll wheel won't work as expected inside it). Enter copy mode to scroll back and select text.
| Keys | Action |
|---|---|
| Ctrl-a Esc | Enter copy / scrollback mode (then arrows / PgUp to scroll) |
| Space | In copy mode: start selection, then Space again to copy |
| Ctrl-a ] | Paste the copied text |
| Esc | Leave copy mode |
| Ctrl-a H | Toggle logging the window to a screenlog.n file (capital H) |
Screen reads ~/.screenrc at startup. A few lines make it far friendlier — a persistent status bar and bigger scrollback are the usual first additions.
| Command | Does |
|---|---|
| screen -S name | Start a session with a name |
| screen -ls | List sessions |
| screen -r name | Reattach to a session |
| screen -d -r name | Force-detach elsewhere, then reattach here |
| screen -x name | Attach alongside (shared view) |
| screen -dmS name cmd | Start a named session detached, running cmd (great for scripts/cron) |
| screen -S name -X quit | Kill a named session from outside it |
| Keys / Command | Action |
|---|---|
| Ctrl-a d | Detach (leave running) |
| Ctrl-a c | New window |
| Ctrl-a n / p | Next / previous window |
| Ctrl-a " | Window list |
| Ctrl-a A | Rename window |
| Ctrl-a S / | | Split horizontal / vertical |
| Ctrl-a Tab | Switch region |
| Ctrl-a Esc | Scrollback / copy mode |
| Ctrl-a ? | Key bindings help |
| screen -S name | Start named session |
| screen -ls | List sessions |
| screen -r name | Reattach |
| screen -dmS name cmd | Start detached running a command |
exit / Ctrl-d) until the session closes — that terminates it, unlike detaching.