How to install and use screen on your Linux server

Screen is described by the GNU project as a terminal multiplexer.

Most often, people use screen for one of two reasons. First, it can be easily used to organize multiple terminal sessions within a single window. The second most popular use for it is to start a long running command on a remote server.

If you have an unreliable network, we recommend that you use screen, which will ensure your sessions are still running if you get disconnected.

Prerequisites

You need access to a Linux server, with sudoers or root privileges. Adding sudo to your account is outside the scope of this tutorial.

Installation

sudo apt-get update
sudo apt-get install screen

screen -v
Screen version 4.06.02 (GNU) 23-Oct-17

Using screen

Starting a screen session

To start a new screen, you could just type the screen command, alone, but it’s very useful to name your screen sessions. I recommend getting into the habit of naming your session when you start screen, like this:

screen -S your_session_name

If you start screen without naming your session, you’ll get an about message. Press ENTER to continue.

Now, your terminal doesn’t look much different.

To verify that you are inside of screen, let’s use a screen command to display the version. On your keyboard, press and hold the control key, then press lowercase a, let go of both, and press the v key in quick succession.

You should see the version of screen displayed at the bottom of your terminal screen.

You can now run Linux commands as you normally do.

ls -l

Screen commands

Pressing CTRL+a then pressing another key is how you control all of screen’s functions. To see the commands available, press CTRL+a, then the question mark key (?). You will see a list of commands. CTRL+a ? is probably a command you should memorize at first, so that you can refer back to the list to learn other commands.

As you can see on the help screen, there are many different commands you can give screen. The most popular are:

CTRL+A d: Detaches from screen, leaving it (and any command inside it) running. This is one of the primary uses of screen.
CTRL+a c: Creates a new numbered terminal inside your screen session.
CTRL+a |: The pipe symbol vertically splits your screen session into two different areas.
CTRL+a TAB: Switches between split regions of the screen.
CTRL+a n: Switches between your numbered terminals.

Splitting your screen

Let’s walk through an example of this.

Inside of screen, start the top command.

top

Create a new terminal using CTRL+a c. The terminal with your top command will disappear, and you will get a new terminal.

Press CTRL+a |. Your screen will show a visible split to the right of your cursor. Notice that at the bottom of the screen, you will now see 1 bash. Your newly created terminal is now terminal 1. Your top command was running in terminal 0.

Use the CTRL+a TAB command to switch to the other side.

Now press CTRL+a n. This will bring your original terminal into the right hand split side. You should see your top command running. At the bottom of this terminal you will see 0 bash.

Switch back and forth between top and your command line using CTRL+a TAB. You can use both to run virtually any Linux command you wish.

Detach from and re-attach to screen

Let’s detach from screen, leaving your top command running, and then get back in.

Press CTRL+a d. You’ll see a message about being detached from your screen session.

Let’s list the screen sessions on the remote computer:

screen -ls

You’ll see the sessions created displayed in a list.

2642.testsession    (11/22/19 21:24:59)    (Detached)

If you’ve only created one session, resuming your session is as easy as:

screen -r

If you’ve created multiple screen sessions, you’ll need to specify the process ID (PID) file for the one you want to connect to.

screen -r 2642

Note that when re-entering the screen, your window split settings from before have been lost. Your terminals are still all there, but you’ll have to re-set up your splits.

Exiting screen

To completely quit your screen session, you have to exit each created terminal.

Simply press CTRL+d or type exit at each command prompt. You might have to CTRL+a TAB to each split, if you have splits open.

Conclusion

Hopefully, this article will whet your appetite to the power of using screen to manage multiple terminals on remote servers, as well as leave long running commands active even if you log out.

Leave a Reply

Your email address will not be published. Required fields are marked *