How to use screen in Linux/Ubuntu

How to use screen in Linux/Ubuntu

While doing the development or operational work like installation, and debugging, you are connected to your remote server using SSH and due to inactivity, your SSH connection is dropped. And you have to start it all again.

If you have faced the above situation and want some solution then the screen utility is very much helpful. In this blog, I will explain you have to install and use screen.

Installation

sudo apt update
sudo apt install screen

Check Version

screen --version

The output would be:

Screen version 4.08.00 (GNU) 05-Feb-20

Start a new screen session

screen -S <screen_session_name>

Now, you will be able to continue your work in the screen utility as usual with benefits like you will never lose your work, you can continue where you have left off, and there is no worry about internet connection drop or network issues.

Detach/Exit from the screen session

Ctrl+a d

Attach to your last screen session

This will work if you have only a single screen session, and you will be attached to your screen session. You don't need to remember the name of your screen session.

screen -x

List down available screen sessions

screen -ls

Resume screen session

screen -r <screen_session_name>

Create a new window in the running screen

You can create multiple screen windows/partitions inside your single-screen session.

Ctrl+a c

Switch between screen windows

Ctrl+a [0-9]

Name screen window

Ctrl+a :title server-1

Kill a screen session

screen -r <screen_session_name>
Ctrl+a :quit

Enable copy mode in a screen session

With copy, mode enabled you will be able to scroll up and down in your running screen session.

Ctrl+a Esc

Exit from copy mode

Esc or q

For screen labels to properly work, you need to create a .screenrc file in the home directory of the user from where you are going to create screen sessions

File:

nano .screenrc

Content to be added in the .screenrc file:

hardstatus on
hardstatus alwayslastline
hardstatus string "%w%=%m/%d %c"

With this screen utility, I'm able to work very efficiently. Especially while doing any debugging sessions on the server, where you have to check logs on one screen, on another you are changing/verifying your settings, on another, you are checking DB entries, on another, you are monitoring web-server logs, and so on.

I hope this will help!

References: