Basic Guide to the Screen Command

by Henry Li

lhrli@ucdavis.edu

Table of Content

Background

To keep our server session alive and for the ease of the workflow, we can to use the screen command.

screen is a built-in command in almost all Linux-based terminals nowadays.

To check if your terminal has the screen command, run the following code:

screen --help

If your terminal has screen, you should see detailed documentation of all the flag usages.

Basic Concepts

There are three layers at play when you use the screen command on a server you are accessing through SSH:

  • SSH Session
  • Screen Session
  • Windows

The SSH session refers to remote connections with the server itself. It can be in various forms, ranging from terminal-based SSH connections to UI-based SFTP file managers like Cyberduck or FileZilla. Each form of connection counts as one SSH session and they are independent of each other, meaning you could be running one task on one connection but also downloading a file on another. We will focus on terminal-based SSH sessions in this guide.

This concept is irrelevant to using the screen command itself; it is important because it has the word “session” in it, which might be confusing with the screen sessions we will be working with later on.

The screen command allows you to create stand-alone “screen sessions” that you can leave temporarily and return to later.

You can have multiple screen sessions in a command-line SSH session, each performing different tasks simultaneously. For our purposes in BIS 180L, one screen session is more than enough for you to keep most commands running while getting a good night’s sleep, knowing that your computer screensaver won’t ruin it.

Screen sessions can be created, detached, and reattached. You can also “quit” a screen session, which removes the screen session completely, along with all windows within.

A window is the most basic layer. You can have multiple windows within a single screen that are doing different things.

Within a single screen session, windows can be created, renamed, split, and killed.

Managing Screen Sessions

As we just mentioned, screen sessions can be created, detached, and reattached. You can also get rid of a screen session by “quitting” it.

We will go over how to perform these actions in the following section.

Listing Screen Sessions

Once you are connected to the server through SSH, you might want to check if you have already made other screen sessions.

To check what screen sessions you have made earlier, use the -ls flag.

screen -ls

If you have previously made screen sessions, you should see a list of them in the following format; Here, the example refers to when you have previously made two screens called {screenName1} and {screenName2}:

There are screens on:
        someNumber1.screenName1       (Detached)
        someNumber2.screenName2       (Detached)
2 Sockets in ...

If you have not yet made a screen session on the server before, you should see the following instead:

No Sockets found in ...

Creating Screen Sessions

Now let’s make a new screen session using the following command:

screen -S {screenName}

Replace {screenName} with a name of your choice.

Once that is done, you should be taken directly to your new screen session.

Run screen with the -ls flag to see if it is “Attached” (you are looking at it directly).

screen -ls

You should see the screen session you have just created:

There is a screen on:
	      someNumber.{screenName}       (Attached)
1 Socket in ...

Detachment and Reattachment

To get out of this screen session you just created, use the following hotkey:

Ctrl + A, D

That is, you hit Control + A at the same time, lift all your fingers, and then hit the D key once. You will see many more hotkey combinations like this later in this guide.

You should now be out of the screen session. You can leave whatever you started running in that screen session and work on other things without waiting for it to finish or worrying about potentially disrupting it. The screen session is safely “Detached” (you are not looking at it directly, but it is still alive in the background).

To check it is still there, run the command with -ls flag again:

screen -ls

You should see that the screen session is now detached, but it still exists in the background:

There is a screen on:
	      someNumber.{screenName}       (Detached)
1 Socket in ...

To get back to this screen session, use the -r flag with the number assigned to it:

screen -r {someNumber}

Replace the {someNumber} with the actual screen session number in the output of your screen -ls command. This number is randomly assigned to your screen session when you created it (at least that’s how I think it’s done).

Since you have given your screen session a name {screenName}, you can also reattach to it by name:

screen -r {screenName}

Note: this might not work sometimes from my experience. It is guaranteed to work if you use {someNumber}, though.

Quiting Screen Sessions

To quit a screen session, that is to completely get rid of this screen session along with all the windows within (we will talk about windows later), use the -X flag and the command quit.

There are two ways of doing this:

If you want to quit a screen session that is attached (you are looking at it directly), use the following command:

screen -X quit

You can also quit your attached screen session by using the following hotkey:

Ctrl + A, \

