Michael O.
5min Read

How To: Use Linux Screen command

HowTo: Use Linux Screen command

A bit about Linux Screen

Linux Screen lets you do amazing things. Using Screen, you can use multiple shell windows from a single SSH session. Linux Screen enables you to keep a shell active even through network disruptions.

You can disconnect and re-connect to shell sessions from multiple locations and run a long-running process without maintaining an active shell session.

One of the lesser known uses of Screen is as an online learning tool. You can share a Linux Screen session with another user in fully interactive mode. This allows them to watch you do a task or do the task themselves with you acting as the monitor.

At HOSTAFRICA, we use Screen as a tool every day.

Here are a few tips on screen usage from the internet:

Is Linux Screen installed?

Most RedHat and CentOS distributions should have Screen installed. Check this by issuing the “which screen” command. If the Screen path is not found, it is a simple matter to install. Under Ubuntu or Debian, use “apt install screen“. On CentOS or RedHat, use “yum install screen“.


Using Screen

Starting:

This is pretty easy. Just type “screen” and press enter. You will then find yourself in the Linux Screen shell which functions exactly as a normal shell, but with a few special properties.

Control Command:

A bit trickier – type “Ctrl-a” ( the control key and the lowercase “a” to initiate Screen commands. To see a list of commands, type “Ctr-a" then “?“. This gives you the following (to understand it all, type man screen“.)

                           Screen key bindings, page 1 of 1.

                           Command key: ^A   Literal ^A: a


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

^] 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

Key bindings are the commands the screen accepts after you hit “Ctrl-a”. You can reconfigure these keys to your liking using a .screenrc file, but the defaults work well for most.

Creating Windows:

To create a new window, you just use “Ctrl-a” “c”. This will create a new window for you with your default prompt.  Your old window is still active and will keep running whatever was running.

Switching Windows:

Screen allows you to move forward and back using “Ctrl-a” “n” (next). The windows are part of a loop, so you could end up back where you started with  “Ctrl-a” “n” or you could move back with “Ctrl-a” “p” (previous). Each process in each window will keep running until you either kill the process or the window.

Detaching from Screen:

This is the awesome part of Linux Screen. You can detach from a screen actively running a process (for example, a cPanel install) and re-attach later. Detach using “Ctrl-a” “d”. This will put you back into your normal terminal shell.

Re-Attach to Screen:

If your connection has been dropped or you have detached from a Screen, you can re-attach using “screen -r“. If you have multiple screen, you may see something like the following:

There are several suitable screens on:
 31638.pts-17.xxx-H47M-D1-B3 (25/03/2018 18:42:15) (Detached)
 31588.pts-21.xxx-H47M-D1-B3 (25/03/2018 18:41:45) (Detached)
 31533.pts-19.xxx-H47M-D1-B3 (25/03/2018 18:41:34) (Attached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

If you see this, just choose the one you want and specify it as follows: screen -r 31638.pts-17.xxx-H47M-D1-B3.

Maybe the screen you need to get to is still attached (see the 3rd screen session).

You can force it to detach and re-attach to it using: screen -d -r 31533.pts-19.xxx-H47M-D1-B3.

Logging your Linux Screen session:

This is trivially easy using “Ctrl-a” “H“. This will create a running log of your session. Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes.

Locking your screen session:

If you need to step away from your computer for a minute, you can lock your screen session using “Ctrl-a” “x”.  This will require a password to access the session again. The only danger is that the screen session will end if any party exits the session, as both parties will be kicked out.


Sharing a screen session

Start a screen session normally. Now let the student/colleague/partner login to the same machine via SSH and then type “screen -x“. They will join your screen session and have full interaction, being able to both type and see what you type as well. You will also be able to see everything they do.

All outputs of any software run will also be visible on both screen sessions. Using ACL settings in the etc/screenrc file or inline with the correct commands. Here you can then allow a user read-only access to a screen session to “observe” only.


Finishing your Screen session

Stop the session when you are done instead of saving it for later. To stop screen you can usually just type exit from your shell. This will close that screen window.

You have to close all screen windows to terminate the session. Once you have done this a message about Screen being terminated will appear. You can also exit with a normal “Ctrl-d”  or “Ctrl-a” “k” (kill).


In Summary

The screen command is extremely powerful and I encourage you t spend some time exploring “man screen” to see more of what is possible. Enjoy and use responsibly.

Happy hosting!


The Author

Michael O.

Michael is the founder, managing director, and CEO of HOSTAFRICA. He studied at Friedrich Schiller University Jena and was inspired by Cape Town's beauty to bring his German expertise to Africa. Before HOSTAFRICA, Michael was the Managing Director of Deutsche Börse Cloud Exchange AG, one of Germany's largest virtual server providers.

More posts from Michael