How to Install and Use Screen on Ubuntu Servers

Introduction

Have you ever experienced the frustration of working on the command line via an SSH connection, only to have the connection abruptly drop and your work is lost? Have you ever wanted a convenient way to execute multiple programs from the command line without the need to open separate SSH connections for each command? There is a utility called screen that allows us to resume the sessions and manage multiple shell sessions from a single ssh session.

Linux screen is a versatile tool that addresses these challenges and enhances your command line experience. With Linux screen, you can create and manage multiple terminal sessions within a single shell. In this blog, we will show you how to install and use screen on Linux servers.

Common Use Cases of the Linux Screen Command

Session management:

Screen allows you to create multiple terminal sessions within a single window. Each session operates independently, so you can switch between sessions, run different commands in each session, and easily organize your work.

Detaching and Reattaching:

One of the key features of screen is the ability to detach from a session without terminating the processes running inside it. This means you can disconnect from a remote server or close the terminal window, and your processes will continue to run in the background. Later, when you reconnect, you can reattach to the session and continue where you left off.

Preserving Long-Running Processes:

Screen is particularly useful for running long-running processes that you don't want to be interrupted if your connection is lost or if you need to log out. By running your process in a screen session, you can safely disconnect and reconnect later to check on the progress or interact with the process.

Terminal Multiplexing:

Screen allows you to split your terminal window into multiple regions, each displaying a different session or process. This feature is useful when you want to monitor multiple processes simultaneously or switch between them quickly.

How to Install Screen

Step 1: Install Screen

In this tutorial, we will use Unbuntu 20.04. Screen is often installed by default on Ubuntu. You can check if it is installed on your system by the following command.

screen --version
Check screen version on Linux servers.

If you don’t have screen installed on your system, you can also use apt to update your package sources and install screen.

Install Linux Screen on Ubuntu and Debian
sudo apt update
sudo apt install screen
Install Linux Screen on CentOS and Fedora
sudo yum install screen
Install screen on Linux servers.
Verify that screen has been installed by running the following command.
which screen
Verify screen installation on Linux servers.

Step 2: Start Screen

Start a new screen session by running the screen command.

screen

You'll be shown the following page. Press space to continue, and you will see the a new screen window.

Install screen on Linux servers.
Install screen on Linux servers.

It is also possible to start a session and name it. This feature comes handy when you need to run multiple screen sessions. Run the following command to start a named session and be sure to replace the session_name with a real name.

screen -S session_name

How to Use Screen

Step 3: Use Screen

Screen is mainly controlled through keyboard shortcuts. Every keyboard shortcut for screen is prefaced with Ctrl-a (hold the control key while pressing the "a" key). That sequence of keystrokes tells screen that it needs to pay attention to the next keys we press.

Create a Screen Window

To create a window in the screen, use the Ctrl-a c. Then, run Ctrl-a w to list all the windows currently opened. The output shows there are 4 windows. Each window has a number and the windows are numbered starting at "0". The current window has an asterisk next to the number.

Show several sockets
Rename a Screen Window and Switch Windows

You can rename the windows by using Ctrl-a A. As you can see in the following screen shot, we have renamed the number 2 window from bash to newname. Then, you can switch among the windows by using Ctrl-a n, Ctrl-a p, and Ctrl-a Ctrl-a. It's also possible to switch to a specific window by using Ctrl-a window_number. For example, Ctrl-a 1.

Rename a screen window
Kill Screen Windows

To kill a window or kill all windows, you can use Ctrl-a k and Ctrl-a \ respectively. You will be prompted to confirm the action as below. Enter y to close windows. We will lose any windows we have created and any unfinished work.

Kill a window
Kill all windows
Detach a Screen

You can detach from the screen session at any time by typing Ctrl-a -d. Unlike the kill feature, this feature allows your programs in the screen instance to continue to run in the background, but it gives you access back to the session.

Detach a window
Reattach a Screen

After the detachment, we can use screen -ls to list the screens that are currently running. From the below screenshot, you can see there is one screen is detached and running in the background.

List running screens

Then, you can reattach a window by running screen -r id_number. For example, screen -r 23496

Reattach a window
Commonly Used Shortcut Keys

Ctrl-a c: It create a new windows.
Ctrl-a w: It display the list of all the windows currently opened.
Ctrl-a A: It rename the current windows. The name will appear when you will list the list of windows opened with Ctrl-a w.
Ctrl-a n: It go to the next windows.
Ctrl-a p: It go to the previous windows.
Ctrl-a Ctrl-a: It back to the last windows used.
Ctrl-a k: It close the current windows (kill).
Ctrl-a \:It close all windows (kill).
Ctrl-a S: It split the current windows horizontally. To switch between the windows, do Ctrl-a Tab.
Ctrl-a |:It split the current windows vertically.
Ctrl-a X: Close active Split window
Ctrl-a Q: Close all Split windows
Ctrl-a d: It detach a screen session without stopping it.
screen -r: It reattach a detached screen session.

If you need help on the screen keys, simple press the shortcut keys Ctrl-a ?.

Install screen on Linux servers.

Step 4: Customize Screen Configurations

When screen is started, it reads its configuration parameters from /etc/screenrc and ~/.screenrc if the file is present. We can modify the default Screen settings according to our preferences using the .screenrc file. You can issue the following command to edit the configuration file.

cd /etc
nano screenrc
Customize screen settings
Change settings

When you’re done editing, save and close the file. If you’re using nano, you can do so by typing CTRL+X and then y and ENTER to confirm.

Conclusion

In this tutorial, you learned how to install and use the GNU screen utility. You can now use several shortcut keys to create screen, switch screen windows, and killing screen windows, detach or reattach screens, and customize the screen settings. using screen on Linux enhances productivity, provides session resilience, enables remote access, facilitates collaboration, and offers efficient terminal window management.