Have you ever experienced the frustration of working on the command line via an SSH connection, only to have the connection abruptly drop and your work is lost? Have you ever wanted a convenient way to execute multiple programs from the command line without the need to open separate SSH connections for each command? There is a utility called screen that allows us to resume the sessions and manage multiple shell sessions from a single ssh session.
Linux screen is a versatile tool that addresses these challenges and enhances your command line experience. With Linux screen, you can create and manage multiple terminal sessions within a single shell. In this blog, we will show you how to install and use screen on Linux servers.
Screen allows you to create multiple terminal sessions within a single window. Each session operates independently, so you can switch between sessions, run different commands in each session, and easily organize your work.
One of the key features of screen is the ability to detach from a session without terminating the processes running inside it. This means you can disconnect from a remote server or close the terminal window, and your processes will continue to run in the background. Later, when you reconnect, you can reattach to the session and continue where you left off.
Screen is particularly useful for running long-running processes that you don't want to be interrupted if your connection is lost or if you need to log out. By running your process in a screen session, you can safely disconnect and reconnect later to check on the progress or interact with the process.
Screen allows you to split your terminal window into multiple regions, each displaying a different session or process. This feature is useful when you want to monitor multiple processes simultaneously or switch between them quickly.
In this tutorial, we will use Unbuntu 20.04. Screen is often installed by default on Ubuntu. You can check if it is installed on your system by the following command.
screen --version
If you don’t have screen installed on your system, you can also use apt to update your package sources and install screen.
sudo apt update sudo apt install screen
sudo yum install screen
which screen
Start a new screen session by running the screen command.
screen
You'll be shown the following page. Press space to continue, and you will see the a new screen window.
It is also possible to start a session and name it. This feature comes handy when you need to run multiple screen sessions. Run the following command to start a named session and be sure to replace the session_name with a real name.
screen -S session_name
Screen is mainly controlled through keyboard shortcuts. Every keyboard shortcut for screen is prefaced with Ctrl-a (hold the control key while pressing the "a" key). That sequence of keystrokes tells screen that it needs to pay attention to the next keys we press.
To create a window in the screen, use the Ctrl-a c. Then, run Ctrl-a w to list all the windows currently opened. The output shows there are 4 windows. Each window has a number and the windows are numbered starting at "0". The current window has an asterisk next to the number.
You can rename the windows by using Ctrl-a A. As you can see in the following screen shot, we have renamed the number 2 window from bash to newname. Then, you can switch among the windows by using Ctrl-a n, Ctrl-a p, and Ctrl-a Ctrl-a. It's also possible to switch to a specific window by using Ctrl-a window_number. For example, Ctrl-a 1.
To kill a window or kill all windows, you can use Ctrl-a k and Ctrl-a \ respectively. You will be prompted to confirm the action as below. Enter y to close windows. We will lose any windows we have created and any unfinished work.
You can detach from the screen session at any time by typing Ctrl-a -d. Unlike the kill feature, this feature allows your programs in the screen instance to continue to run in the background, but it gives you access back to the session.
After the detachment, we can use screen -ls to list the screens that are currently running. From the below screenshot, you can see there is one screen is detached and running in the background.
Then, you can reattach a window by running screen -r id_number. For example, screen -r 23496
Ctrl-a c: It create a new windows.
Ctrl-a w: It display the list of all the windows currently opened.
Ctrl-a A: It rename the current windows. The name will appear when you will list the list of windows opened with Ctrl-a w.
Ctrl-a n: It go to the next windows.
Ctrl-a p: It go to the previous windows.
Ctrl-a Ctrl-a: It back to the last windows used.
Ctrl-a k: It close the current windows (kill).
Ctrl-a \:It close all windows (kill).
Ctrl-a S: It split the current windows horizontally. To switch between the windows, do Ctrl-a Tab.
Ctrl-a |:It split the current windows vertically.
Ctrl-a X: Close active Split window
Ctrl-a Q: Close all Split windows
Ctrl-a d: It detach a screen session without stopping it.
screen -r: It reattach a detached screen session.
If you need help on the screen keys, simple press the shortcut keys Ctrl-a ?.
When screen is started, it reads its configuration parameters from /etc/screenrc and ~/.screenrc if the file is present. We can modify the default Screen settings according to our preferences using the .screenrc file. You can issue the following command to edit the configuration file.
cd /etc nano screenrc
When you’re done editing, save and close the file. If you’re using nano, you can do so by typing CTRL+X and then y and ENTER to confirm.
In this tutorial, you learned how to install and use the GNU screen utility. You can now use several shortcut keys to create screen, switch screen windows, and killing screen windows, detach or reattach screens, and customize the screen settings. using screen on Linux enhances productivity, provides session resilience, enables remote access, facilitates collaboration, and offers efficient terminal window management.