Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.

Screen allows you to switch between terminals in which processes are running without interrupting them. This is especially effective when you need to build / install software and monitor disk space or use hardware resources.

Screen can split the current terminal into several windows with the ability to launch the same or other terminals in them. This further simplifies the above task, especially when using large monitors.

Installing screen

To install screen on ubuntu, use this command:

apt-get install screen

To install screen on centos, use this command:

yum install screen

Getting Started with screen

To start screen, simply type it as shown below and hit Enter:

screen

screen

You’re left at the command prompt, and nothing much seems to have happened. However, you’re now running a shell inside a multiplexed terminal emulator. Why is this a good thing? Well, let’s start a process that’s going to take a long time to complete. We’ll download the source code for the latest Linux kernel and redirect it into a file called latest_kernel.zip.

curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.13.4.tar.xz > latest_kernel.zip

Our download begins, and the curl output shows us the progress.

screen

To detach from the current screen, use the key combination “Ctrl + A” and then press the “d” key.

screen

The download process is still running but the window showing the download is hidden. You’re returned to the terminal window from which you launched the screen session. A message tells you that a screen window labeled 48079.pts-0.TechSupport has been detached.

You need the number from the start of the window name to reattach it. If you forget it, you can always use the -ls (list) option, as shown below, to get a list of the detached windows:

screen -ls

When you’re ready, you can use the -r (reattach) option and the number of the session to reattach it, like so:

screen -r 48079

The window that’s been working away in the background is now brought back to your terminal window as if it had never left.

screen

If it’s a process that’s going to run through to its conclusion it will eventually complete. If it’s a continual process, you’ll eventually want to terminate it. Either way, when the process ends, you can type exit to exit from the screen. Alternatively, you can press Ctrl+A, and then K to forcibly kill a window.

screen

You’re returned to your previous terminal window, which will still show the command you used to reattach the window. Because we closed our one and only detached window, we get a message that screen is terminating.

Using named Screen sessions

You can use the -S (session name) option to name your screen session. If you use a memorable name rather than the numerical identity of the session, it’s more convenient to reconnect to a session. We type the following to name our session “bigfile”:

screen -S bigfile

When screen launches our session, we see a blank window with a command prompt. We’re going to download a big file, so we can use a long-running process as an example.

We type the following:

curl https://speed.hetzner.de/10GB.bin > bigfile.zip

When the download starts, we press Ctrl+A, and then press D to detach the session. We type the following to use the -ls (list) option with screen to see the details of our detached session:

screen -ls

screen

Behind the numeric identifier (48804), we see the name of our session (bigfile). We type the following, including the session’s name, to reattach it:

screen -r bigfile

We’re reconnected to our download window and see the long download is still in progress.

screen

Using Screen with multiple windows

So far, we’ve used screen to place a single process in the background in a detached window. However, screen is capable of doing much more than that. Next, we’ll run a few processes that allow us to monitor some aspects of our computer.

We type the following to start a screen session called “monitor”:

screen -S monitor

At the command prompt in our new window session, we’ll launch dmesg and use the -H (human-readable) and -w (wait for new messages) options. This will display the kernel buffer messages; new messages will appear as they occur.

We type the following:

dmesg -H -w

The existing messages appear. We’re not returned to the command prompt because dmseg is waiting for new messages, and will display them as they arrive.

We want to run another application, so we need a new screen window. We press Ctrl+A, and then C to create a fresh window. We’re going to use watch to repeatedly run vmstat, so we get a frequently updated display of the virtual memory usage on our computer.

At the new command prompt, we type the following:

watch vmstat

The vmstat output appears and updates every two seconds.

Our two processes are now running. To hop between the screen windows, you press Ctrl+A, and the number of the window. The first one we created is window zero (0), the next is window 1, and so on. To hop to the first window (the dmesg one), we press Ctrl+A and 0.

If we press Ctrl+A and 1, it takes us back to the vmstat window.

That’s pretty nifty! We can press Ctrl+A, and then D to detach from this session; we can reattach later. Both sessions will still be running. Again, to switch between the windows, we press Ctrl+A and the number (0 or 1) of the window we want to switch to.

Let’s go to the next step and view both screens in one window.

To do this, we press Ctrl+A, and then Shift+S (a capital “S” is required).

The window splits into two “regions.”

screen

The top region still displays vmstat, and the bottom region is blank. The cursor is highlighted in the screenshot above. To move it to the lower region, we press Ctrl+A, and then Tab.

The cursor moves to the lower region, which really is just an empty space. It isn’t a shell, so we can’t type anything in it. To get a useful display, we press Ctrl+A, and then press “0” to display the dmesg window in this region.

screen

This gives us both live outputs in one split window. If we press Ctrl+A and D to detach the window, and then reattach it, we’ll lose the split-pane view. However, we can restore it with the following keyboard shortcuts:

Ctrl+A, S: Split the window horizontally.
Ctrl+A, Tab: Move to the lower region.
Ctrl+A, 0: Display window zero in the lower region.

We can take things even a step further. We’ll now split the lower 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 in it. The lower region displays the new window and gives us a command prompt.

Next, we run the df command to check file system usage:

df

When we see df running, we hit Ctrl+A and the pipe character (|). This splits the lower region vertically. We press Ctrl+A and Tab to move to the new region. Next, we press Ctrl+A and 0 to display the dmesg window.

screen

You can also move from region to region, and add more vertical or horizontal splits. Here are some more useful key combinations:

Ctrl+A: Hop back and forth between the current and previous regions.
Ctrl+A, Q: Close all regions except the current one.
Ctrl+A, X: Close the current region.

Sharing a Screen session

You can also use a Screen session so that two people can see and interact with the same window. Let’s say someone wants to connect to our Ubuntu server.

He would type the following:

ssh [email protected]

After he’s connected, he starts a screen session called “ssh-share” using the -S (session name) option. He also uses the -d (detach) and -m (enforced creation) options to create a new screen session that’s already detached.

He types the following:

screen -d -m -S ssh-share

He types the following, using the -x (multiscreen mode) option to attach the session:

screen -x ssh-share

On another computer, another person connects to the Ubuntu computer with the same account credentials, as shown below:

ssh [email protected]

Once he’s connected, he types the screen command and uses the -X (multiscreen mode) option to join the same window session, like so:

screen -X ssh-share

Now, anything either person types, the other will see. For example, when one person issues the nslookup command, they both see it as it’s typed, as well as its output.

Both people are now sharing a screen session that’s running on a remote Ubuntu computer.

For a piece of software that first saw the light of day in 1987, screen still packs a good productivity wallop. Familiarizing yourself with it will be time well spent!