Bobcares

Understanding GNU Screen

by | Oct 8, 2009

Screen is a full-screen window manager that switches a physical terminal between several processes (typically interactive shells). In other words, a screen allows a user to access multiple terminal sessions inside a single terminal window. Each virtual terminal provides the functions of a DEC VT100 terminal and in addition, several control functions like insert/delete line and support for several character sets. Other features of a screen include a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between different windows

When screen is called, it creates a single window with a shell in it (or the specified command). It then gets out of your way so that you can use the program as you normally would. Then, at any time, you can do the following:

 

  • 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-and-paste text between windows
  • View the scrollback history
  • View the scrollback history

All windows are independent of each other while running their program. Programs continue to run when their window is currently not visible and even when the entire screen session is separated from the user’s terminal. The window running a program is killed by screen when the program terminates. If this window was in the foreground, the display switches to the previous window and if none are left, the screen exits.

What you type is sent to the program running in the current window. The only exception to this process is the one keystroke which is used to initiate a command to the window manager. By default, each command begins with a control-a (abbreviated C-a from now on), and is followed by another keystroke. The command character and all the key bindings can be fully customized as you wish , though they are always two characters in length.

The standard way to create a new window is to type “C-a c”. This command will create a new window running a shell and will switch to the new window immediately, regardless of the state of the process running in the current window. Similarly, you can create a new window with a custom command in it by first binding the command to a keystroke (in your .screenrc file or at the “C-a :” command line) and then using it just like the “C-a c” command.

Getting Started

If you want to get started without doing a lot more reading, you should remember the command:“C-a ?” .Typing these two characters will display a list of the available screen commands and their bindings

A quick look at screen command line options

-aForce all capabilities into each window’s termcap-A -[r|R]Adapt all windows to the new display width & height-c fileRead configuration file instead of ‘.screenrc’.-d (-r)Detach the elsewhere running screen (and reattach here).-dmS nameStart as daemon: Screen session in detached mode.-D (-r)Detach and logout remote (and reattach here).-D -RRDo whatever is needed to get a screen session.-e xyChange command characters.-fFlow control on, -fn = off, -fa = auto.-h linesSet the size of the scrollback history buffer.-iInterrupt output sooner when flow control is on.-lLogin mode on (update /var/run/utmp), -ln = off.-LTurn on output logging.-mignore $STY variable, do create a new screen session.-OChoose optimal output rather than exact vt100 emulation.-p windowPreselect the named window if it exists.-qQuiet startup. Exits with non-zero return code if unsuccessful.-rReattach to a detached screen process.-s shellShell to execute rather than $SHELL.-T termUse term as $TERM for windows, rather than “screen”.-U Tell screen to use UTF-8 encoding.-wipeDo nothing, just clean up SockDir.-xAttach to a not detached screen. (Multi display mode).-XExecute
as a screen command in the specified session.

Commonly Used shortcuts

The following are some of the commonly used shortcuts that will help you to navigate through your screen. Please note that unless modified by your .screenrc, every screen shortcut is preceded by Ctrl+a by default. These shortcuts are case-sensitive.

0 through 9 : Switches between windows

n: Switches to the next available window

p or backspace : Switches to the previous available

A : Changes window session name

K: Kills a window session

c: Creates a new window

