What Is screen?

Screen is a GNU utility (GNU is a large collection of open source software, and the bundling thereof into an operating system is commonly known as Linux) which allows you start one or more screens inside your terminal.

You may ask why would I want to start a screen inside my terminal, and can you define screen better/further? Sure. Imagine that you are using a remote server of SSH and suddenly your network connection breaks, right in the middle of the database migration or production backup, which was running inside your terminal window. It happens regularly, and the problem is that any command which was executing when the connection broke is now terminated, irrespective of the state it was in.

This clearly annoying situation is one you want to avoid at all costs. Not only may it leave the system or software package in an undefined state, but all your work done this far may be simply lost.

Welcome to screen, the Linux utility which allows you to start a virtual terminal session (a ‘screen‘) inside your terminal session, with the primary and great benefit that such a screen session will not be terminated whenever the network connection breaks down, or when you close the terminal session which started the screen session (the parent process). You can simply wait till your network connection comes back, or open another terminal if you’re not connected via SSH and working on the local machine only, and simply reconnect to your screen session.

Great? Let’s quickly install screen then if you haven’t already:

Installing screen

To install screen on your Debian/Apt based Linux distribution (Like Ubuntu and Mint), execute the following command in your terminal:

sudo apt install screen

To install screen on your RedHat/Yum based Linux distribution (Like RHEL, Centos and Fedora), execute the following command in your terminal:

sudo yum install screen

Now that screen is installed, you can start using it immediately. Simply execute screen at the command line and you’re in. You may have to press enter or space to get through the screen splash screen.

You will see a new command prompt. Press the key sequence CTRL+a > CTRL+d to return to the command prompt of the parent shell session. Then, you can execute screen -ls to see a list of active screen sessions. Typing the command (in the parent shell you are in now if you followed the text identically), screen -d -r will bring you back into the screen session, provided that there is only one screen session active (you can start many).

If there is more then one screen session running on your system (and owned by you), you can type screen -d -r NAME where NAME is the name of a screen as listed by screen -ls, for example 367434.pts-1.roel1, though just specifying any part from either before or after the dot (like 367 or pts), which still uniquely identifies a particular screen session, is sufficient also.

Let’s see how we can now define a great screen profile, based on an excerpt from setup_server.sh, located in the Percona-QA GitHub repository (GPLv2 Licensed), a script created for setting up a server for quality assurance testing. I do not recommend you run this script as it is outdated, used as a reference only, and usually run manually step by step. The only section we will be using is the .screenrc code, reproduced below.

Defining a Great .screenrc Configuration

The .screenrc file is a hidden configuration file in your home directory. To edit it (it will likely not exist yet, especially if you just installed screen, simply use your favorite text editor and open the file ~/.screenrc. If you are experienced with Vim you can simply type at your terminal prompt vi ~/.screenrc. If you would like to learn more about Vi/Vim, consider reading our article Define a Great Vim Profile Using .vimrc.

Once you have opened ~/.screenrc, simply add the following block of text:

If you do not want to use a text editor, or want to somehow script the installation of this .screenrc file into many systems, you could use the following code instead, which will – whenever pasted, or executed from another script, create the file ~/.screenrc for the user who is executing the script. Let’s call our script make_screenrc.sh or similar.

The script presets a number of settings, and most of the ones here are quite self-explanatory. For example, startup_message off simply turns off the Splash screen we saw earlier. defscrollback 10000 defines our scrollback to 10000 lines (you can access scrollback/edit mode by pressing the key sequence CTRL+a > CTRL+ESC and you can exit this mode by pressing ESC once or twice).

We also turn on a virtual bell (as we cannot hear the speaker of a remote server beeping) by using the vbell on and vbell_msg ‘!Bell!’ commands which set the virtual bell message to ‘!Bell!’. I have found however that the virtual bell often does not work.

Next, we set some specific termcapinfo codes to enlarge out output buffer and adjust window resizing. We also remove various potentially annoying key bindings and add a few handy keyboard settings.

All we have to do to activate this new .screenrc configuration is to open a new screen session. We will now benefit from the newly defined settings.

Wrapping up

Defining a great default .screenrc file helps when you use screen a lot. Turning off the splash screen, increasing the scrollback, and improving the output buffer for speed are just a few of the things we did in our .screenrc template. Enjoy using screen and the .screenrc template!