screen, the Linux utility which allows you to start a ‘screen’ inside a terminal, is by default a little under-configured. Learn how to create a great screen profile which is loaded automatically each time you use screen.
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.
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:
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+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
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
.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:
# General settings vbell on vbell_msg '!Bell!' autodetach on startup_message off defscrollback 10000 # Termcapinfo for xterm termcapinfo xterm* Z0=\E[?3h:Z1=\E[?3l:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l # Do not resize window termcapinfo xterm* OL=1000 # Increase output buffer for speed # Remove various keyboard bindings bind x # Do not lock screen bind ^x # Idem bind h # Do not write out copy of screen to disk bind ^h # Idem bind ^\ # Do not kill all windows/exit screen bind . # Disable dumptermcap # Add keyboard bindings bind } history bind k kill
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.
#!/bin/bash # Create screen script ~/.screenrc touch ~/.screenrc if [ -z "$(cat ~/.screenrc|grep 'termcapinfo xterm')" ]; then cat << EOF > ~/.screenrc # General settings vbell on vbell_msg '!Bell!' autodetach on startup_message off defscrollback 10000 # Termcapinfo for xterm termcapinfo xterm* Z0=\E[?3h:Z1=\E[?3l:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l # Do not resize window termcapinfo xterm* OL=1000 # Increase output buffer for speed # Remove various keyboard bindings bind x # Do not lock screen bind ^x # Idem bind h # Do not write out copy of screen to disk bind ^h # Idem bind ^\ # Do not kill all windows/exit screen bind . # Disable dumptermcap # Add keyboard bindings bind } history bind k kill EOF fi
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+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.
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!