Never Ending Security

It starts all here

Tag Archives: Screen

Linux super-duper admin tools: screen


Time to learn about yet another cool little admin application that will change the way you think and work. We had strace, a mighty, versatile debugging tool that helped us diagnose and categorize system programs quickly and effectively and point us in the right direction in our investigation of problems. We had OProfile, a powerful profiling utility that can be used to time the system and application performance and identify chokepoints and bottlenecks in program executions. Time to step back and appraise screen.

Teaser

screen

screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the legendary DEC VT100 terminal.

Additionally, the utility has insert/delete line, support for multiple character sets, a scrollback history buffer for each virtual terminal, and a copy-and-paste mechanism that allows moving text regions between windows.

When screen is called, it creates a single window with a shell in it and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new full-screen windows with other programs in them, including more shells, kill existing windows, view a list of windows, turn output logging on and off, copy & paste text between windows, view the scrollback history, switch between windows in whatever manner you wish, etc.

All windows run their programs completely independent of each other. Programs continue to run when their window is currently invisible and even when the whole screen session is detached from the user’s terminal. When a program terminates, screen kills the window that contained it. If this window was in the foreground, the display switches to the previous window; if none are left, screen exits.

In a nutshell, if Ctrl + F buttons allow you to switch between up to seven virtual consoles, horizontally, screen lets you create an infinite vertical stack of consoles in each one of these.

Home users running full GUI desktops and playing with tabbed terminal utilities would be hard-tempted to find merit in screen, but when you’re running in runlevel 3 and the monitor space is limited, screen is a blessing.

Screen in action

Let’s begin with a few screenshots. To start screen, just type screen in any one console windows, be it gnome-terminal, xterm, Konsole, or any other.

Launch

This will display an introduction messages. Press Enter to exit.

Started

You’re inside a new virtual console. Why not fire another?

First

And here’s the second:

Second

Using the right keyboard shortcuts, we can switch between them, back and forth. Use Ctrl + a then 0 to go to the zeroth (first) screen, Ctrl + a then 1 to go to the second one, and so forth.

First toggled

Now, demonstrating screen with still images is difficult, so here’s a Flash movie! Created using Wink, which served us well so many times, including the tutorial itself, as well as the Windows PowerShell article, and a few others.

So here we go:

Lovely, right! Damn right!

Help window

Don’t hesitate to call for help. Ctrl + a, then ? will pop the help screen.

Help

Of course, you can also read the man page for more details. There’s a plenty you can do with screen, attach/detach/reattach sessions, specify the history scrollback buffer, turn login mode on and off, suppress error messages, and more. screen is a powerful marvel and you should start using it.

Conclusion

Yet another powerful tool mastered. Our list grows bigger, and so does our knowledge. screen may seem trivial to you, but what if you need to debug problems across multiple session and you can’t afford to have tons of Konsole or xterm windows strewn about the desktop like mad. Then, there’s the issue of practical visibility. Never take your eyes off the screen and yet enjoy full multi-view console.

I hope you liked this little surprise. Now, off to new wonders. Stay tuned for many more articles of great admin tools, aptly called super-duper, by me. Be excellent to each other and party on.

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.

cyberpunk@n0where.net ~ $ 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.

cyberpunk@n0where.net ~ $ screen
cyberpunk@n0where.net ~ $ sudo apt-get install dpkg

Sample Output

Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following packages will be upgraded:
  dpkg
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 http://debian.com/latest/ 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]
cyberpunk@n0where.net ~ $

Re-attach the screen

After you detached the screen you can easy return to that session with:

cyberpunk@n0where.net ~ $ 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.

cyberpunk@n0where.net ~ $ screen -ls
SAMPLE OUTPUT
cyberpunk@n0where.net ~ $ 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.

cyberpunk@n0where.net ~ $ 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.

cyberpunk@n0where.net ~ $ 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.
Password:

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“.

cyberpunk@n0where.net ~ $ 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

cyberpunk@n0where.net ~ $ 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.
Password:
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.