How to Use Linux’s screen Command

Key Takeaways

  • The Linux screen command is a versatile tool that allows you to run terminal applications in the background and switch back to them when needed.
  • It supports split-screen displays and can be used over SSH connections, even after disconnecting and reconnecting.
  • With screen, you can create new windows, run multiple processes, detach and reattach sessions, and share sessions between multiple users in real-time.


With the Linux screen command, you can push running terminal applications to the background and pull them forward when you want to see them. It also supports split-screen displays and works over SSH connections, even after you disconnect and reconnect!


What Is the screen Command?

The screen command is a terminal multiplexer, and it’s absolutely packed with options. To say it can do a lot is the granddaddy of understatements. The man page runs to over 4,100 lines.

The following are the most common cases in which you would use the screen command, and we’ll cover these further in this article:

  • The standard operation is to create a new window with a shell in it, run a command, and then push the window to the background (called “detaching”). When you want to see how your process is doing, you can pull the window to the foreground again (“reattach”) and use it again. This is great for long processes you don’t want to accidentally terminate by closing the terminal window.
  • Once you’ve got a screen session running, you can create new windows and run other processes in them. You can easily hop between windows to monitor their progress. You can also split your terminal window into vertical or horizontal regions, and display your various screen windows in one window.
  • You can connect to a remote machine, start a screen session, and launch 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 two people can see the same thing, in real-time.

Install Linux screen

To install screen on ubuntu, use this command:

sudo apt-get install screen

sudo apt-get install screen in a terminal window

To install screen on Manjaro, use the following command:

sudo pacman -Sy screen

sudo pacman -Sy screen in a terminal window

On Fedora, you type the following:

sudo dnf install screen

sudo dnf install screen in a terminal window

Getting Started with Linux screen

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

screen

screen in a terminal window

You’ll see a page of license information. You can press the Space bar to read the second page or Enter to return to the command prompt.

screen license information in a terminal window

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.

To do so, we type the following:

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

curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.9.tar.xz > latest_kernel.zip in a terminal window

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

output from curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.9.tar.xz > latest_kernel.zip in a terminal window

We can’t show you an image of the next bit, because it’s a keystroke sequence. You type Ctrl+A, release those keys, and then press d to detach the screen.

The download process is still running but the window showing the download is removed. You’re returned to the terminal window from which you launched the screen session. A message tells you that a screen window labeled 23167.pts-0.howtogeek 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

screen -ls in a terminal window

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

screen -r 23167

screen -3 23167 in a terminal window

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

reattached screen session restored to the terminalwindow

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.

Type the following command:

exit

exit in a terminal window

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.

screen is terminating message in a terminal window

Detach from Linux Screen Session and Other Shortcuts

We briefly touched on how to detatch yourself from a screen session, but it bears repeating. We’ve also thrown in some other common shortcuts you should get comfortable with.

When you’re using screen, shortcuts will usually take the form Ctrl + a followed by a letter. Pressing Ctrl + a basically tells screen: “You need to look for another input now.”

Main Keys

Second Key

Action

Ctrl + a

d

Disconnect from the current screen

Ctrl + a

View all of the available screen windows

Ctrl + a

1,2,3…9

Connect to a screen with a particular number

Ctrl + a

i

View information about the current screen

Ctrl + a

k

Kills the current window

Ctrl + a

\

Kills all windows and terminates screen

This isn’t an exhaustive list, but it should get you through most of what you need to do. We’ll also be covering more keybinds that are handy for specific scenarios later in the article. There is more detailed documentation over on the official GNU screen website, if you want to look through the entire list of keybinds.

Using Named screen Sessions on Linux

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

screen -S bigfile in a terminal window

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 http://ipv4.download.thinkbroadband.com/1GB.zip > bigfile.zip

curl http://ipv4.download.thinkbroadband.com/1GB.zip > bigfile.zip in a terminal window

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 -ls in a terminal window

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

screen -r bigfile

screen -r bigfile in a terminal window

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

curl download output in a reattached screen session in a terminal window

When the download is complete, we type exit to close the session window.

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

screen -S monitor in a terminal window

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

dmesg -H -w in a terminal window

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.

dmsesg output in a terminal window

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

watch vmstat in a terminal window

The vmstat output appears and updates every two seconds.

vmstat output in a terminal window

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.

returning to the dmesg screen in a terminal window

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

retutrning to the vmstat screen in a terminal 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. When you do this, you’ll stretch your terminal window to a size that makes this step useful. Our examples are constrained to the size of our screenshots, so our windows will look a little cramped.

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

The window splits into two “regions.”

screen window session split into two horizontal regions in a terminal window

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

screen window session split into two horizontal regions with cursor in the bottom region in a terminal window

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 session with horizontal split pane showing a different process in each region in a terminal window

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.

screen session with horizontal split pane showing a new window the lower region in a terminal window

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

df

screen session with horizontal split pane with df typed in the lower region in a terminal window

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 session showing three panes in a single terminal window

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.

Using screen Over SSH

With screen, you can start a window session, detach it so it’s still running in the background, log off or back in, and reattach the session.

Let’s make an SSH connection to our computer from a different one with the ssh command. We have to provide the name of the account with which we’re going to connect and the address of the remote computer.

For our example, we type the following:

ssh [email protected]

ssh dave@192.168.4.30 in a terminal window

After we authenticate on the remote computer and log in, we type the following to start a screen session called “ssh-geek”:

screen -S ssh-geek

screen -S ssh-geek in a terminal window

For demonstration purposes, we’ll run top in the screen window, but you could start any long-running or endless process.

We type the following:

top

sudo apt-get install screen in a terminal window

Once top is running in the window, we hit Ctrl+A, and then D to detach the window.

top running in a screen window in a terminal window

We’re returned to the original, remote terminal window.

User returned to their original terminal window

If we type exit, as shown below, it disconnects the SSH session and we’re back on our local computer:

exit

exit in a terminal window

We type the following to reconnect:

ssh [email protected]

ssh dave@192.168.4.30 in a terminal window

After we’re reconnected and logged in, we can type the following to reattach the screen session:

screen -r ssh-geek

screen -r ssh-geek in a terminal window

We’re now reconnected to our still-running instance of top.

top in a terminal window

This is great if you want to start a process on one machine, and then pick up wherever you left off on another.

Sharing a screen Session

You can also use a screen session to allow two people to see and interact with the same window. Let’s say someone running Fedora on his computer wants to connect to our Ubuntu server.

He would type the following:

ssh [email protected]

screen -S ssh-geek in a terminal window

After he’s connected, he starts a screen session called “ssh-geek” 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-geek

screen -d -m -S ssh-geek in a terminal window

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

screen -x ssh-geek

screen -X ssh-geek in a terminal window

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

ssh [email protected]

ssh dave@192.168.4.1 in a terminal window

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

screen -X ssh-geek

screen -X ssh-geek in a terminal window

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

date in a terminal window

date in a terminal window

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!

Source

      Guidantech
      Logo
      Shopping cart