Friday, 15 May 2015

Using Screen


If you've been frustrated with managing multiple network device GUI terminals (i.e. Lots of PuTTY sessions) or just prefer to use the keyboard for most of the work, you may be interested in a utility called 'screen'.

Screen provides the ability to run multiple virtual terminals inside of a single terminal and manage these virtual terminals purely through the keyboard. Terminals can be configured to dump a running history, copy and paste and can be detached to resume the virtual terminal session again later.

You can either install screen in a Linux host through the native package manager or on Windows through Cygwin. I'm using screen in a mintty terminal on Windows.

Start by creating the .screenrc file in your home directory. This file contains the user specific configuration for screen. You can use 'vi' to do this. My configuration contains the following:

startup_message off
shell -/bin/bash
hardstatus alwayslastline
hardstatus string "%w"



The first line removes the startup/welcome message. The shell command specifies the shell to use for new virtual terminals. The hardstatus command refers to a status line that takes up the last row of text on the screen. alwayslastline ensures that it always stays and the "%w" signifies that the number and name of all the terminals should be displayed in the hard status line.

Once you start screen with this configuration, you should see something like the following.



There is a single terminal (0), entitled 'bash', that you can use as a normal terminal or try some of the commands below.


Ctrl-A Ctrl-A
Switch to the previous window
Ctrl-A d
Detach from screen (terminals are left running and can be reattached to later)
Ctrl-A c
Create a new terminal
Ctrl-A #
Change to terminal number signified by '#'
Ctrl-A H
Start logging terminal output to screenlog.# ('#' indicates terminal number)
Ctrl-A A
Change the window title
Ctrl-A S
Split the window in to multiple terminals. Each is visible at the same time.
Ctrl-A tab
Switch between split windows
Ctrl-A k
Kill the current window
Ctrl-A X
Remove the current split window


Here is an example of a split screen instance. An SSH session is being run in the top section and a VI session for a file is being run in the bottom section.



If you detach a screen instance using "Ctrl-A d", you can reattach using "screen -r" or get a listing of the screen instances to reattach to using "screen -ls"

$ screen -ls
There are screens on:
        1744.pty0.scyth (Attached)
        5008.pty4.scyth (Detached)
2 Sockets in /tmp/uscreens/S-jreichman.



Links
http://en.wikipedia.org/wiki/GNU_Screen
http://www.gnu.org/software/screen/manual/screen.html

No comments:

Post a Comment