How to Use Screen in Linux [List, Attach, Detach, Close]

How to Use Screen in Linux [List, Attach, Detach, Close]

Screen is a terminal multiplexer that allows you to run multiple shell sessions within a single terminal window. Each screen session runs as a separate background process, so commands and scripts continue executing even if the terminal is closed or the SSH connection is lost.

Screen is commonly used for:

  • Running long-running commands on remote servers
  • Managing multiple shells over SSH
  • Preventing accidental termination of critical processes
  • Switching between multiple terminal sessions efficiently

Installing Screen on Linux

Screen is available in the default repositories of most Linux distributions and can be installed using the system package manager.

Install on RHEL / Fedora / Rocky / AlmaLinux

On modern RHEL-based systems:

sudo dnf install screen -y

On older systems:

sudo yum install screen -y

Install on Debian / Ubuntu / Linux Mint

Install screen using APT:

sudo apt install screen -y

On older Debian-based distributions:

sudo apt-get install screen -y

Install on Arch / openSUSE / Gentoo

On Arch Linux:

sudo pacman -S screen

On openSUSE:

sudo zypper install screen

On Gentoo:

sudo emerge -a sys-apps/screen

Getting Started with Screen

Once installed, you can start using screen immediately from the terminal. Screen sessions can be exited or detached depending on whether you want the session to continue running.

Start a Screen Session

Start a new screen session by running:

screen

This opens a new shell inside screen. Any command executed in this session will continue running until the session is explicitly closed.

Exit vs Detach a Screen Session

There is an important difference between exiting and detaching a screen session.

To exit and terminate the screen session:

exit

This stops the screen process and all commands running inside it.

To detach the screen session and keep it running in the background:

Ctrl + a + d

Detaching allows you to safely disconnect from the terminal while the screen session continues running. You can reattach to it later.


Common Screen Keyboard Shortcuts

Below are some commonly used keyboard shortcuts inside a screen session. All shortcuts start with Ctrl + a.

Shortcut Action
Ctrl + a ? Show all key bindings
Ctrl + a c Create a new window
Ctrl + a n Switch to the next window
Ctrl + a p Switch to the previous window
Ctrl + a w List all windows
Ctrl + a " Select a window from a list
Ctrl + a d Detach the current session
Ctrl + a k Kill the current window
Ctrl + a [ Enter scrollback / copy mode

Screen Command Quick Reference Table

Commonly used screen commands at a glance:

Command Description
screen Start a new screen session
screen -ls List all screen sessions
screen -r Attach to a screen session
screen -d Detach a screen session
screen -d -r Detach and reattach a session
screen -S name Start a named session
screen -X -S id quit Kill a screen session
screen -L Start a session with logging enabled

Managing Screen Sessions

List Screen Sessions

To view all currently running screen sessions, use:

screen -ls

or

screen -list

This displays the session ID, session name, and whether it is Attached or Detached.

Attach to a Screen Session

To attach to an existing screen session, specify the session ID or name:

screen -r 9074.pts-1.fedora

If only one detached session exists, you can simply run:

screen -r

Detach a Screen Session

To detach from the current screen session without stopping it, use the keyboard shortcut:

Ctrl + a + d

This keeps the session running in the background and returns you to the normal shell.

You can also detach a session from another terminal using:

screen -d 9074.pts-1.fedora

Reattach a Detached Session

To reattach a previously detached screen session:

screen -r 9074.pts-1.fedora

If the session is attached elsewhere and you want to force reattachment:

screen -d -r 9074.pts-1.fedora

Close (Kill) a Screen Session

To terminate a specific screen session, use the following command:

screen -X -S 9074.pts-1.fedora quit

This immediately stops the screen session and any processes running inside it. Use this carefully to avoid terminating important tasks.


Working with Multiple Windows in Screen

A single screen session can contain multiple windows, each running its own shell or command. This allows you to manage several tasks within one screen session without opening multiple terminals.

Create a New Screen Window

To create a new window inside the current screen session, use the keyboard shortcut:

Ctrl + a + c

This opens a new shell window while keeping the previous one running in the background.

Switch Between Screen Windows

You can switch between windows using the following shortcuts:

Ctrl + a + n    Switch to the next window
Ctrl + a + p    Switch to the previous window

To display a list of all windows:

Ctrl + a + w

To select a window from an interactive list:

Ctrl + a + "

Rename a Screen Window

Renaming windows helps identify what task is running in each window.

To rename the current window:

Ctrl + a + A

Enter a descriptive name, such as the service or script running in that window.

You can also set a window name when creating it:

screen -t build-session

Screen Security and Locking

Screen provides a built-in locking mechanism to prevent unauthorized access to an active session.

Lock a Screen Session

To lock the current screen session, use:

Ctrl + a + x

The session will be locked immediately and prompt for the user password to regain access. This is useful when stepping away from a terminal on a shared or remote system.


Logging and Monitoring

Enable Logging for a Screen Session

To start a screen session with logging enabled, use:

screen -L

This creates a file named screenlog.0 in the directory where the session was started. All terminal output in that session is written to this file.

You can also enable logging for an already running session using:

Ctrl + a + H

This toggles logging on or off for the current window.

View Screen Log Files

To view the contents of the screen log file:

cat screenlog.0

Or follow the log in real time:

tail -f screenlog.0

Log files are plain text and can be archived or shared if needed.


Screen vs tmux (Which Should You Use?)

Both screen and tmux are terminal multiplexers that allow you to run persistent terminal sessions, but they differ in features, development activity, and flexibility.

Use screen when:

  • It is already installed on the system
  • You are working on minimal or legacy servers
  • You need basic session attach/detach functionality
  • You want a simple, no-configuration setup

Use tmux when:

  • You want split panes and advanced layouts
  • You need better scripting and automation support
  • You frequently customize key bindings and status bars
  • You work heavily with SSH and modern development workflows

In general, tmux is more actively maintained and preferred for new setups, while screen remains a reliable choice for quick access and older environments where tmux may not be available.


Summary

The screen command is a powerful utility for managing persistent terminal sessions in Linux. It allows you to detach and reattach sessions, manage multiple windows, enable logging, and secure sessions with locking.

Screen is especially useful for remote administration, long-running tasks, and environments where tmux may not be available.


Also Read


References

Screen Linux man page

Omer Cakmak

Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware.