If you’re logging onto a service or running remote command line operations over a network link via the Secure Shell (SSH) protocol, the last thing you need is for your session to be cut off by a faulty connection. This scenario is all too common – but for Linux users, the Screen utility can prevent it from occurring.
The tool also enables you to create multiple screen windows from a single session, navigate between windows, detach and resume screen sessions, and customize your screen terminal. Here’s how.
Installing The Screen Utility
These days, the Screen or GNU Screen package comes already installed on most Linux distributions. At the $ prompt, you can type the following to check if the utility is on your distro:
$ screen –version
The output might look something like this:
Screen version 4.06.02 (GNU) 23-Oct-17
If the “which screen version” query doesn’t return any details, the utility probably isn’t on your system. In this case you can usually install it using one of the following commands:
For Linux Screen on Ubuntu and Debian, type:
sudo apt install screen
For Linux Screen on CentOS and Fedora, the syntax is:
sudo yum install screen
Otherwise, consult the documentation for the package manager of your distro.
Starting A Screen Session
To start a screen session within your SSH session, you simply type:
screen
This will open a screen session, create a new window, and start a shell in that window. To get a list of commands, type:
Ctrl+a ?
Managing Multiple Screen Sessions
Screen is a “terminal multiplexer,” allowing you to run many terminal sessions within a single SSH session, detach from them, and reattach them as required.
If you intend to run more than one screen session, it’s a good idea to give each of them a meaningful name, so that you can easily identify which tasks they are specifically handling. To create a named session, run the screen command with the following arguments:
screen -S session_name
So if you were starting a session you wanted to call “slow build”, at the $ prompt, you would use a command like this to name it:
$ screen -S slow-build
To create a new window with a shell in it, type Ctrl+a c, and the first available number from the range 0 to 9 will be assigned to it.
Some Common Commands For The Screen Tool
Some of the most common commands for managing Linux Screen windows include:
- Ctrl+a c This creates a new window (with shell)
- Ctrl+a ” Lists all existing windows
- Ctrl+a 0 Switches to window 0 (or whichever number you specify)
- Ctrl+a A Renames the current window
- Ctrl+a S Splits the current region horizontally into two separate regions
- Ctrl+a | Splits the current region vertically into two regions
- Ctrl+a tab Switches the input focus to the next region
- Ctrl+a Ctrl+a Toggles between the current region and the previous one
- Ctrl+a Q Closes all regions except the current one
- Ctrl+a X Closes the current region
Information You Can Get From Screen Listings
With the Screen utility, you can start a screen session, then open any number of windows or virtual terminals inside that session. Processes running in Screen will continue to run when their windows are not visible – even if you get disconnected. If you have multiple screen sessions running and you want to detach from the current screen and reattach to another, you’ll first have to choose the session you want from a list.
The screenshot below illustrates the kind of result you might get after typing in the Screen list command screen -ls:
(Image source: NetworkWorld.com)
Detaching And Reattaching From Linux Screen
At any time during a session, you can detach from the current screen by typing:
Ctrl+a d
Any program running in that screen session will continue to run after you detach from it.
If you have multiple screen sessions running on your machine, you’ll need to add the screen session ID after the r switch of the “reattach” command. In other words, reattaching to a particular session requires that you supply the name that you initially assigned to it. For example, to reattach to the “slow build” session you would type:
$ screen -r slow-build
Any process you left running there while you were working on another session should have continued processing while “slow build” was detached. If you now use the list command to ask about your screen sessions, you should see that the session you’re currently reattached to is once again “attached.”
(Image source: NetworkWorld.com)
Customization Options For Linux Screen
When the Screen utility starts up, it reads its configuration parameters from the file /etc/screenrc and ~/.screenrc if that file is present on your system. You can edit the .screenrc file to modify the default Screen settings to suit your preferences.
The screenshot below gives an example ~/.screenrc configuration, with a customized status line and some additional options:
(Image source: Linuxize.com)
The Linux Screen tool is especially useful if you need to run any time-consuming processes that could get interrupted if your SSH session disconnects prematurely for any reason. This screenshot gives a recap of some of the commands used above:
(Image source: NetworkWorld.com)