Useful screen commands

Screen is a very useful tool allowing you to manage serveral virtual command lines. Here's my take on some of the more useful screen commands.

First you might want to enable the kaypad keys. I use them a lot, if you don't, then no need to add the following. To enable screen to use keypad keys (like xterm) and use the scrollback history, change /etc/screenrc and uncomment this line:

term xterm-color
termcapinfo xterm|xterms|xs|rxvt ti@:te@

Creating and navigating screens

To start a screen, go to xterm or the console. We'll start a new screen and give the screen a name for easy reference:

screen -S mysession

where mysession is the name you want to give this screen session. A lot info shows up, just hit enter.

Next, we want to get some help on screen. To get the screen help page in a session:

Ctrl + a ?

This is Ctrl and a at the same time, followed by ?. This displays the help info:

                     Screen key bindings, page 1 of 2.

                     Command key:  ^A   Literal ^A:  a

break       ^B b         license     ,            removebuf   =
clear       C            lockscreen  ^X x         reset       Z
colon       :            log         H            screen      ^C c
copy        ^[ [         login       L            select      '
detach      ^D d         meta        a            silence     _
digraph     ^V           monitor     M            split       S
displays    *            next        ^@ ^N sp n   suspend     ^Z z
dumptermcap .            number      N            time        ^T t
fit         F            only        Q            title       A
flow        ^F f         other       ^A           vbell       ^G
focus       ^I           pow_break   B            version     v
hardcopy    h            pow_detach  D            width       W
help        ?            prev        ^H ^P p ^?   windows     ^W w
history     { }          quit        \            wrap        ^R r
info        i            readbuf     <            writebuf    >
kill        K k          redisplay   ^L l         xoff        ^S s
lastmsg     ^M m         remove      X            xon         ^Q q


                Screen key bindings, page 2 of 2.

  ^]   paste .
  "    windowlist -b
  -    select -
  0    select 0
  1    select 1
  2    select 2
  3    select 3
  4    select 4
  5    select 5
  6    select 6
  7    select 7
  8    select 8
  9    select 9
  I    login on
  O    login off
  ]    paste .
  |    split -v
  :kB: focus prev

You can create a new screen from the current screen:

Ctrl + a c

To name the current screen:

Ctrl + a A

That's Ctrl + a followed by Shift + a. You'll see a text:

Set window's title to: bash

Change the current screen name to your liking.

To navigate from one screen to another:

Ctrl + a n
Ctrl + a p

To obtain a list of the active screens:

Ctrl + a " (double quote)

Output:

Num Name

   0 screen 1
   1 screen 2

If you want to change to a screen session using it's name, use:

Ctrl + a ' (single quote)

It's also possible to show more than one screen at the same time. To this end, we will split the current screen into horizontal regions:

Ctrl + a S

Output:


(SCREEN)[root@mymachine]$



0 screen 1

To navigate to the bottom part, use:

Ctrl + a TAB

You'll see the cursor appearing under the "0 screen 1" line. Use Ctrl + a " to open the list of screens, select the one you want to open in the bottom half and hit enter. Voila!

SCREEN)[root@mymachine]$


   0 screen 1
(SCREEN)[root@mymachine]$


   1 screen 2

To stop using the split screen, navigate to the screen you want to remove and execute the following:

Ctrl + a :remove

The screen is removed but not destroyed and the split screen mode is gone.

To exit screen mode, without destroying the active screens because that's what screen is all about:

Ctrl + a d

To resume the session, type screen -r followed by enter:

screen -r

This will show you the active screens if there is more then 1 active. Otherwise the command will immediately resume the active screen:

screen -r
There are several suitable screens on:
    31927.31911.test        (21-11-17 21:49:48)     (Detached)
    31911.test      (21-11-17 21:46:53)     (Detached)
    31791.pts-11.mymachine  (21-11-17 21:40:35)     (Attached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Another way to list the active screens:

screen -ls

Now if you want to rename the screen session, use the above command to resume the session you want to rename.

Next, in the screen session:

Ctrl + a :sessionname new-session-name

That's Ctrl + a followed by a colon and sessionname (literally), followed by the new session name. The result of the above command:

screen -ls

There are screens on:
    32316.new-session-name  (21-11-17 22:18:49)     (Detached)
    31927.31911.test        (21-11-17 21:49:49)     (Detached)
    31791.pts-11.mymachine  (21-11-17 21:40:36)     (Attached)

This can also be done without resuming the session.:

screen -S 32316.new-session-name -X sessionname new-session-name-2

where S is the session to rename, and -X is used to give the command and the new session name.

To quit a session from the main console:

screen -S 31791 -X quit

screen -ls

There are screens on:
    32316.new-session-name-2        (21-11-17 22:18:48)     (Detached)
    31927.31911.test        (21-11-17 21:49:48)     (Detached)
2 Sockets in /run/screen/S-root.

It's also possible to lock a screen session:

Ctrl + a x

There are couple of ways to enalbe logging the screen output to a file. To enable logging in the screen session:

Ctrl + a H

This creates a running log of the session named "screenlog.0". It's also captures the escape characters. Issue the command again to turn the logging off.

To create a screenshot called a hardcopy in screen terms:

Ctrl + a h

You'll see a message Screen image written to "hardcopy.0". The file hardcopy.0 will be placed in your home directory.

Another way to activate logging feature, you can add the parameter “-L” when you're creating the screen:

screen -L -S mybrandnewsession

This will also result in the creation of a logfile named "screenlog.O"


The article ended up a bit longer then expected but screen is a very useful tool!