Blog » Linux » How to Use screen Command in Linux: Keep Your Sessions Running
› how-to-use-screen-command-in-linux

How to Use screen Command in Linux: Keep Your Sessions Running

Table of Contents

If you’ve ever lost a long-running process because your SSH connection dropped, you already know why learning how to use screen command in Linux matters. I learned this lesson the hard way back when I was running a four-hour database migration on a remote server. My coffee shop’s WiFi hiccuped, my terminal closed, and hours of work vanished. That was the day I discovered screen.

Screen is a terminal productivity tool that lets you create persistent terminal sessions. These sessions keep running even when you disconnect. Whether you’re a sysadmin managing remote servers or a developer running long builds, screen will save you from connection disasters.

What is the screen Command (And Why Every Sysadmin Needs It)

Screen is a terminal multiplexer. That’s a fancy way of saying it lets you run multiple terminal windows inside a single session and keeps everything running in the background. You can detach from a session, close your laptop, drive home, reconnect from your couch, and pick up right where you left off.

The key benefits that make screen essential:

  • Session persistence: Your processes keep running even if you disconnect
  • Multiple windows: Run several terminals inside one session
  • Detach and reattach: Leave work running and come back later
  • Split screens: View multiple programs side by side

You might have heard of tmux as an alternative. Both are valid choices in 2025. I keep screen in my toolkit because it’s everywhere. Nearly every Linux server I SSH into already has screen installed. When you’re troubleshooting SSH connection issues at 2 AM, you want tools that just work.

RackNerd Mobile Leaderboard Banner

Get a VPS from as low as $11/year! WOW!

Installing screen on Linux

Good news: screen probably came with your system. Let’s check first, then install if needed.

Check if screen is Already Installed

Open your terminal and run:

screen --version

If you see a version number like “Screen version 4.09.00”, you’re ready to go. If not, install it with the commands below.

Install screen on Ubuntu/Debian

sudo apt update
sudo apt install screen

Install screen on RHEL/CentOS/Fedora

sudo dnf install screen

For older CentOS versions, use yum instead of dnf.

Install screen on Arch Linux

sudo pacman -S screen

Starting Your First screen Session

The simplest way to start screen is just typing screen and pressing Enter. You’ll see a welcome message, then land in what looks like a normal terminal. But this terminal is now protected inside a screen session.

Creating a Named Session

I always use named sessions. When you’re managing multiple servers or projects, generic session names like “12345.pts-0” become useless fast. Name your sessions something meaningful:

screen -S deployment

This creates a session called “deployment” that you can easily find and reattach to later.

Understanding the screen Interface

Here’s the key thing to know: all screen commands start with Ctrl-a. This is called the escape key prefix. You press Ctrl-a first, release it, then press the second key.

Quick Tip: New to screen? The escape key is Ctrl-a. Press it, release, then press the command key. It takes about an hour to feel natural.

Detaching and Reattaching Sessions

This is where screen becomes magic. You can walk away from a running process, and it keeps going.

How to Detach from a Session

To detach from your current session, press:

Ctrl-a d

You’ll see “[detached from session-name]” and return to your normal terminal. Your screen session keeps running in the background. This is exactly what saves you when SSH connections drop. If you were already setting up SSH keys for remote access, adding screen to your workflow is the natural next step.

Listing All screen Sessions

To see all your running sessions:

screen -ls

Output looks like this:

There are screens on:
    12345.deployment    (Detached)
    12346.backup        (Attached)
2 Sockets in /run/screen/S-username.

Reattaching to a Session

To jump back into a session:

screen -r deployment

You’re back where you left off. Any output that happened while you were gone is waiting for you.

Force Reattaching When a Session is Already Attached

Sometimes a session shows “(Attached)” but you can’t access it. This happens when your SSH connection died but the server doesn’t know yet. The fix is force reattach:

screen -d -r deployment

This forcibly detaches the session from wherever it thinks it’s attached, then reattaches it to your current terminal. I use this command weekly when securing your SSH configuration doesn’t prevent those occasional network hiccups.

Working with Multiple Windows

A single screen session can hold many windows. I typically have one window for my main work, another tailing logs, and a third running htop.

Creating New Windows

Ctrl-a c

This creates a new window inside your current session.

Switching Between Windows

  • Next window: Ctrl-a n
  • Previous window: Ctrl-a p
  • Jump to specific window: Ctrl-a [0-9]
  • List all windows: Ctrl-a “

The window list (Ctrl-a “) is my favorite. It shows all windows with numbers and names, and you can arrow through them.

Naming and Renaming Windows

Ctrl-a A

This lets you rename the current window. I rename windows immediately after creating them. “bash” tells you nothing. “logs” or “deploy” tells you everything.

Closing Windows

Just type exit or press Ctrl-d to close the current window. You can also use Ctrl-a k to kill a window, but screen will ask for confirmation first.

