en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com

How to use the Linux Screen command

There are certain incidents where the entire work gets lost due to connection failure. Rings some bells? Well, it is a common thing, and you must have encountered this situation too! To provide you with some relief, we are here to deliver a solution to this issue. A utility known as “screen” helps you to restore your work.

Linux Tutorial Mar 22, 21 by Nisal N 8 min Read
How to use the Linux Screen command

There are certain incidents where the entire work gets lost due to connection failure. Rings some bells? Well, it is a common thing, and you must have encountered this situation too! To provide you with some relief, we are here to deliver a solution to this issue. A utility known as “screen” helps you to restore your work.

A screen? Well, yes,! Let us analyze the concept in detail. A Screen or more commonly known as a GNU screen, is a terminal multiplexer. It assists you to resume any session as well as multiple virtual terminals inside that particular session.

When we talk about the Linux screen command, you can easily push the running applications in the background. Afterwards, according to your wish, pull them forward as and when required. This terminal multiplexor is packed with immense options for you. Want to know more? Let’s begin our learning process!

Here are some pros of the screen command?

  • If you are running the screen session, you can easily create new windows and carry out other tasks inside them. It is effortless to switch between Windows to keep a check on the work. You have the option to split the terminal window into either horizontal or vertical regions. Furthermore, you can easily look at several screen windows in one window.
  • It is straightforward to detach and reattach. Detach is the process when you push the window to the background. Whereas reattach is the process when you pull the window to the front.
  • You have the liberty to share a screen session. Two people are able to look at the same screen at the same time with different SSH connections.
  • It becomes feasible to use multiple cell windows within the same SSH session. The Linux screen helps you to keep the shell active amidst any network failure. 
  • Even from multiple locations, you can easily disconnect and reconnect to various shell sessions.
  • It is possible to hold a long-running process without the need to maintain an active shell session.

Now let us initiate the process to learn how to install the Linux GNU screen!

How to install Linux GNU screen?

Almost all the Linux distros nowadays are pre-installed with the screen package. To check if your system has the installed version, type the following command:

screen - - version

Output

Screen version 4.06.02 (GNU) 06-Mar-21

If you do not see any installed package, don’t worry! You can do that in seconds with the help of your package manager.

How to install Linux screen on Ubuntu?

sudo apt update

sudo apt-get install screen

To install on Fedora:

sudo dnf install screen

Moving on, 

How to start a Linux screen?

Just type: 

screen

You will witness this page.

This command helps in creating a window and starting a shell. Now, a screen session is in front of you. Here is a list of some control commands in front of you.

  • Ctrl+a “

            It lists all the windows for you

  • Ctrl+a c  

            A new window is created

  • Ctrl+a 0

            This helps to switch to window 0

  • Ctrl+a S

            It splits your current region horizontally 

  • Ctrl+a A

            You can rename your current window

  • Ctrl+a d

            It detaches you from the ongoing screen session

  • Ctrl+a tab

            It helps to switch the input to the next region

  • Ctrl+a I

            The current region is split vertically into two regions

  • Ctrl+a X

            The current region can be closed

  • Ctrl+a Q

            All the other regions except your current region are closed

  • Ctrl+a Ctrl+a

 You can easily toggle between the previous and the current regions.

Now that you have got the hang of the basic commands, let us learn some more concepts:

How to name screen sessions?

It’s always easy to remember a familiar word as a filename rather than numerics. Here is an example:

screen -S newfile

When the Linux screen launches the session, a blank window is visible. Now what? Just download the file ‘newfile’ using this:

curl http://ipv4.download.thinkbroadband.com/1GB.zip >newfile.zip

After the download begins, use Ctrl+A and press D. Your session will be detached. Use the -ls command to notice the details of your session.

You see the name of your file, reattach it using the following command:

screen -r newfile

Now, you can see the progress of your download.

Type exit and close the session window.

Now, if you want to customize your Linux screen, read on!

Whenever you start your screen, the configuration parameters are read from /etc/screenrc and ~/.screenrc. Use ~/.screenrc in order to modify the default screen settings as per your needs.

Sample:

# 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}]'

How to use the screen with multiple windows?

Here are a few processes for you to showcase examples where you can use the screen with multiple windows.

To start a screen session, called monitor, type the following:

screen -S monitor

Now, in the command prompt, type the following to display the kernel buffer messages.

dmesg -H -w

Below given is the output screen:

Now, to showcase another application, we need a new screen window. As on this window, dmesg is looking for new messages.

In order to create a new window, press Ctrl+A then C. We are going to display the virtual memory usage of the computer here.

To do that, use the following command:

watch vmstat

This is the output:

Now, you have successfully run two processes at the same time. Congratulations! 

If you want to switch between the two, use Ctrl+A and the window’s number! In our case, the number assigned to the first window is 0, and the second is 1. 

Hence, we type, Ctrl+A 0

Now, type Ctrl+A 1 to go to the vmstat window.

Moving further, press Ctrl+A, Shift+S to split the current window into two regions. 

How to use screen over SSH?

The following process is recommended if you want to initiate a process on a machine and continue it on another.

As you have seen that you can start the window, detach it and reattach it whenever you want. Let us now make an SSH connection to the computer. 

For instance,

ssh abc@192.168.4.30

Now, after initializing, start a screen session.

screen -S ssh - abc

Just for a sample, we will initiate the network monitoring command: top.

top

In order to detach the window, press Ctrl+A D.

Now, this is the original terminal window.

If you want to exit the session, type exit. 

exit

Again, reconnect and reattach the screen session to witness the still-running instance of the top command.

How to share a screen session?

If someone wants to connect to the Ubuntu server from Fedora, follow this command:

ssh user@192.168.4.30

After this, your connection is established. Start a screen session using -S. Use -d and -m to create a detached screen session.

screen -d -m -s ssh-abc

For multi-screen mode, use -X to attach the session. 

screen -X ssh-abc

This way, when either of the people types something, it will be visible to both. 

Conclusion 

The above-stated data is all about profoundly making you understand the most important commands to use on the Linux screen. We hope that the information presented in the article guided you to solve your queries and widened your horizons.

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment