how to use screen utility tool 1

How to Use the Screen Utility Tool on Linux

Do you need a tool that lets you spawn multiple virtual screen terminals in a single session? Or have you experienced unstable connection, and your remote server disconnects so you have to start compiling the process all over again? You may already have experienced this issue at least once in your professional career. Luckily for you, the screen utility tool helps to solve this.

The screen utility tool is one of the most flexible tools you could ever use as a systems administrator. It offers an extensive array of functionalities perfect for engaging in multitasking, remote server administration, and display history management.

What is Screen?

Screen is a full-screen window manager that multiplexes a physical terminal between several processes into virtual terminals. When the screen command is called, it creates a single window with a shell. 

You can create new windows with other programs within them, including more shells, kill existing windows, and view a list of windows, among other things. Your programs run when their window is not visible and even when the whole screen session is detached from the user’s Terminal.

Installing screen

Screen is available both in the apt repo of Debian-based OS and in the epel-repo in Fedora-based OS. You can check out this guide if you haven’t installed the epel-repo yet on your Linux device.

To install screen on Ubuntu or other Debian-based OS, execute the command:

$ sudo apt install screen

To install screen on CentOS or other Fedora-based OS, execute the command:

$ sudo dnf install screen

Once installed, you can check what version was installed. Execute:

$ screen --version


screen utility tool

How to Use the Screen Utility Tool

Knowing when to create, switch and terminate a screen session means that you are already capable of multitasking. Plus, you might also be familiar with all of the running programs in each virtual shell session.

However, important details such as knowing when you are inside a shell screen and which screen session you are in can be a bit tricky without remembering your very first screen session. 

We can eliminate this factor by simply modifying the screenrc file to provide details that immediately tells you that you are inside a virtual terminal. The instructions outlined below will help you utilize the screen command well enough to make you familiar with it.

Modifying a Custom screenrc File

You can modify the screenrc file located within your home folder to create a custom screen environment for your specified user account. 

Nonetheless, in this example, we will be modifying the screenrc file in the /etc folder, which the screen environment on every user inside your machine will uniformly encounter.

Save the previous screenrc configuration. To do this, execute the command:

$ sudo mv /etc/screenrc /etc/screenrc.bak

Then create a new screenrc file inside etc:

$ sudo vim /etc/screenrc

After that, add the following custom config:

startup_message off
shelltitle "> |csh"
defscrollback 100000
escape \034\034
caption always
hardstatus alwayslastline
hardstatus string '%{= by}%-Lw%{= Yk}%50>%n%f* %t%{= by}%+Lw%< %{= kG}%-=%D %d %M %Y %C:%s %A%{-} %H '
bind j focus down
bind k focus up
bind t focus top
bind b focus bottom

Unlike starting with the default config, the custom config, using the csh shell environment, will be more user-friendly and interactive. For context, you can see a comparison of the two below:

screen utility tool

Unlike the default screen, the custom screen below shows the date, the shell environment, and the hostname of the machine.

Creating a virtual terminal

To create a virtual screen session, execute the following command:

$ screen -S <screen name>
screen create

In the above example, a screen with a virtual session was initiated with an identifier name of Distroid. Once you are inside the virtual shell environment, it will look something like this:

screen new

To list down all the available screens on your user account, issue the command:

$ screen -ls
list screen utility tool

In this example, you will notice two screen environments present. One is named Bobby, and the other Disdroid. The numbers preceding the screen names are the unique screen numbers that you can use to reattach the screen session.

Opening and Switching between virtual terminals

You can open simultaneous screen tabs in one session. To do this, you need to initiate a screen session first, then press the keys “Ctrl + \” and “C”. Note that the default host key for screen was “Ctrl + A” the original host key. In our custom script, the host key was changed to “Ctrl + \”.

switch terminals

You can also switch between virtual terminals, when inside a session. This can be particularly handy when compiling code while troubleshooting a buggy problem. 

To switch in between screens, type in “Ctrl + A” and “P” if you want to switch to the next window. But if you wish to switch to the previous one, type “Ctrl + A” and “P”.

Detaching and reattaching a virtual terminal

When you need to leave your virtual terminal session for any reason at all or if you’re just done for the day, you can input “Ctrl + \” and “D” keys.

Once detached, it will look something like this:

screen detach

To reattach, first, list down all the detached or active sessions in your Linux account. Then reattach it by executing the command:

$ screen -r <screen name or number>
screen utility tool reattach

Terminating your screen session

When you’re done with a particular screen session, and there’s no point in having it hanging around, you can terminate the virtual session by simply typing the word “exit” on your current screen session.

Final Thoughts

Utility tools such as screen have always been handy and helpful in many ways than what it was initially intended for use. 

In this guide, you’ve learned how to initiate screen sessions, create multiple screens in one session, switch between virtual terminals, and detach and reattach screen sessions. Along with how to terminate one.

You can explore more of the screen utility tool by trying it firsthand with your own Linux device. This way, you will be able to learn and appreciate its usefulness and versatility.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts