HomeLinuxLinux Screen Command: Installation, Usage, Examples

Linux Screen Command: Installation, Usage, Examples

Are you tired of losing your work when your SSH session abruptly ends or due to network disconnections? Do you find it difficult to manage multiple terminal sessions on your Linux machine? Look no further, because the screen command in Linux is here to rescue you!

In this article, we will explore the installation, usage, and examples of the screen command, a powerful tool that allows you to create and manage multiple terminal sessions within a single SSH session. With the screen, you can detach and reattach sessions, keeping them running in the background even if your connection is lost. This means you can pick up right where you left off, saving you time and frustration.

Mastering the screen command is a valuable skill that can greatly improve your productivity. We will guide you through the installation process, show you how to create and manage sessions and provide practical examples to help you leverage the full potential of the screen.

Installation of the Screen Command in Linux

The first step to harnessing the power of the screen command in Linux is to ensure that it is installed on your system. Fortunately, most Linux distributions come pre-installed with the screen command, making it readily available for immediate use. However, if you find that the screen command is not installed on your system, fret not, as installing it is a straightforward process. You can typically install the screen command using your distribution’s package manager, such as apt-get for Debian-based systems or yum for Red Hat-based systems. Simply run the appropriate command with administrative privileges, and you’ll have the screen command up and running in no time.

To install Screen on Ubuntu and other Debian-based distributions, execute:

sudo apt-get update
sudo apt-get install screen

To install Screen on Red Hat-based distributions like CentOS, execute:

sudo yum install screen

To install Screen on Arch-based distributions, execute:

sudo pacman -S screen

To install Screen on Alpine Linux, execute:

sudo apk add screen

To install Screen on OpenSUSE, execute:

sudo zypper install screen

Once successfully installed, you can verify the screen utility installation by executing the command:

screen --version

Basic Usage of the Screen Command in Linux

Once you have successfully installed the screen command, it’s time to dive into the basics of using this versatile tool. The screen command operates by creating virtual screens within a single SSH session, allowing you to multitask and manage multiple processes simultaneously. To start a new screen session, simply type screen followed by pressing Enter. You will be greeted with a new terminal window, similar to starting a new terminal session. From here, you can run commands, execute scripts, or perform any other terminal-based task just as you would in a regular terminal session.

screen

To detach from the screen session and leave it running in the background, press Ctrl + A followed by Ctrl + D. This allows you to continue working in your main terminal session while the screen session remains active in the background.

If you must close the screen completely, just type “exit” and click enter. This will close the utility.

Screen Command Options and Arguments

The screen command can be used with options and arguments like any other Linux command. The syntax for the screen command is as follows, along with options and arguments:

screen [-opts] [cmd [args]]
CategoryCommand/Key BindingDescription
Starting a SessionscreenStarts a new screen session.
screen -S session_nameStarts a new screen session with a specified name.
Detaching & Reattachingscreen -dDetaches the currently attached screen session.
screen -rReattaches to a detached screen session.
screen -d -rDetaches the session from elsewhere and reattaches it.
screen -lsLists all active screen sessions.
screen -wipeLists all sessions and removes dead ones.
Window ManagementCtrl-a cCreates a new window.
Ctrl-a nSwitches to the next window.
Ctrl-a pSwitches to the previous window
Ctrl-a dDetaches the current screen session.
Ctrl-a kKills the current window.
Ctrl-a wLists all windows.
Named WindowsCtrl-a AAllows you to name the current window.
Logging & MonitoringCtrl-a HStarts/stops logging of the current window to screenlog.0.
Ctrl-a _Sets a hardstatus line at the bottom of the screen.
Multiuser Modescreen -S session_name -X multiuser onEnables multiuser mode for the session.
screen -S session_name -X acladd usernameAdds a user to the session’s ACL (Access Control List).
Custom Configurationscreen -c fileUses a custom configuration file instead of ~/.screenrc.
Command Executionscreen commandRuns a command in a new screen window.
screen -S session_name commandRuns a command in a specific screen session.
This table provides a quick reference to the most commonly used options and key bindings for the screen command in Linux.

Starting a Screen Session

