The Screen utility is a Linux window manager that allows you to switch between multiple processes in a single physical terminal. Screen provides a scrollable history buffer and a mechanism for copying and pasting text between windows.
With Screen, you can create new windows with different programs, close the current windows, view a list of active windows, enable and disable output logging, and switch between windows. All windows work independently, and programs continue to run even when the session is disconnected from the user's terminal. This makes Screen a useful tool for efficiently managing multiple tasks in a single terminal.
The Screen may be pre-installed in the operating system or require separate installation depending on the distribution. To install Screen, use the following command:
For Ubuntu and Debian:
apt install -y screen
For CentOS and Fedora:
yum install -y screen
Or:
dnf install -y screen
Let's go over the basic commands for managing Linux Screen sessions.
To start Screen, simply enter the following command in your terminal:
screen
This will open a Screen session, create a new window, start a shell in it, and you will see a new window. Press Enter to proceed to enter commands.
You can name your sessions, which is especially useful when working with multiple Screen sessions. To create a named session, use the following command:
screen -S session_name
It’s always helpful to choose a descriptive name for the session.
To detach from a Screen session at any time, type:
Ctrl+a d
The program running in the Screen session will continue to run in the background after you detach.
To resume your Screen session, use the command:
screen -r
If you have multiple Screen sessions running, you need to specify the session ID or its name after the -r
parameter.
To see the list of currently running sessions, use:
screen -ls
You will see a list of sessions like this:
There are screens on:
1468393.hostman (01/25/2025 02:07:34 PM) (Detached)
1466624.pts-3.1495851-user (01/25/2025 01:54:05 PM) (Detached)
2 Sockets in /run/screens/S-linuxize.
To resume the session with ID 1466624.pts-3.1495851-username
, enter:
screen -r 1466624
To resume a session using its name, type:
screen -r session_name
Screen offers a variety of useful features for convenient session management in the terminal.
You can customize each window to suit your needs, such as adjusting its size according to display settings or configuring options using a custom configuration file.
You can also pause a session and resume it later or run Screen in daemon mode to keep it running in the background.
Additionally, you can customize command keys, manage data flow, and enable logging.
It's also possible to switch between windows, change their titles, and use UTF-8 encoding, making terminal work more comfortable and adaptable to different tasks.
Screen options:
-a
: Enables all possible features for each window, maximizing functionality.
-A -[r|R]
: Automatically adjusts all windows to fit the new screen width and height.
-c file
: Specifies an alternate configuration file instead of the default .screenrc
.
-d (-r)
: Detaches the current Screen session without terminating it so that you can reconnect later.
-D (-r)
: Terminates the active connection to a remote session, but the session itself remains running and can be resumed.
-D -RR
: Takes all necessary actions to reconnect to an existing Screen session if one is available. If not, it starts a new one.
-e xy
: Changes the default keybindings for Screen commands to custom ones, which is useful for avoiding conflicts with other programs.
-f
: Enables data flow control; -fn
disables it; -fa
enables automatic flow control, which is helpful when working with large amounts of data.
-h lines
: Sets the scrollback buffer size, allowing you to scroll through more command history.
-i
: Interrupts data output on the screen when flow control is enabled, preventing terminal overload.
-l
: Logs session information to the system log to keep track of active sessions; -ln
disables this.
-ls [pattern]
: Displays a list of all active Screen sessions currently connected.
-L
: Enables logging of all terminal output to a log file.
-p window
: Automatically selects the specified window on startup if it exists.
-q
: Runs Screen in “quiet” mode, suppressing unnecessary error messages.
-V
: Displays the version of Screen and then exits.
-r [session]
: Reconnects to a previously started but detached Screen session.
-R
: If an existing session is found, it reconnects to it; if not, it starts a new one.
-S session_name
: Assigns a name to the new session, making it easier to reconnect to it later.
-t title
: Sets a title for the window, which is displayed in the window list.
-U
: Enables UTF-8 encoding support for text display.
-v
: Displays the current version of the Screen program.
-x
: Attaches to an active session, allowing it to be used simultaneously on multiple screens.
-X
: Executes the specified command within an active Screen session.
You can work with multiple Screen sessions simultaneously, with several windows open for each session.
To create a new window with a shell, press:
Ctrl+a c
The window will be automatically assigned a number from 0 to 9.
Below are the common commands for managing windows in Screen:
Ctrl+a c
— Create a new window (with a shell).
Ctrl+a "
— Display a list of all windows.
Ctrl+a 0
— Switch to window 0 (by number).
Ctrl+a A
— Rename the current window.
Ctrl+a S
— Split the current region horizontally into two regions.
Ctrl+a |
— Split the current region vertically into two regions.
Ctrl+a tab
— Move the input focus to the next region.
Ctrl+a Ctrl+a
— Toggle between the current and previous windows.
Ctrl+a Q
— Close all regions except the current one.
Ctrl+a X
— Close the current region.
To see all commands, enter:
Ctrl+a ?
When Screen starts, it reads its configuration settings from /etc/screenrc
and ~/.screenrc
, if they exist.
You can customize Screen's default settings to suit your preferences using the .screenrc
file.
This example includes a custom status line and several additional options:
# Disable the startup message
startup_message off
# Automatically detach from the session when the connection is lost
autodetach on
# Set the scrollback buffer to 10,000 lines
defscrollback 10000
# Enable logging for the current session
logfile /path/to/screenlog
Automatic Window Splitting on Startup
Useful if you frequently work with multiple windows and want them to open automatically when Screen starts.
screen -t shell1
split
focus
screen -t shell2
Logging All Sessions
Useful for keeping a record of work.
deflog on
logfile $HOME/.screen/screenlog.%t
Automatic Reconnection on Disconnection
Useful when working with unstable connections.
autodetach on
reattach on
Example 1: To monitor file changes in real-time, you can use two Screen windows: one for editing the file and another for displaying its content using the command:
tail -f filename
This allows you to instantly see all changes made without having to re-run the command.
Example 2: When working in a terminal over SSH using a Screen session, you won’t lose data if the connection is interrupted. Even if the connection drops, you can reconnect and resume work exactly where you left off by simply reattaching to the Screen session.
Example 3: For long-running tasks, such as compiling code or performing a backup, you can start the task in one Screen session and monitor its progress. You can safely disconnect anytime, knowing the task will continue running. Later, you can reconnect to the session to check the results.
In this guide, we covered how to use Linux Screen to manage terminal sessions effectively. You learned how to:
We also discussed how to customize the terminal using the .screenrc
configuration file to make your work environment more convenient and personalized.
You can now use Screen for a more comfortable and productive terminal experience by mastering these basic features.
For more information about Screen, check out the Screen user's manual.