The Linux Screen utility is a powerful tool that allows users to create multiple virtual terminal sessions within a single terminal window or SSH session. It enables you to detach and reattach terminal sessions, which is particularly useful when working on remote servers or long-running processes that you don’t want to be interrupted if your connection drops.
Here’s a detailed explanation of how to use the Linux Screen utility:
Installation:
If Screen is not already installed on your system, you can install it using your distribution’s package manager. For example, on Debian-based systems like Ubuntu, you can install it with the following command:
sudo apt-get install screen
sudo yum install screen
Basic Usage:
- Starting a Screen Session: To start a new Screen session, simply type:
screen
This will create a new session with a single terminal window.
- Working within Screen: Inside a Screen session, you can work as you would in a regular terminal window. You can run commands, edit files, and perform any other tasks.
- Detaching from a Session: To detach from the current Screen session without terminating it, press
Ctrl + A
followed byCtrl + D
. This will return you to the shell prompt while leaving the Screen session running in the background. - Listing Screen Sessions: You can list all active Screen sessions by typing:
screen -ls
This will display the list of active Screen sessions along with their session IDs.
- Reattaching to a Session: To reattach to a detached Screen session, use the following command:
screen -r [session_id]
Replace
[session_id]
with the ID of the Screen session you want to reattach to. If there is only one detached session, you can omit the session ID. - Creating Named Sessions: You can create named Screen sessions for easier identification and management. To start a named session, use the
-S
option followed by the session name:screen -S [session_name]
- Exiting a Screen Session: To exit a Screen session, simply type
exit
within the session and press Enter. This will terminate the Screen session and return you to the shell prompt.
Advanced Usage:
- Splitting the Screen: Screen allows you to split the terminal window into multiple regions, each displaying a different terminal session. Press
Ctrl + A
followed byS
to split the current region horizontally, orCtrl + A
followed by|
to split it vertically. - Navigating between Regions: You can navigate between different regions within a Screen session using
Ctrl + A
followed byTab
. - Copying and Scrolling: Screen allows you to copy and scroll within the terminal window. Press
Ctrl + A
followed by[
to enter copy mode. Use the arrow keys or page up/page down keys to navigate, andSpace
to start selecting text. PressEnter
to copy the selected text. To exit copy mode, pressEsc
. - Configuration File: Screen settings can be customized using a configuration file located at
~/.screenrc
. You can define default settings, key bindings, and other preferences in this file.
Linux Screen is a versatile utility that enhances terminal multiplexing and remote access capabilities, making it an essential tool for system administrators, developers, and anyone who frequently works in the command line environment.