Split Screen Functionality

Screen can split your terminal into regions. I use this when monitoring logs while running commands.

Horizontal and Vertical Splits

  • Horizontal split: Ctrl-a S (capital S)
  • Vertical split: Ctrl-a | (pipe character)

After splitting, the new region is empty. Press Ctrl-a Tab to move to it, then Ctrl-a c to create a window there.

Navigating Between Regions

Ctrl-a Tab

This cycles through your split regions.

Removing Splits

  • Close current region: Ctrl-a X (capital X)
  • Close all but current: Ctrl-a Q (capital Q)

Closing a region doesn’t kill the window inside it. The window keeps running. You just removed the view.

Customizing screen with .screenrc

The default screen behavior is fine, but a few tweaks make it much better. Configuration lives in ~/.screenrc.

Essential .screenrc Settings

Here’s my minimal config that I drop on every server:

# Skip the startup message
startup_message off

# Bigger scrollback buffer (default is 100 lines)
defscrollback 10000

# Enable 256 colors
term screen-256color

# Visual bell instead of audible
vbell on

Creating a Custom Status Line

A status line at the bottom shows which window you’re in and what else is running. Add this to your .screenrc:

hardstatus alwayslastline
hardstatus string '%{= kG}[%{G}%H%{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c%{g}]'

This looks cryptic, but it gives you the hostname, window list, and current time. The official GNU Screen manual documents all the hardstatus escape codes if you want to customize further.

Common Problems and Solutions

Screen is reliable, but these issues come up regularly.

Session Shows as ‘Attached’ But You Can’t Access It

This happens after network disconnects. Force reattach with:

screen -d -r sessionname

If that doesn’t work, you might need to check if the process is truly running using the ps command.

Screen Version Incompatibility After Update

Sometimes after a system update, old sessions won’t reattach because the screen binary version changed. You’ll see errors about socket compatibility. Unfortunately, these sessions are usually lost. The fix is to not update screen while sessions are running. Learned that one the hard way.

Lost Sessions After Reboot

Screen sessions don’t survive reboots. This is by design. When the system shuts down, all processes end, including screen sessions. If you need persistent services, look into managing services with systemctl instead.

Cannot Open Terminal Error

If you see “Cannot open your terminal ‘/dev/pts/X'” errors, the TERM variable might be wrong. Fix it with:

export TERM=screen-256color

Also, run screen -wipe to clean up dead sessions that might be causing issues.

screen vs tmux: Which Should You Use?

Both screen and tmux are terminal multiplexers. Tmux is newer with more active development and features. Screen has been around since 1987 and is stable as a rock.

“In many sysadmin circles, screen remains the terminal multiplexer of choice by a wide margin for its simplicity and ubiquity.”

My take: learn screen first because it’s already installed almost everywhere. When you SSH into a random server, screen is probably there. Tmux might not be. Once you’re comfortable with screen, exploring tmux for your personal machines makes sense. The concepts transfer directly.

Real-World Use Cases for screen

Here’s how I actually use screen daily:

  • Deployment scripts: Start the deploy in screen, detach, go get coffee. If anything fails, all the output is waiting for me.
  • Long builds: Compiling from source takes hours sometimes. Screen keeps it safe.
  • Database migrations: Never run migrations without screen. Ever. I learned this after losing that four-hour migration I mentioned earlier.
  • Log monitoring: Split screen with logs tailing on one side, work on the other.
  • Shared troubleshooting: Use screen -x to let a colleague attach to your session and see what you see. Perfect for pair debugging.

For killing processes that go rogue inside screen, all the normal methods work. Screen just keeps the session alive.

Quick Reference: Essential screen Commands

Action Command
Start new session screen
Start named session screen -S name
List sessions screen -ls
Reattach to session screen -r name
Force reattach screen -d -r name
Detach Ctrl-a d
Create window Ctrl-a c
Next/Previous window Ctrl-a n / Ctrl-a p
List windows Ctrl-a "
Horizontal split Ctrl-a S
Vertical split Ctrl-a |
Kill window Ctrl-a k

Start Using screen Today

The screen command has been protecting sysadmins from connection disasters since 1987. It’s simple, reliable, and everywhere. Start with named sessions (screen -S name), learn the detach command (Ctrl-a d), and you’re already ahead of most Linux users.

For more ways to level up your terminal game, check out our guide to terminal productivity tools. And if you’re doing remote work, make sure your SSH configuration is locked down before you start those long-running sessions.

For even more advanced screen tricks, check out Red Hat’s tips for using screen.

author avatar
Alexa Velinxs
I'm Alexa Velinxs, a cryptocurrency trading expert passionate about demystifying digital assets for both beginners and seasoned investors. Through my writing, I share actionable strategies, market insights, and practical tips to help you navigate the crypto landscape with confidence. Let's explore the future of finance together.
Related Posts