Q: How do you start a new screen session?
Answer: Use the command screen to start a new screen session.

screen

You’ll be greeted with a new screen session. It will look similar to your usual command line, but now you’re in a screen session. As we do not specify any name for the session we opened via screen, the screen utility will give a custom name for it automatically to identify and differentiate from other sessions.

Q: How do you start a new screen session with a specified name?
A: Use the command screen -S session_name to start a new screen session with a specified name.

screen -S session_name

Replace session_name with your desired name for the screen session. For example, if you want to name your session “mysession,” you would use: screen -S mysession

Detaching & Reattaching Screen

With GNU Screen, users may easily manage ongoing tasks and sessions by detaching and reattaching. Users can disconnect from the terminal session and leave processes running in the background by detaching, which enables them to log out or move to another activity without interfering with their work. Later reattachment brings the session back to its original state, providing continuity and adaptability in the management of long-running procedures, remote sessions, or jobs that necessitate continuous observation or communication. Productivity greatly depends on this feature, particularly in settings where sessions must be kept open despite disruptions or accessible from many places.

Q: How do you list all active screen sessions?
A: Use the command screen -ls to list all active screen sessions.

screen -ls
To find the session ID or name, you can list all running screen sessions with: screen -ls
This will show you all the screen sessions you have, along with their IDs and names, allowing you to use screen -d more effectively.

Q: How do you detach the currently attached screen session?
A: Use the command screen -d to detach the currently attached screen session.

screen -d session_name

OR

screen -d session_ID

Q: How do you reattach to a detached screen session?
A: Use the command screen -r to reattach to a detached screen session.

screen -r session_name

OR

screen -r session_ID

This will bring you back to the screen session and allow you to continue where you left off.

Q: How do you detach a session from elsewhere and reattach it to the current terminal?
A: Use the command screen -d -r to detach the session from elsewhere and reattach it to the current terminal.

screen -d -r session_name

OR

screen -d -r session_ID

This command ensures that the session is detached from any other terminal and reattached to the terminal where you issued the command.

Q: How to remove dead screen sessions?
A: Use the command screen -wipe to remove all dead screen sessions.

screen -wipe

This command will remove any sessions that are considered dead (i.e., those that are no longer running but still listed).

Window Management in Screen

Q: How do you create a new window in a screen session?
A: Use the key binding Ctrl-a c to create a new window.

Ctrl -a c

Here; Ctrl + A tells screen that you want to give it a command and the next input C is the command to create a new window. This will create a new window within your current screen session, allowing you to run a separate command or task in that new window.

Q: How do you switch to the next window in a screen session?
A: Use the key binding Ctrl-a n to switch to the next window.

Ctrl -a n

Here; Ctrl + A tells screen that you want to give it a command and the next input N is the command to switch to the next window. This command help you efficiently navigate between different windows within your screen session.

Q: How do you switch to the previous window in a screen session?
A: Use the key binding Ctrl-a p to switch to the previous window.

Ctrl -a p

Here; Ctrl + A tells screen that you want to give it a command and the next input P is the command to switch to the previous window. This command help you efficiently navigate between different windows within your screen session.

Q: How do you detach the current screen session?
A: Use the key binding Ctrl-a d to detach the current screen session.

Ctrl -a d

Here; Ctrl + A tells screen that you want to give it a command and the next input D is the command to detach the session. This will detach the screen session, leaving it running in the background, and return you to your regular terminal prompt.

Q: How do you kill the current window in a screen session?
A: Use the key binding Ctrl-a k to kill the current window.

Ctrl -a k

Here; Ctrl + A tells screen that you want to give it a command and the next input K is the command to kill the window. This will close (kill) the current window within your screen session.

Q: How do you list all windows in a screen session?
A: Use the key binding Ctrl-a w to list all windows.

Ctrl -a w

Here; Ctrl + A tells screen that you want to give it a command and the next input is the command to list all windows. This will display a list of all windows in the current screen session, along with their numbers and names, allowing you to see and select from the available windows.

Naming a Screen Window

Naming a Screen window allows users to easily identify and switch between different tasks or applications running within a single Screen session. Therefore it is one of the best feature in screen utility.

