With the Linux screen control, you can push running terminal applications to the background and advance them when you want to see them. It also supports split screen displays and works on SSH connections, even after logging out and logging back in!
What is the screen command?
Here are the most common cases in which you would use the screen command, and we’ll cover them later in this article:
The standard operation is to create a new window containing a shell, to execute a order, then push the window in the background (called “detachment”). When you want to see how your process is going, you can again drag the window to the foreground (“attach”) and reuse it. This is ideal for long processes that you do not want to accidentally interrupt by closing the terminal window.
Once a screen session is running, you can create new windows and run other processes there. You can easily jump between the windows to follow their progress. You can also divide your terminal window into vertical or horizontal regions and display your different screen windows in a single window.
You can connect to a remote machine, start a screen session, and start a process. You can disconnect from the remote host, reconnect and your process will still be running.
You can share a screen session between two different SSH connections so that two people can see the same thing in real time.
To install the screen on ubuntu, use this command:
sudo apt-get installation screen
To install the screen on Manjaro, use the following command:
sudo pacman -Screen
On Fedora, you type the following:
sudo dnf installation screen
Getting started with the screen
To start the screen, simply type it as shown below and press Enter:
You will see a license information page. You can press the space bar to read the second page or press Enter to return to the command prompt.
You are left at the command prompt, and nothing seems to have happened. However, you are now running a shell inside a multiplexed terminal emulator. Why is this a good thing? Well, let’s start a process that will take a long time. We will download the source code of the latest Linux kernel and redirect it to a file called latest_kernel.zip.
To do this, we type the following:
curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.9.tar.xz> latest_kernel.zip
latest_kernel.zip “command in a terminal window.” width = “646” height = “77” onload = “pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);” onerror = “this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);” />
Our download starts and the curl output shows us the progress.
latest_kernel.zip “command in a terminal window.” width = “646” height = “247” onload = “pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);” onerror = “this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);” />
We cannot show you a picture of the next bit because it is a keystroke. You type Ctrl + A, release these keys, and then press d to detach the screen.
The download process is still in progress, but the window displaying the download is removed. You return to the terminal window from which you launched the screen session. A message informs you that a screen window called 23167.pts-0.howtogeek has been detached.
You need the number from the start of the window name to attach it. If you forget, you can always use the -ls (list) option, as shown below, to get a list of detached windows:
When you’re ready, you can use the -r (attach) option and the session number to attach it, like so:
screen -3 23167
The window that was running in the background is now brought back to your terminal window as if it had never left it.
If it is a process that will continue until its conclusion, it will end in the end. If this is an ongoing process, you may wish to end it. In both cases, at the end of the process, you can type exit to exit the screen. Alternatively, you can press Ctrl + A, then K to forcibly kill a window.
Type the following command:
You are returned to your previous terminal window, which will still display the command you used to attach the window. Because we closed our one and only detached window, we get a message that the screen ends.
Using named screen sessions
You can use the -S (session name) option to name your screen session. If you are using a memorable name rather than the numerical identity of the session, it is more convenient to reconnect to a session. We type the following to name our session “bigfile”:
screen -S bigfile
When the screen starts our session, we see an empty window with a command prompt. We are going to upload a large file, so we can use a lengthy process as an example.
We type the following:
curl http://ipv4.download.thinkbroadband.com/1GB.zip> bigfile.zip
bigfile.zip “command in a terminal window.” width = “646” height = “77” onload = “pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);” onerror = “this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);” />
When the download starts, we press Ctrl + A, then D to detach the session. We type the following to use the -ls (list) option with screen to see the details of our individual session:
Behind the numeric identifier (23266), we see the name of our session (bigfile). We enter the following, including the name of the session, to attach it:
screen -r bigfile
We are reconnected to our download window and find that the long download is still in progress.
Once the download is complete, we type exit to close the session window.
Using the screen with multiple windows
So far we have used the screen to place a single process in the background in a detached window. However, the screen is capable of doing much more than that. Then we will run a few processes that will allow us to monitor certain aspects of our computer.
We type the following to start a screen session called “monitor”:
screen -S monitor
At the command prompt of our new window session, we will launch dmesg and use the options -H (human readable) and -w (wait for new messages). This will display kernel buffer messages; new messages will appear as they occur.
We type the following:
dmesg -H -w
Existing messages appear. We did not return to the command prompt because dmseg is waiting for new messages and will display them as soon as they arrive.
We want to run another application, so we need a new screen window. We press Ctrl + A, then C to create a new window. We will use the watch to run repeatedly vmstat, so we get a frequent update display virtual memory usage on our computer.
At the new command prompt, we type the following:
The vmstat output appears and updates every two seconds.
Both of our processes are running. To jump between screen windows, you press Ctrl + A and the window number. The first one we created is window zero (0), the next one is window 1, and so on. To jump to the first window (that of dmesg), we press Ctrl + A and 0.
If we press Ctrl + A and 1, this brings us back to the vmstat window.
It’s pretty cool! We can press Ctrl + A, then D to break away from this session; we can reattach it later. Both sessions will still be running. Again, to switch between windows, we press Ctrl + A and the number (0 or 1) of the window to which we want to switch.
Let’s go to the next step and view the two screens in one window. When you do this, you stretch your terminal window to a size which makes this step useful. Our examples are limited to the size of our screenshots, so our windows will look a bit cramped.
To do this, we press Ctrl + A, then Shift + S (a capital “S” is required).
The window is divided into two “regions”.
The upper region always displays vmstat and the lower region is empty. The cursor is highlighted in the screenshot below. To move it to the lower region, we press Ctrl + A, then Tab.
The cursor moves to the lower region, which is really just empty space. It’s not a shell, so we can’t type anything into it. To get a useful display, we press Ctrl + A, then “0” to display the dmesg window in this region.
This gives us the two live outputs in a split window. If we press Ctrl + A and D to detach the window and then reattach it, we will lose the view of the split pane. However, we can restore it with the following keyboard shortcuts:
Ctrl + A, S: Divide the window horizontally.
Ctrl + A, Tongue: Move to the lower region.
Ctrl + A, 0: Display the zero window in the lower region.
We can go even further. We will now divide the bottom pane vertically and add a third process to the display. With the cursor in the lower region, we press Ctrl + A and C to create a new window with a shell inside. The lower region displays the new window and gives us a command prompt.
Then we run the df command to check file system usage:
When we see df running, we hit Ctrl + A and the pipe character (|). This divides the lower region vertically. We press Ctrl + A and Tab to move to the new region. Then we press Ctrl + A and 0 to display the dmesg window.
You can also move from region to region and add more vertical or horizontal divisions. Here are some more useful key combinations:
Ctrl + A: Go back and forth between the current and previous regions.
Ctrl + A, Q: Close all regions except the current region.
Ctrl + A, X: Close the current region.
Using the screen via SSH
With the screen, you can start a window session, detach it so that it always runs in the background, disconnect or reconnect, and then re-attach the session.
Let’s do an SSH connection to our computer from another with the ssh command. We must provide the name of the account with which we will connect and the address of the remote computer.
For our example, we type the following:
Once we authenticate on the remote computer and connect, we type the following to start a screen session called “ssh-geek”:
screen -S ssh-geek
For demonstration purposes, we will run at the top in the screen window, but you can start any long or endless process.
We type the following:
Once the top runs in the window, we hit Ctrl + A, then D to detach the window.
We return to the original remote terminal window.
If we hit exit, as shown below, it disconnects the SSH session and we’re back on our local computer:
We type the following to reconnect:
After being reconnected and connected, we can type the following to attach the screen session:
screen -r ssh-geek
We are now reconnected to our still active instance of top.
It’s fine if you want to start a process on one machine and then pick up where you left off on another.
Share a screen session
You can also use a screen session to allow two people to see and interact with the same window. Suppose someone running Fedora on their computer wants to connect to our Ubuntu server.
He would type the following:
Once connected, it starts a screen session called “ssh-geek” using the -S option (session name). It also uses the -d (detach) and -m (forced creation) options to create a new screen session that is already detached.
He types the following:
screen -d -m -S ssh-geek
He types the following, using the -X option (multi-screen mode) to attach the session:
screen -X ssh-geek
On a Manjaro computer, another person logs into the Ubuntu computer with the same account credentials, as shown below:
Once connected, it types the screen command and uses the -X option (multi-screen mode) to join the same window session, like this:
screen -X ssh-geek
Now whatever anyone types, the other person will see. For example, when a person issues the date command, they both see it as it is typed, as well as its output.
The two people are now sharing a screen session that runs on a remote Ubuntu computer.
For software that was launched in 1987, screen still offers a good boost in productivity. Familiarizing yourself with this will be time well spent!