[: Then use arrows to scroll up and down terminal

” : List all windows for selection

?: Show key bindings

Multi user mode to share a session with other users

A great feature of Screen is that it allows other users to login to your console and to watch the work that you are doing. It is a great way for teaching someone to work on the shell.

How to setup session sharing via screen

Primary settings should be done as a root user and need to be done only once in a machine.

Primary settings include SUID (set user id) the screen binary, and correcting permissions for the directory /var/run/screen. Please note that SUID is Unix access rights flags that allow users to run an executable with the permission of the executable’s owner. It is important that you are aware of the security issues, before doing this.

# chmod u+s /usr/bin/screen
# chmod 755 /var/run/screen

Here are the steps to share a session with another user.

Start the screen session to be shared: screen
Press Ctrl+a, then type :multiuser on and press Enter.
Press Ctrl+a, then type :acladd abdurahiman (where “abdurahiman” is the user who will connect to your screen session).

Connecting to the shared screen:

SSH into the machine that you are going to watch the screen session on.
On your terminal type: screen -x mohammed/ (where “mohammed” is the user who is sharing the screen session. Make sure to put / at the end).

And now both mohammed and abdurahiman are sharing the same session. They can run commands from the same terminal. Sharing is useful when you want to teach someone about your server. Sharing also helps you monitor the work being done by other users on your server.

Copying, pasting, and the scrollback buffer

Screen remembers a number of scrollback lines without which, you won’t be able to use your terminal emulator’s scroll features while running screen. You can access the scrollback buffer by entering “copy mode”, which is obtained by typing C-a [. You can mark the text anywhere in the scrollback buffer and paste it with C-a ].

Why xterm scrollbar will not work with screen?

The xterm will not allow scrolling if the other text buffer is selected with screen. The standard definitions of termcap initialize capabilities ti and te switch to and from the alternate text buffer. You can tell screen not to use these initializations by adding the following line to your ~/.screenrc

termcapinfo xterm ti@:te@

Silence and Activity Monitoring

If you run many programs via screen, you may not be able to watch everything simultaneously. To solve this problem, screen has features that allows you to monitor a window for silence or activity.

To start or stop monitoring the current window for 30 seconds of silence, type C-a _.
To start or stop monitoring a window for activity, type C-a M.

Check for existing screen sessions

If you wish to use screen extensively, I’m sure that this option will be handy to you, as you cannot remember all your session names/ids at one go. It is nothing but an “-ls” or “-list” option with screen command. You can use it from within a screen session or from a normal console session.

[@mohammed]$ screen -list
There are screens on:
4308.first (Attached)
15785.second (Attached)
2 Sockets in /tmp/uscreens/S-mohammed.
[@mohammed]$

Split your Screen

Do you wish to monitor activities on multiple sessions together? Split screen is the best option for you then. You can see various programs/activities that you started without switching through the sessions. It will increase your work speed as you can immediately notice which program/activity has stopped first.

Keystrokes related to split screen:

 

Split a current screen session to multiple regions: C-a S

Switch between displayed regions: C-a TAB

Remove current region: C-a X

Remove all regions, but retain current one: C-a Q

 

Check out a screen session that is split into 3 regions.

 

Customize your screen settings

How can I personalize my screen settings?

The ~/.screenrc file is the answer for you. Your customized settings at ~/.screenrc will override the server wide settings at /etc/screenrc.

Example for .screenrc:

hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
hardstatus alwayslastline
termcapinfo xterm ti@:te@

Here is how it will look like, when you enter into a screen session:

 

 

Quick Reference

Help

See help: C-a ? (lists keybindings)

Getting in

start a new screen session: screen
start a new screen session with session name: screen -S

attach to a running session: screen -r
attach to a running session with name: screen -r

Getting out

detach: C-a d

detach and logout (quick exit): C-a D D

exit screen: exit all of the programs in screen.

force-exit screen: C-a C- (not recommended)

Window Management

create new window: C-a c

enter a name for the current window: C-a A (title)

change to last-visited active window: C-a C-a (commonly used to flip-flop between two windows)

change to window by number: C-a (only for windows 0 to 9)

change to window by number or name: C-a ‘

change to next window in list: C-a n or C-a

change to previous window in list: C-a p

see window list: C-a ” (allows you to select a window to change to)

show window bar C-a w (if you don’t have window bar)

close current window: Close all applications in the current window (including shell)

kill current window: C-a k (not recommended)

Split screen

split display: C-a S

jump to next display region: C-a tab

remove current region: C-a X

remove all regions but the current one: C-a Q


If you type “C-a s” by mistake, your session will be suspended. You can un-suspend it by typing “C-a q”.

Miscellaneous

redraw window: C-a C-l

enter copy mode: C-a [ (also used for viewing scrollback buffer)

paste: C-a ]

monitor window for activity: C-a M

monitor window for silence: C-a _

enter digraph: C-a C-v

lock (password protect) display: C-a x

enter screen command: C-a :


About the Author :

Mohammed Abdurahiman joined Bobcares in December 2006, and is an expert in shell scripting. System Administration and troubleshooting is ingrained in him. He also follows popular linux forums and contributes to the open source community. Mohammed Abdurahiman is an asset to Bobcares because of his keen interest on improving systems and innovative suggestions. During his free time, Mohammed loves to blog, read technical articles and watch football.


0 Comments

Never again lose customers to poor
server speed! Let us help you.