Again, you need to hit Control + A at the same time, lift all your fingers, then hit the \ key once. From this point on, all similar hotkey combinations are done this way.

A prompt will show up asking for your confirmation:

Really quit and kill all your windows [y/n]

Type Y and you will quit the screen session.

[screen is terminating]

If you want to quit a specific screen session that is detached (you are not looking at it, it’s in the background), use the following command instead:

screen -X -S {screenName} quit

The -S flag specifies the screen session you want the quit command to act on.

You should now see the screen session with the assigned {someNumber} gone when you run screen -ls.

Managing Windows within a Screen Session

Like we’ve mentioned above, there can be multiple windows within a single screen session. Windows can be created, renamed, split, and killed.

We will go over how to perform these actions in the following section.

Make sure you are in an attached screen session. You can verify this by running screen -ls and see if your screen session has “(Attached)” after its name.

Creating Windows

When you create your screen session, you are taken into a window by default. This is window 0.

To create a new window within this screen session, use the following hotkey:

Ctrl + A, C

You should be brought to a new window, window 1, where you can do different things from window 0. Windows are given a temporary name based on your terminal settings. In our case, in our Linux-based server, all windows are named “bash”.

You can make many more windows, and the window list goes on.

To see a complete list of windows you have created in a screen session, use the following hotkey:

Ctrl + A, "

You should see a list of windows with their window numbers and names.

Use the up and down arrow keys to select the window you want to go to, then hit enter/return. You should now be brought to that window.

Renaming Windows

In the list view of all the windows, you should see they all have the same name “bash”.

To rename a specific window, you need to go into that window, and use the following hotkey:

Ctrl + A, A (capitalized)

That means you need to hit Control + A at the same time, lift all your fingers, then hit Shift + A at the same time.

You should see a prompt on the bottom of the window for you to make changes:

Set window's title to: bash

You can then edit the window name to your liking. To cancel renaming, just hit esc.

Once you are done, hit enter/return to confirm. The change should be reflected in the window list if you run Ctrl + A, " again.

Splitting Windows

We can split the screen to display multiple windows in parallel.

There are two ways to split, the horizontal split (windows stacked on top of each other) and the vertical split (windows placed side by side).

To perform a horizontal split, use the following hotkey:

Ctrl + A, S (capitalized)

That means you need to hit Control + A at the same time, lift all your fingers, then hit Shift + S at the same time.

To perform a vertical split, use the following hotkey:

Ctrl + A, |

The | key is called “pipe”, which you can find while holding Shift and pressing the \ backslash key.

Now your screen should be split into two halves.

No matter how you split your screen, your new half-window should have nothing on it.

To navigate to the new half-window, use the following hotkey:

Ctrl + A, TAB

You can use this hotkey to switch between all viewing windows. Just use it multiple times to jump around. You should see your caret moves as you switch windows.

In the new half-window, you can create a new window using Ctrl + A, C, or open an existing window using Ctrl + A, " and selecting the window to display.

Since windows are numbered, you can switch to a window directly using its window number:

Ctrl + A, 0...9

If you have named your windows and you want to switch to a specifically named window, you can do so with the following hotkey:

Ctrl + A, '

A prompt on the bottom of the screen will ask you for the name of the window you want to go to. Simply type the name in or the window number and hit enter/return and you should be taken directly there.

To unsplit a screen, use the following hotkey:

Ctrl + A, Q (capitalized)

You should be brought to a single-window view, where your current window is the window you were in when you used the hotkey.

Killing Windows

Perhaps you have created too many windows or you are simply not using a window anymore. You can kill a window, just like closing a window on a desktop.

There are two ways to kill a window.

Make sure you are in the window you want to kill (your caret is in that window); simply kill it by typing “exit” and hitting enter/return.

Another way to kill this window is to use the following hotkey, again while you are in that window, use:

Ctrl + A, K

A prompt will show up asking for your confirmation:

Really kill this window [y/n]

Type Y and the window will be closed. A prompt is displayed on the bottom of your screen, telling you what happened.

Window # (windowName) killed.

Note: If you have only one window left in your current screen session, Ctrl + A, K will quit the screen session entirely. Like before, it will prompt for your confirmation:

Really kill this window [y/n]

Type Y and you will close this last window. Since you have no windows left, you have effectively quitted the screen session.

[screen is terminating]