Q: How do you name the current window in a screen session?
A: Use the key binding Ctrl-a A to name the current window.

Ctrl -a A

Here; Ctrl + A tells screen that you want to give it a command and the next input A is the command to set the window title. This will set the new name for the current window, making it easier to identify when switching between windows.

Logging & Monitoring in Screen

In GNU Screen, logging and monitoring are used to capture and review session activity. Logging saves all input and output to a file, providing a full record of commands run and their results. This is useful for auditing, troubleshooting, and long-term process documentation. Monitoring enables users to view session activities in real time, including which windows are open and any ongoing interactions, facilitating supervision and cooperation among users sharing the same session.

Q: How do you start or stop logging of the current window to a file named screenlog.0?
A: Use the key binding Ctrl-a H to start or stop logging of the current window.

Ctrl -a H

Here; Ctrl + A tells screen that you want to give it a command and the next input H toggles the logging on or off. This toggle allows you to easily start and stop logging as needed within your screen session.

The screenlog.0 file is a log file created by the screen command in Linux when logging is enabled. It captures the output of a screen session, allowing you to review the activities and outputs that occurred within that session.

Q: How do you set a hardstatus line at the bottom of the screen?
A: Use the key binding Ctrl-a _ to set a hardstatus line at the bottom of the screen.

Ctrl -a _

Here; Ctrl + A tells screen that you want to give it a command and the next input : allows you to enter a command.

The hardstatus line in GNU Screen is a customizable status bar at the bottom of the terminal window, showing window lists, titles, system time, and custom indicators. It remains fixed, offering continuous visibility of session details. Users can adjust its content and appearance in the ~/.screenrc configuration file to suit their preferences.

Multiuser Mode in Screen

GNU Screen’s Multiuser Mode allows numerous users to login to and interact with the same Screen session at the same time. Each user sees the same terminal output and input, making it ideal for collaborative activities, pair programming, or remote training sessions in which participants must watch or operate the same terminal environment.

Q: How do you enable multiuser mode for a screen session?
A: Use the command screen -S session_name -X multiuser on to enable multiuser mode for the session.

screen -S session_name -X multiuser on

-S session_name: This specifies the name of the screen session you want to target. Replace session_name with the actual name of your session.
-X : This option allows you to send a command to a running screen session.
multiuser on: This is the command being sent to the specified screen session to enable multiuser mode.

Q: How do you add a user to the session’s ACL (Access Control List)?
A: Use the command screen -S session_name -X acladd username to add a user to the session’s ACL.

screen -S session_name -X acladd username

-S session_name: This specifies the name of the screen session you want to target. Replace session_name with the actual name of your session.
-X: This option allows you to send a command to a running screen session.
acladd username: This is the command being sent to the specified screen session to add a user to the ACL.

Custom Configuration for Screen

Custom configuration files enable you to customize and personalize your terminal multiplexing environment, increasing usability and productivity according on your individual requirements and preferences. Normally screen uses the configurations set by default from the .screenrc file. You can avoid it and create a custom configuration file anywhere in your machine and give instructions in it . Here’s a simple example of what a custom screen configuration file might look like:

Example for Custom Configuration in Screen

After that to start a screen session with this custom configuration, you can specify the file after the command: screen -c.
For example: screen -c ~/my_screen_config

Q: How do you use a custom configuration file instead of the default ~/.screenrc?
A: Use the command screen -c file to use a custom configuration file.

screen -c file

Here –c stands for custom file.

Command Execution with Screen in Linux

Q: How do you run a command in a new screen window?
A: Use the command screen command to run a command in a new screen window.

screen command

As an example, if you need to open text editor nano in screen, then enter screen nano

Q: How do you run a command in a specific screen session?
A: Use the command screen -S session_name command to run a command in a specific screen session.

screen -S session_name command

For example, to start a new screen session named my_session and running the top command: screen -S my_session top

This tutorial covered almost most important features of screen command. For Linux users, the screen command is an essential utility that provides a reliable way to run lengthy tasks, manage many terminal sessions, and guarantee persistent connections. You may greatly increase your productivity and efficiency in the Linux environment by becoming proficient with this screen command. 🥳

Scroll to Top