📒 

Introduction

The screen command is a powerful terminal multiplexer for Linux and Unix-like systems. It allows you to manage multiple shell sessions from a single terminal window, keep sessions running even after logging out, and reconnect to those sessions later. This is particularly useful for remote sessions over SSH, as it ensures that long-running processes do not get interrupted if your connection drops. In this guide, we’ll cover how to install, use, and manage screen sessions, along with some common commands and practical tips.

Why Use screen?

Using screen offers several advantages:

  • Session Management: Run multiple shell sessions in a single terminal window.
  • Session Persistence: Continue running processes even if you disconnect from the terminal or lose your SSH connection.
  • Reattachment: Reconnect to a session at any time and pick up where you left off.
  • Ease of Use: Simple commands and shortcuts make it easy to manage sessions.

Installing screen

screen is usually pre-installed on most Linux distributions. However, if it’s not available on your system, you can install it using your package manager:

  • Debian/Ubuntu:
    sudo apt-get update
    sudo apt-get install screen
  • CentOS/RHEL:
    sudo yum install screen
  • Fedora:
    sudo dnf install screen
  • Arch Linux:
    sudo pacman -S screen

Once installed, you can start using screen immediately.

Starting a screen Session

To start a new screen session, simply type:

screen

This will open a new screen session with a command-line interface that behaves like a normal terminal. You can start running commands or processes within this session.

To start a new session with a specific name (making it easier to manage later), use:

screen -S session_name

Replace session_name with a meaningful name for your session.

Detaching and Reattaching to Sessions

One of the most useful features of screen is the ability to detach from a session and reattach later.

Detaching from a Session

To detach from a screen session without closing it, press:

Ctrl + A, D

The Ctrl + A sequence tells screen that you’re about to give it a command, and D stands for “detach.” After detaching, your session will continue running in the background.

Reattaching to a Session

To reconnect to a detached session, use:

screen -r

If you have multiple sessions, you can list them and choose the specific session to attach to:

screen -ls

This command will display all active screen sessions, including their IDs and names:

There are screens on:
1234.session_name (Detached)
5678.another_session (Detached)
2 Sockets in /var/run/screen/S-user.

To reattach to a specific session, use its ID or name:

screen -r 1234

or

screen -r session_name

Using Multiple Windows in a Single screen Session

screen allows you to create multiple windows within a single session. This means you can run different commands or processes in separate windows and switch between them easily.

Creating a New Window

To create a new window inside an existing screen session, press:

Ctrl + A, C

This will open a new window with a new shell prompt.

Navigating Between Windows

To switch between windows, use:

  • Ctrl + A, N: Move to the next window.
  • Ctrl + A, P: Move to the previous window.
  • Ctrl + A, “: Display a list of open windows and choose one to switch to.

Renaming a Window

To rename a window for better organization, press:

Ctrl + A, A

Type the new name for the window and press Enter.

Closing a Window

To close a window, simply exit the shell running in that window by typing:

exit

or pressing Ctrl + D.

Locking and Unlocking a screen Session

If you need to lock your screen session temporarily, use the following command:

Ctrl + A, X

This will require you to enter your user password to unlock the session.

Customizing screen Configuration

The behavior of screen can be customized using the .screenrc file, which is located in your home directory. You can add commands to this file to change default key bindings, set window titles, and customize the startup behavior of screen.

To create or edit the .screenrc file, use a text editor:

nano ~/.screenrc

Here are a few example settings you might add:

# Change the default escape sequence to Ctrl + B
escape ^Bb

# Set a status line at the bottom of the screen
hardstatus on
hardstatus alwayslastline "%{= kw}%-w%{= BW}%n %t%{-}%+w"

Save the file and restart screen to apply the changes.

Example Use Cases for screen

  • Running Long-Running Processes: If you need to run a script or a command that takes a long time to complete, run it inside a screen session. This way, even if you disconnect from SSH, the process will continue running.
  • Managing Multiple Sessions: If you are working on multiple projects, you can use different screen sessions or windows for each task.
  • Remote Server Management: Use screen to manage remote servers via SSH. This is especially useful for administering servers that require ongoing maintenance or monitoring.

Conclusion

The screen command is a versatile and powerful tool for managing multiple terminal sessions and running long processes on Linux systems. With the ability to detach, reattach, and create multiple windows, screen provides a robust environment for both casual and power users. Whether you’re running complex scripts or just need a way to maintain a persistent shell session, mastering screen will greatly enhance your productivity and flexibility on Linux.