Master GNU Screen: Create, Detach, and Reattach Sessions on Remote Servers
Learn how to use GNU Screen to create named sessions, detach them safely, list active sessions, reattach when needed, and manage multiple windows with keyboard shortcuts, ensuring long-running tasks on remote servers continue uninterrupted even after SSH disconnections.
Introduction
When working on a remote server, an unexpected network drop can abort important scripts or compilation jobs. GNU screen solves this by providing a virtual terminal manager that lets you create multiple independent sessions, detach them, and reattach later, keeping processes alive even after the SSH connection closes.
Core Concepts: Session and Window
Session (Session) – Created when you run screen. It is a container that persists independently of your terminal, allowing processes to continue after disconnection.
Window (Window) – Within a session you can open several windows, each acting like a separate shell, similar to browser tabs, enabling easy task switching.
Creating a New Screen Session
The basic command is simply screen, but for clarity you should name the session using the -S option: dave@gitlab-runner:~$ screen -S compiler This creates a session named compiler. The session runs in the background while the terminal appears unchanged.
Detaching (Detach) the Current Session
To let a task continue after you leave, detach the session with the shortcut Ctrl+a d: [detached from 202352.compiler] The session remains active on the server.
Listing All Running Sessions
After reconnecting, use one of the following commands to see existing sessions: screen -ls or screen -list Example output:
dave@gitlab-runner:~$ screen -ls
There are screens on:
202352.compiler (08/18/25 15:30:00) (Detached)
201098.pts-0.gitlab-runner (08/18/25 12:51:32) (Detached)
2 Sockets in /run/screen/S-dave.The compiler session is shown as Detached.
Reattaching (Reattach) an Existing Session
To return to a detached session, use: screen -r [session_name_or_pid] For example: dave@gitlab-runner:~$ screen -r compiler If only one detached session exists, screen -r without arguments works as well.
Managing Windows Inside a Session
All window‑related shortcuts start with Ctrl+a: Ctrl+a c – Create a new window. Ctrl+a " – Show a list of windows for selection. Ctrl+a n – Switch to the next window. Ctrl+a p – Switch to the previous window. Ctrl+a A – Rename the current window.
Terminating a Screen Session
Inside the session, type exit in each window; when the last window closes, the session ends.
From outside, send a quit command with the -X option:
dave@gitlab-runner:~$ screen -S compiler -X quitUML Flowchart: Common Screen Operations
Visual representations of the workflow:
Conclusion
Although screen offers many options, mastering three core commands— screen -S (create), screen -ls (list), and screen -r (reattach)—combined with the Ctrl+a d shortcut for detaching, solves the majority of everyday use cases. This makes screen a reliable “save point” for long‑running tasks on Linux servers, allowing you to work without fearing network interruptions.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
