GNU Screen

Notice

The pairCloud service is ending on November 1, 2018.

The screen command serves a couple of very useful purposes. The first is to maintain a session even if you get disconnected. This is especially useful if you are working remotely for a prolonged period of time. The second purpose is to allow you to have multiple sessions open at the same time to the same server without opening a fresh connection for each one.

screen is an ongoing project and due to this different Linux distributions have different feature sets. This tutorial has been tested on Centos 6.6 and Ubuntu 14.04. Where a feature we are covering doesn’t exist in one but does in the other, this will be pointed out. Please note however that this may change after the date of publication. To check if the feature is available in your version of screen please see the man pages: man screen.

Basic operation.

To open a screen session type screen at the prompt. This will open a fresh screen session where you can begin working. screen will exist when you close the last shell you are using within screen. On Ubuntu you will now get a welcome screen which you can dismiss by hitting either space or enter. To permanently disable this welcome screen please edit your ~/.screenrc file and add this line.

startup_message off

All commands in screen start with CTRL+a then the command's shortcut key. In this tutorial we will shorten this to ^ac for example. This means CTRL+a then the c key.

Opening a new session/shell and changing sessions.

To open a new shell from within screen use the ^ac command. To go to the next session use ^an.

Using only these two options you can have multiple commands running and still have a shell to type new commands into. For instance once you open screen run the top then use ^ac to create a new session/shell. You can then do work in this shell and move back to the Top session by using ^an.

Splitting the Screen

To split the screen use the ^aS shortcut. This will split the screen horizontally. On Ubuntu 14 you can also split vertically by using ^a|. The | is the pipe symbol.
Once the screen is split you will need to create a new session or attach an existing one. To create a new session use the ^ac option. Please see the later section on attaching sessions.
To move to the next region use ^aTAB. To close a split use ^aX.

Listing sessions and assigning a name

You can have multiple screen sessions, to list all sessions you would use the command screen -list.

If you have only run screen once then only one will be listed and it will be called something like this.

6192.pts-0.ubuntu

This is the process id.The PTS-Numberhostname.
You can however change this using ^a:sessionname new_session_name

Reconnecting to a lost Session

If you are running screen and you for instance lose your internet connection. When you come back you can reconnect to that session.

root@ubuntu:~# screen -list
There is a screen on:
        6956.pts-0.ubuntu       (09/17/2015 03:47:19 PM)        (Detached)
1 Socket in /var/run/screen/S-root.

You cannot access any shells running in the Detached screen. To re-attach a screen you would use the -x option like this: screen -x 6956.pts-0.ubuntu.

If you had split screens running when you lost your connection you will need to rebuild them. To do this create the splits again using ^aS and ^a|. Then ^aTAB to each pane and use ^an to find the shell you were running in that pane.

Conclusion

These are the most useful of the screen features but it does have more features. To find out more please see the man pages for screen as follows: man screen.

Updated on August 21, 2018

Was this article helpful?

Related Articles