Using the screen command for flexibility
You might at first think of “screen” as the device on which your Linux desktop is displayed. That is one meaning. For SysAdmins like us, screen is a program, a screen manager, that enhances the power of the command line. The Screen utility allows launching multiple shells in a single terminal session and provides means to navigate between the running shells.
If you’re having a bit of confusion regarding the terminology of the command line, my recent article, The Linux Philosophy for SysAdmins, Tenet 05 — Embrace the CLI, has a good explanation of the terms you’ll encounter. I suggest you read that article first and then return here to continue.
Note: The term, Screen, with uppercase S refers to the screen program as a tool, while screen, in lowercase and bold refers to the command.
The problems
I have many times had a remote session running a program when the communications link failed. When that happened the running program was terminated as well and I had to restart it from the beginning. It could get very frustrating. The Screen program can prevent that. The remote Screen session will continue to run even if the connectivity to the remote host is broken because the network connection fails. It also allows the intentional disconnection of the Screen session from the terminal session and reconnecting later from the same or a different computer. All of the CLI programs running in the screen terminal sessions will continue to run on the remote host. This means that once communications is reestablished one can log back into the remote host, use the screen -r command at the remote command line to reattach the screen session to the terminal.
Sometimes I’ve been working on a remote host and had the need to leave it running while I move to another location. Terminating an SSH network connection to a remote host also terminates the program running on that remote host. I usually start up one or more terminal sessions in Screen, disconnect from Screen and log out of the remote host. Then I can go to another location, login to a different host, SSH to the host running my programs in screen, login and use the screen -r command to reconnect to the screen session and all of the terminal sessions and their respective programs will still be running.
There are also some environments where physical access to a hardware console is not available to provide access to the Virtual Consoles but the flexibility of multiple shells is needed via a remote connection like SSH.
In other cases, a GUI desktop is not available and the options for multiple terminal sessions or use of a GUI terminal emulator is not possible. In this situation you can use multiple Virtual Consoles, but that can still be constraining.
Screen
You will probably find it convenient to use the Screen program in many of those cases in order to work quickly and efficiently. Let’s look at how Screen can help. But first install the screen program. I use DNF because it’s the CLI software management tool for Fedora, my distro. You can use the tool for your distro. Many distros have graphical software tools.
As root, install the Screen package.
# dnf -y install screen
Usage Note: In this experiment I provide instructions such as “press Ctrl-a + c” to open a new terminal, for example. That means that you should hold down the Control key while you press the “a” key; at this point you can release the Control and “a” keys because you have alerted the Screen program that the next keystroke is intended for it. Now press the “c” key. For the sequence Ctrl-a + ” (double quote) sequence which shows a list of all open terminals in that screen session, do Ctrl-a, release those keys and then press shift plus the ” (double quote) at the same time. Use the Ctrl-a + Ctrl-a sequence which toggles between the most recent two terminal sessions. You must continue to hold down the Control key and press the “a” key twice. This sequence of keystrokes seems a bit complicated but I soon learned it as muscle memory and it is quite natural by now.
Perform this experiment in a terminal session as a non-root user. Any terminal emulator will work. Enter the screen command which will clear the display and leave you at a command prompt. You are now in the screen display manager with a single terminal session open and displayed in the window.
$ screen
You won’t see much change as there’s no indicator to tell you that the screen program is running. This is one of its best features; it just gets out of the way and lets you work.
Type any command such as ls to have something displayed in the terminal session besides the command prompt. You could use ls -l
which is one I use just to see something in the screen session.
Press Ctrl-a + c to open a new shell within the screen session and enter a different command, such as df -h in this new terminal session. Type Ctrl-a + a to easily switch between the two terminal sessions. Enter Ctrl-a + c to open a third terminal. Now type Ctrl-a + “ to list the open terminals.
Choose window 1 — session numbering starts at zero ( 0 ) — by using the up/dn arrow keys and hit the Enter key to switch to that terminal. To close the active terminal, type exit and press the Enter key.
Type the command Ctrl-a + “ to verify that the terminal is gone. Notice that the terminal with the number you have chosen to close is no longer there and that the other terminals have not been renumbered. To reopen a fresh terminal use Ctrl-a + c. This new terminal is assigned the lowest available number.
Type Ctrl-a + “ to verify that the new terminal has been created. Notice that it has been opened in the place of the terminal that was previously closed.
To disconnect from the screen session and all open terminals, press Ctrl-a + d. Note that this leaves all of the terminals and the programs in them intact and still running.
Enter the command screen -list command on the command line to list all of the current screen sessions. the screen -ls command does the same thing and is a bit shorter. Either way, this can be useful to ensure that you reconnect to the correct screen session if there are multiple ones.
Use the command screen –r to reconnect to the active screen session. If multiple active screen sessions are open, then a list of them will be displayed and you can choose the one to which you wish to connect; you will have to enter the name of the screen session to which you want to connect.
I recommend against opening a new screen session inside of an existing screen session. It can be difficult to switch between the terminals because the screen program does not always understand which of the embedded sessions to which to send the command.
I use the screen program all the time. It is a powerful tool that provides me with extreme flexibility for working on the command line.