What is Screen?
Screen is a terminal program in Linux which allows us to use a virtual (VT100 terminal) as full-screen window manager which multiplexes an open physical terminal between multiple processes, which are typically, interactive shells. It allows us to access multiple terminal sessions within a single terminal or a remote terminal session. It is most useful when addressing multiple Linux shell commands on the command line, as well as separating commands from the shell that started the commands.
Screen also allows a user to initiate a command from one terminal, disconnect from that terminal, and then reconnect from a different location to that same terminal, while using a different terminal without having to restart the command. This simply lets a user better control multiple and separate command windows.
Screen also lets multiple remote computers connect to the same screen session at once. This allows multiple users to connect to the same screen session allowing a second user to follow along with another administrator working on a server.
Screen is started by using the following screen command:
Here is a list of some of the more common flags and how we can use them.
|-ls||Lists the current screen sessions||screen -ls|
|-aS||Create and names a screen session which include all capabilities||screen -aS screenname|
|-r||Resume a detached screen session||screen -r (name|pid)|
|-x||Attach to an attached screen session||screen -x (name|pid)|
|-d||Detach an attached screen session||screen -d (name|pid)|
|Start an new screen session in detached mode and run $command||screen -d -m $command|
|-DR||Force disconnect of current user and reconnect with your user||screen -DR|
|-wipe||Removes all dead screen sessions. (Sessions that are unreachable)||screen -wipe|
Default Session Key Bindings
While inside the screen session, ctrl+a activates the key bindings. The following table shows some of the default key bindings:
|?||Show key bindings.|
|A||Set a title for the current window.|
|c||Create a new window.|
|n||Switch to the next window.|
|p||Switch to the previous window.|
|0-9||Switch to window number n.|
|w||Show a list of windows.|
|"||Present a list of windows for selection.|
|F||Resize the window to the current region size.|
|[ or esc||Enter copy/scrollback mode.|
|k||Destroy current window.|
To get a full list of available screens, use this command:
we will then see output similar to this:
There is a screen on: 26092.ttyp0.host (Attached) 1 Socket in /tmp/screens/S-root.
This output typically shows us the currently running screens. As you can see by the "(Attached)" output, the 26092 session is currently attached to a terminal. Since this screen is currently attached, we can detach it remotely using this command.
screen -d 26092
If another user is logged onto this screen session and actively working, this modification will kill their session. So, beware of detaching screen sessions this way. To reattach to that same screen, run this command.
screen -r 26092
We can detach and reattach to the screen concurrently using this command.
screen -d -r 26092
In order to create a new screen session called LW, we can enter this command.
screen -S LW
Modifying Screens Behavior
To enable scrollback in xterm, we can add the following information to our ~/.screenrc file on our workstation.
# Enable mouse scrolling and scroll bar history scrolling termcapinfo xterm* ti@:te@
If we use the -L option with screen, it tells screen to turn on the automatic logging output for the window. This will create a file within the current directory, with the name screenlog.# where # is the ID number for that screen beginning from 0.
screen -L -S LW
Multiuser Screen Sessions
Multiuser is an excellent option to use if we need to share our session with others. It is not turned on by default for security reasons but can be enabled easily. In order to allow multiple users to connect to the same screen session, the multiuser option must be enabled. Here are the steps needed to complete this task.
- Begin by logging in to the server and starting the screen session via the command line.
- Next, type Ctrl-a to enter the command mode.
- Now, type in ":" (colon) to enter the screen's built-in command line.
- We can now activate the multiuser mode by entering multiuser on in the screen's command line, which appears in a reverse color at the bottom of the console window.
- To confirm this change, hit Enter or Return.
Multiuser mode can be very useful if we are attempting to demonstrate to someone what we are doing on a server. With this mode on, the other user will be able to follow our keystrokes within that screen session.
If more in depth information is required, we can always consult the man page for screen using:
To initiate a screen session named LW and run the top command in it without attaching to it, nothing will appear to happen but you will see the screen with screen -ls:
screen -S LW -d -m top
To end or kill the LW screen session, use this command:
screen -S LW -p 0 -X kill
To start a bash session in screen, we can use this command:
screen -S LW -d -m /bin/bash
To run the ls command inside the screen session use this command:
screen -S LW -p 0 -X exec ls /home
To end or kill the LW screen session, run this command:
screen -S LW -p 0 -X kill
To scroll up within a screen window, use this command:
CTRL+a then ESC, #Press CTRL KEY and 'A' KEY together and then hit the ESC KEY #to scroll using the arrow keys hit ESC again to exit.
Most Useful Screen Commands
Here is a quick overview of the most useful screen commands that we might need to use.
- Ctrl+A, C - This key command creates a new screen
- Ctrl+A, N (where N is equal to the number of the screen) - This key command switch us to a specific screen
- Ctrl+A, A - This key command switches us to the next screen
- Ctrl+A, W - This key command lets us view a list of screens
- Ctrl+A, " (double quote) - This key command allows us to view the screen picker
- Ctrl+A, Ctrl+D - This key command detaches us from the current session
- screen -R - This key command attaches us to a running screen session
- Ctrl+A, ? - This key command shows us the shortcut key options
There are numerous other commands that we can use to control our screen session, many of which can be viewed by typing "Ctrl+A ?" at the prompt. This will bring up a help window that shows us all the available key bindings. For example:
- The 'C-a p' and 'C-a n' key sequence can be used to switch to the next or previous window.
- Using the 'C-a N' key sequence - (where N is a number from 0 to 9, that can will connect to the corresponding screen window.
- The 'C-a w' key sequence displays a list of all screen windows. The unique ID of each window, along with its name, and running process is displayed, for each individual window. The current window will be marked with an asterisk (*).
- The 'C-a k'key sequence will kill the current window session. We can also type 'exit' to kill the current window. If no other screen sessions are open, screen will simply exits. The 'C-a \' key sequence also accomplishes the same task.
- The 'C-a d' key sequence detaches the from the current screen session.
We can also detach from the screen by closing the terminal that is running our screen utility. Although this may appear to close our terminal session, in reality this simply closes the terminal and unbinds our session from the current terminal. All the commands that were started under our screen will continue to run.
We can also log out from the server and login again, then restart a terminal session and type 'screen -r' to connect again from where we left off. In case, there are more than one running screen sessions on the server, screen will prompt us for a host.tty.pid. For example, say I have two screen sessions. So, when we type the 'screen -r' command, it gives the following message:
$ screen -r There are several suitable screens on: 2999.pts-6.localhost (Detached) 1920.PTS-6.localhost (Detached) To connect to one of the above detached screens run, screen -d -r 2999.pts-6.localhost to resume that screen.
Screen is an invaluable tool when we must work with the terminal daily. It allows us to utilize a limited amount of screen space while maintaining the full functionality of a terminal window. Additionally, screen has the ability to be used as a teaching tool allowing multiple users to connect to an existing screen and, if we have enabled logging, share that file with the user for later reference.
Are you trying to use the screen command and experiencing some difficulty with the control commands, flags or other options with the program?
Our knowledgeable system administrators and experienced linux technicians are standing by to assist you with the help you need. Give us a call at today at 800.580.4985, or open a chat or ticket with us to speak with us right away!
- ChatGPT Integration — How to Create a Plugin for ChatGPT
- Stable Diffusion AI Image Generator (SDXL) — Using the Web UI
- How to Install VMware Tools on Ubuntu: Step-by-Step Guide
- How to Install WordPress on Linux (AlmaLinux)
- What is CentOS? Everything You Need to Know
- Virtual Desktop Environment — Configuring Kasm Workspaces
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.