The screen command in Linux: persistent terminal sessions

Introduction

The screen command in Linux is an essential tool for system administrators and developers who need to keep terminal sessions active despite disconnections, SSH closures, or temporary reboots. In this article we will explore what screen is, how to install it, create and manage persistent sessions, and some tricks to get the most out of it. By the end you will have practical knowledge that will allow you to work more securely and efficiently in remote environments. Moreover, screen is lightweight, does not require root privileges for basic use, and is available in most distributions such as Ubuntu, Debian, CentOS, and Fedora.

What is screen

Screen is a terminal multiplexer that allows you to create one or several windows within a single command-line session. Each window works like an independent terminal, but they all share the same parent process, which makes administration easier. When you start screen, a session is created that remains active on the server even if you close your SSH client; when you reconnect you can resume exactly where you left off. This feature is particularly useful for long tasks such as compilations, downloads, or log monitoring, as it prevents loss of work due to unexpected interruptions. Moreover, screen allows sharing sessions among multiple users, facilitating real-time collaboration without the need for external tools.

Installing screen

In most Linux distributions, screen is available in the official repositories and its installation is straightforward. On Debian/Ubuntu-based systems, simply run sudo apt update and sudo apt install screen. On Red Hat, CentOS, or Fedora, use dnf install screen or yum install screen depending on the version. After installation, you can verify that the command is available by typing screen –version, which will display the installed version number. No additional configuration steps are required to start using it, although there is an optional configuration file ~/.screenrc for customizing shortcuts and behavior.

Starting a session

To start a new screen session, simply type screen at the prompt. A welcome screen will appear that you can press Enter to skip and go directly to the terminal. From that moment, you are inside a screen session and you can run any command as you would in a normal terminal. If you want to name the session to identify it more easily later, use the command screen -S session_name. For example, screen -S compilacion_java will create a session called compilacion_java that you can later resume with the same identifier. You can also start screen with an activity log by adding -L, which will save everything displayed in the session to a file called screenlog.0 in the current directory, useful for auditing or debugging.

Detaching and reattaching

One of the greatest advantages of screen is the ability to detach (detach) the session without terminating the processes running inside it. To do so, press Ctrl+a followed by d. The session will remain in the background and you can close your SSH client or close the local terminal without losing anything. To reconnect (reattach) to the session, use the command screen -r. If you have several active sessions, screen -r will show a list for you to choose which one to resume, or you can specify the name with screen -r session_name. If the session is detached but another user has it attached, you can use -x to attach in shared mode, allowing both to see the same output in real time.

Managing multiple windows

Within a screen session you can create multiple windows, each with its own shell, similar to browser tabs. To create a new window, press Ctrl+a followed by c. To switch between windows, use Ctrl+a and then the window number (0-9) or Ctrl+a n to go to the next and Ctrl+a p to go to the previous. You can name each window with Ctrl+a A, which makes it easier to identify its purpose, for example, one for code editing, another for log monitoring, and a third for a database console.

Useful shortcuts and configuration

Screen offers a wide set of shortcuts that speed up daily work. Some of the most used are: Ctrl+a ? to show help, Ctrl+a K to kill the current window, Ctrl+a followed by the backslash key to terminate the entire session, and Ctrl+a : to enter command mode where you can execute commands such as resize or focus. Moreover, you can customize behavior via the ~/.screenrc file: define a status bar with hardstatus alwaysvisible, change the escape key (default Ctrl+a) to another combination, or enable automatic logging of all sessions. These adjustments make screen adapt to your workflow and your team’s policies.

  • Ctrl+a ? : shows help
  • Ctrl+a c : creates a new window
  • Ctrl+a d : detaches the session
  • Ctrl+a K : kills the current window
  • Ctrl+a + backslash : terminates the entire session
  • Ctrl+a : : enters command mode

Conclusion and best practices

In summary, screen is a lightweight and powerful solution for keeping terminal sessions persistent in Linux, especially valuable when working via SSH or on remote servers. Its features of detachment, multiple windows, and flexible configuration make it an indispensable tool for administrators, developers, and anyone who needs to avoid work loss due to interruptions. Remember to always name your sessions, use activity logging when needed, and periodically review the ~/.screenrc file to maintain a clean and secure environment. With this knowledge, you can improve your productivity and the reliability of your command-line tasks.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .