Never Ending Security

It starts all here

Screen Commands Cheatsheet

Screen Command Examples

Screen is a full-screen software that can be used to ‘switch’ your console between several processes. You can open several terminal instances inside one terminal window, detach a long running process from a session and then attach it back at a later time…. Screen is a very useful piece of software and it comes with most Linux distribution. If, for some reason you need to install it, you can do that with:
# apt-get install screen

Start screen for the first time

When you type ‘screen’ at the command prompt, screen will show you the interface exactly like the command prompt. ~ $ screen

Show screen parameter

With screen you can do all your work as you are in the normal CLI environment

Ctrl-A” and “?” without quotes will print all parameters.

 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 .

To get out of the help screen, you can press “Space” button or “Enter“.

Detach the screen

One of the biggest advantages of screen commands is option to detach current session. If you are doing some time intensive task during your SSH session [ie], you can detach the session. ~ $ screen ~ $ sudo apt-get install dpkg

Sample Output

Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following packages will be upgraded:
1 upgraded, 0 newly installed, 0 to remove and 1146 not upgraded.
Need to get 2,583 kB of archives.
After this operation, 127 kB of additional disk space will be used.
Get:1 testing/main dpkg i386 1.16.10 [2,583 kB]
47% [1 dpkg 1,625 kB/2,583 kB 47%]                                        14,7 kB/s

While download is in progress you can press “Ctrl-A” and “d“. The output will be:

[detached from 5561.pts-0.n0where] ~ $

Re-attach the screen

After you detached the screen you can easy return to that session with: ~ $ screen -r

If you have more than 1 screen session, you need to type the screen session ID. Use screen -ls to see how many screen are available. ~ $ screen -ls
SAMPLE OUTPUT ~ $ screen -ls
There are screens on:
        7849.pts-0.n0where (09/06/2014 01:50:45 PM)        (Detached)
        5561.pts-0.n0where (09/06/2014 11:12:05 AM)        (Detached)
2 Sockets in /var/run/screen/S-punk

If you want to restore screen 7849.pts-0.n0where, then type this command. ~ $ screen -r 7849

Using Multiple Screen

With ‘screen’, you can also have nested screens.

Switching between screens

When you do nested screen, you can switch between screens using command “Ctrl-A” and “n“. It will be move to the next screen. When you need to go to the previous screen, use “Ctrl-A” and “p“.

To create a new screen window, use “Ctrl-A” and “c“.

Logging whatever you do

Sometimes it is important to record your console.

With screen logging, you don’t need to write down every single command that you have done. To activate screen logging function, just press “Ctrl-A” and “H“.

At the bottom left of the screen, you will receive a notification: Creating logfile “screenlog.0“. You will find screenlog.0 file in your home directory. This feature will append everything you do while you are in the screen window to the file. To close screen logging , press “Ctrl-A” and “H” again.

Another way to activate logging feature, is to add the parameter “-L” when running screen. The command will be like this. ~ $ screen -L

Lock screen

Screen also have shortcut to lock the screen. You can press “Ctrl-A” and “x” shortcut to lock the screen.

Screen used by Darth Ra on n0where.

Use your password to unlock it.

Add password to lock screen

For security reason, you may want to put a password to your screen session. This password will be used whenever you want to re-attach the screen. This password is different than Lock Screen password.

To make your screen password protected, you can edit “$HOME/.screenrc” file. If the file doesn’t exist, you can create it manually. The syntax will be like this.

password crypt_password

To create “crypt_password” above, you can use “mkpasswd” command on Linux. Here’s the command with password “punk123“. ~ $ mkpasswd punk123

mkpasswd will generate a hash password as shown above. Once you get the hash password, you can copy it into your “.screenrc” file and save it. So the “.screenrc” file will be like this.

password xxxxxxxxxxx

Next time you run screen and detach it, you will need your password when you try to re-attach it ~ $ screen -r 5741
Screen password:

After you implement this screen password and you press “Ctrl-A” and “x” , then the output will be:

Screen used by Darth Ra on n0where.
Screen password:

First password is your Linux password, and the second password is the password that you put in your .screenrc file.

Leaving Screen

You can use “Ctrl-A” and “d” to detach the screen or you can send exit command to terminate the screen. There’s also “Ctrl-A” and “K” which kills the screen.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s