Never Lose Your SSH Session Again: Master GNU Screen on Linux
This guide explains how to install GNU Screen, start and name sessions, manage multiple windows with keyboard shortcuts, detach and reattach safely, and customize settings via .screenrc, ensuring long‑running tasks persist across SSH disconnections.
Introduction
GNU Screen is a terminal multiplexer that lets you run multiple virtual terminals inside a single SSH session; processes continue running even if the connection drops.
Installing Screen
Most Linux distributions ship with Screen pre‑installed. Verify with: screen --version If missing, install it using your package manager:
Ubuntu/Debian: sudo apt install screen CentOS/Fedora:
sudo yum install screenStarting a Screen Session
Launch a new session by typing: screen This creates a window with a shell. View the command list with Ctrl+a ?.
Naming Sessions
Give a session a descriptive name to manage multiple sessions:
screen -S session_nameUsing Screen Windows
Screen starts with one window. Create additional windows with Ctrl+a c. Switch between windows using Ctrl+a 0 … Ctrl+a 9. Common window management shortcuts: Ctrl+a c – create new window Ctrl+a " – list all windows Ctrl+a 0 – switch to window 0 Ctrl+a A – rename current window Ctrl+a S – split region horizontally Ctrl+a | – split region vertically Ctrl+a Tab – move focus to next region Ctrl+a Q – close all regions except current Ctrl+a X – close current region
Nested Session Shortcuts
Ctrl+a "– list all sessions Ctrl+a 0 – switch to session 0 Ctrl+a n – next session Ctrl+a p – previous session Ctrl+a \\ – terminate all sessions and Screen
Detaching and Re‑attaching
Detach from a session without stopping its processes: Ctrl+a d Later, re‑attach with: screen -r If multiple sessions exist, list them with screen -ls and re‑attach a specific one using its ID, e.g., screen -r 15105.
Customizing Screen
Screen reads configuration from /etc/screenrc and ~/.screenrc. Example .screenrc to disable the welcome message, turn off visual bell, increase scrollback, and set a custom status line:
# Disable startup message
startup_message off
# Disable visual bell
vbell off
# Set scrollback buffer size
defscrollback 10000
# Custom status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'Basic Usage Steps
Start Screen: screen Run the desired program inside the window.
Detach safely with Ctrl+a Ctrl+d.
Re‑attach later using screen -r.
Conclusion
You now know how to install and use GNU Screen to create multiple windows, navigate between them, detach and resume sessions, and personalize the environment with a .screenrc file, ensuring your long‑running tasks survive SSH disconnects.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
