How to use the Linux Screen Command
Introduction
Before we begin talking about how to use the Linux Screen command, let's briefly understand - What is a Screen Command?
Using Linux Screen command, you can launch and use many shell sessions with a single ssh session. You can detach from a session and can reattach later. After you detach, the process you started earlier will still be running and managed by the screen itself. When you re-attach the process later, you will get the terminals the way you left them earlier.
In this tutorial, you will use the Linux Screen command. We will also address a few FAQs on how to use the Linux Screen command.
What's the Syntax of the screen Command
screen [-opts] [cmd [args]]
Options
-a: Force all capabilities into each window's termcap.
-A-[r|R]: Adapt all windows to the new display width and height.
-c file: Reads configuration file instead of .screenrc
.
-d (-r): It detaches the screen running elsewhere (reattach here).
-dmS name: Starts as daemon: screen session in detached mode.
-D -RR: It does whatever is required to get a screen session.
-e xy: Changes the command characters
-f: Makes the flow control on, -fn=off, -fa=auto.
-h lines: Sets the size of scrollback history buffer.
-i: Interrupts the output sooner when flow control is on.
-L: Makes the login mode on.
-m: Ignores $STY variable, creates a new screen session.
-O: Chooses optimal output rather than exact vt100 emulation.
-p window: Preselect the named window if it exists.
-q: Exits with non-zero return code when unsuccessful.
-Q: Command sends the response to the stdout of the querying process.
-r [session]: It reattaches to the detached screen process.
-R: It reattaches if possible, otherwise starts a new session.
-S sockname: Names the session .sockname.
-t title: It sets the title.
-T term: It uses term as $TERM for windows.
-U: It tells the screen to use UTF-8 encoding.
-v: It prints screen version.
-x: It attaches to the not detached screen.
-X: It executes as a screen command in the specific session.
Install Linux GNU Screen
Nowadays, the screen package is pre-installed on most Linux distros. Verify if it is installed on your system using the following command:
screen --version
If screen
is not installed, use the following set of commands:
sudo apt update
sudo apt install screen
For CentOS and Fedora:
sudo yum install screen
Start Linux Screen
You need to simply type screen
to start the screen session:
screen
Now, open a screen session, create a new window and start shell in that window.
You can get a list of commands using the following command:
Ctrl+a ?
Start Named Session
If you want to run multiple screen sessions, you can utilize named sessions. Run the following command to create a named session:
screen -S session_name
It is recommended to choose a descriptive session name.
Detach from Linux Screen Session
Run the following command to detach from the screen session:
Ctrl+l+a
d
The program will keep running in the screen session after you detach from the session.
Reattach to a Linux Screen
Use the following command to resume the screen session:
screen -r
You need to append the r
switch if you want to have multiple screen sessions running on your machine.
You can find the session ID using the following command:
screen -ls
Output
There are screens on:
10835.pts-0.linuxize-desktop (Detached)
10366.pts-0.linuxize-desktop (Detached)
2 Sockets in /run/screens/S-linuxize.
To restore the screen 10835.pts-0, type the following command:
screen -r 10835
Customize Linux Screen
It is possible to modify the default screen settings using the .screenrc
file. Check the below sample ~/.screenrc
configuration:
# Turn off the welcome message
startup_message off
# Disable visual bell
vbell off
# Set scrollback buffer to 10000
defscrollback 10000
# Customize the status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
Linux Screen Usage
Below are the basic steps to get started with screen:
- Type
screen
on the command prompt. - Run the required program.
- Use the key sequence
Ctrl-a
+Ctrl-d
to detach from the screen session. - Reattach to the screen session using
screen -r
.
FAQs to Use the Linux Screen Command
How do I use the screen command?
Open the terminal, simply type "screen" and press Enter to start a new screen session.
How can I create a new screen session with a specific name?
To create a named session, use the command "screen -S session_name" where "session_name" is the desired name for the session.
Can I detach and reattach to screen sessions?
Yes, you can detach from a screen session by pressing "Ctrl + A" followed by "d". To reattach, use the command "screen -r" or "screen -r session_name".
How do I create multiple windows within a screen session?
Within a screen session, press "Ctrl + A" followed by "c" to create a new window. Use "Ctrl + A" and "n" or "p" to navigate between windows.
Is it possible to split the screen within a window?
Yes, you can split the screen horizontally using "Ctrl + A" followed by "S" and split vertically with "Ctrl + A" followed by "|".
Can I scroll within a screen session?
Yes, you can enter scroll mode by pressing "Ctrl + A" followed by "Esc". Then, you can use arrow keys or Page Up/Down to scroll.
How can I send commands to different windows or sessions within the screen?
Use "Ctrl + A" followed by the number or name of the target window or session to send commands directly to that specific screen.
Conclusion
We hope this detailed guide helped you understand how to use the screen command.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.