Screen

From Gentoo Wiki
Jump to: navigation, search
External resources

GNU Screen is a program that enables the user to create sessions and have several virtual consoles.

Installation

Install app-misc/screen:

→ Information about USE flags
USE flag Default Recommended Description
debug No No Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see http://www.gentoo.org/proj/en/qa/backtraces.xml
nethack No Express error messages in nethack style
pam No Adds support for PAM (Pluggable Authentication Modules) - DANGEROUS to arbitrarily flip
selinux No  !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
multiuser No Enable multiuser support (by setting correct permissions)
root # emerge --ask screen

Configuration

The configuration files are located in /etc/screenrc and ~/.screenrc.

Usage

General usage of Screen.

Start Session

Screen can be started with the following command:

user $ screen

Once started is will create a socket for the session in /var/run/screen/S-<username> You can use any key combination within the session except of Ctrl+a which initiates the Command-Mode.
Give the session a reasonable name on startup:

user $ screen -t portage

Start a command in a screen session without attaching to it (like a daemon):

user $ screen -t portage -d -m emerge -uDN @world

Commands

In Command Mode (after pressing Ctrl+a) the following key combinations can be used:

General

  • ? = show quick help
  • d = detach from the current session, all windows will continue running in the background

Creating/Managing windows

  • c = create another window
  • n or Space = switch to the next window
  • p or Backspace = switch to the previous window
  • 0 = switch to window 0 (similar for 1-9)
  • ', then a number or title = switch to window with given number or title, used to reach windows numbered above 9
  • Ctrl+a = switch to last used window
  • Shift+a = set title for window
  • " = display a list of windows, which can be navigated with arrow keys and selected with Enter or Space
  • w = display a non-interactive list of windows at the bottom of screen (see Hardstatus Customization)

Copy, Paste and Scroll operations

  • Esc = begin "copy mode", which allows scrolling and copying of window contents with following keys
    • Ctrl+B = scroll up in copy mode
    • Ctrl+F = scroll down in copy mode
    • arrow keys = move the curser in copy mode
    • Space or Enter = begin/end copying
  • ] = paste the text copied in copy mode

Logging, Monitoring

  • Shift+h = start/stop logging everything displayed on current window. log will be saved as a file "screenlog.X" under the directory in which screen was run
  • h = (disabled in default configuration) dump the current contents of the current window to a file named "hardcopy.X" under the directory in which screen was run
  • Shift+m = start/stop monitoring current window for activity, any output produced by a program will cause an alert
  • _ = start/stop monitoring for inactivity (i.e. a long and output producing command finishes runnning)

Resume Session

After the session is detached, all the active terminals remain active and so do commands that didn't finish yet. To list existing sessions:

user $ screen -ls
There is a screen on:
        30014.pts-1.disi-bigtop (Detached)
1 Socket in /var/run/screen/S-disi.

To resume this session:

user $ screen -r 30014.pts-1.disi-bigtop

Multiuser

initiate screen with a human readable session name.

user $ screen -S multi

all other clients to view (and act on) the active live terminal can join via

user $ screen -x multi

then its like conference call style terminal.

Session within Session Recursion

You can use a Screen within a Screen session. This occurs when you connect to a another host's Screen session from within your Screen session.

To signal the secondary (remote) Screen session with the Meta key, you issue the key twice to signal the second (remote) Session.

Ctrl+a Your normal meta key usage on your local screen
Ctrl+aa Signals the second (remote) Session
Ctrl+aaa Signals the third (remote) Session
Ctrl+aaaa Signals the fourth (remote) Session, and so on

Some tend to remap the Meta key on the other terminals so they don't have to count. However, if you use the console/terminal often, there tends to be a shortage of keys on the keyboard.

Hardstatus Customization

Customizing the status line, or hardstatus line in GNU Screen terminology is quite common. The following will get you started.

File$HOME/.screenrc

hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{+b m}(%{W}%n*%f%t%?(%u)%?%{m})%{-b w}%?%+Lw%?%?%= %{g}][%{c} %D %M %d %{g}%c %{g}]'

Screenshot of what my hardstatus line looks like using the above hardstatus incantation, along with nine additional terminal titles (ie. screen -t bash 7 bash --noprofile") defined within the .screenrc file. Each terminal gives me Calendar (calcurse), HTop (htop), startx (manual startx/DWM WM), Root (su), WWW (elinks), EMail (mutt) and a multitude of additional (bash) shells for working in. (This screenshot unfortunately omits color.)

user $
[ localhost2 ][ 0$ Calendar  1$ HTop  2$ startx  3$ Root  4$ bash  5-$ bash  (6*$bash) ][ Tue Nov 15  3:02 ]

Solutions to Common Problems

After quitting an editor like Vim, contents of the edited file remain in the screen window

Add the following line to screen preferences file

File$HOME/.screenrc

altscreen on

Programs with too much output make older window contents scrolled away

You can increase the scrollback buffer size with following line in your preferences file

File$HOME/.screenrc

defscrollback 10000

TTY Autologin with a GNU Screen Session

The following will allow your system to auto login as your user, and automatically initiate a GNU Screen session on tty2. A good scenario for this; You're off the network, secluded from society, and you find entering a password is a bit redundant. The following demonstrates using the BASH Shell.

Warning
Take heed of the obvious possible security implications this has!

MinGetty will give you autologin capability along without having to use a password.

root # emerge --ask net-dialup/mingetty

Comment-out the second TTY (ie. c2, or terminal number of your choosing) and add mingetty with autologin capability.

File/etc/inittab

-- c2:2345:respawn:/sbin/agetty 38400 tty2 linux
++ #c2:2345:respawn:/sbin/agetty 38400 tty2 linux
++ scr2:4:respawn:/sbin/mingetty --autologin USER_NAME tty2 linux

Replace the above "USER_NAME" with your login name!

Add a line within your boot loader's configuration file to specify loading the init #4 level. (Referencing the "scr2:4" level specified within the above inittab file.) Also, utilize the "nox" option as well. (As I can recall, the line ending with "4" specifies runlevel four in correspondence to the above inittab entry. And the nox avoids XDM starting(?) If something needs further documenting here for clarification, please do so!)

The following configuration states, use framebuffer at the specified screen size using nox and start init level 4.

File/boot/grub/menu.lst

title Gentoo - No X using Framebuffer - GNU Screen Console
# Default Entry
#:0
    root (hd0,1)
    kernel (hd0,1)/boot/vmlinuz quiet root=/dev/sda2 video=uvesafb:nocrtc,ywrap,mtrr:3,1280x1024-16@60 nox 4

At this point, your system can now auto login on TTY2 with your default shell. Assuming you're using the BASH shell, you can start GNU Screen automatically by adding the "screen" command to your $HOME/.bash_profile file. This file is executed automatically when you first login. (Make sure you start any subsequent BASH shells using the "bash --no-profile" switch, including shells started within the $HOME/.screenrc files!)

File$HOME/.bash_profile

# You don't need the "if/fi" statements. I only make mention to give you the idea of using a shared $HOME/.bash_profile.
# if [ $HOSTNAME = "localhost1.local" ]; then

    # Insert any $VARIABLES you want exported to the environment before GNU Screen is executed.

    # Insert any commands you want started on initial login before screen is executed.

    # vico/case checks if we're running from only a virtual tty
    vico="$(tty | grep -oE ....$)"
    case "$vico" in
        tty2) screen -wipe; ionice -c 2 -n 0 screen -RD ;;
    esac
    # Nothing further is executed after "screen -RD" unless you use "&"

# elif [ $HOSTNAME = "localhost2.local" ]; then
# fi

With a little further scripting within $HOME/.bash_profile and a second entry within /etc/inittab, you could specify runlevel 5 for auto logging in and starting your favorite graphical desktop. (ie. XFCE4, GNOME, KDE, ...) And, while still spawning a GNU Screen session onto the desktop. But this is getting a little off topic of this GNU Screen Wiki.

Enable 256 Colors

In order for GNU Screen to allow applications or the terminal to use 256 colors, two requisites must be met:

1) The "term screen-256color" line must be specified prior to any 'screen' definitions. (ie. "screen -t Mail") The resulting side effect of specifying after 'screen' definitions, the $TERM variable will be defined as 'screen' instead of the required 'screen-256color', and applications will not be able to see the terminal supports 256 colors.
2) Requires both the application and terminal to be compiled with 256 color support.

Note
Past the below code prior to any 'screen' definitions!
Code

#
# 256 COLOR SUPPORT
#
# Set terminfo and termcap for nice 256 color terminal
# Allow bold colors - necessary for some reason
#attrcolor b ".I" 

# Tell screen how to set colors. AB = background, AF=foreground 
#termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm' 
terminfo rxvt-unicode 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'

# Erase background with current bg color 
#defbce "on" 

# By default, screen uses an 8-color terminal emulator. Use the
# following line to enable more colors
term screen-256color

See also

  • tmux - An alternative to GNU screen
  • screenie - A GNU screen wrapper
  • byobu - Profiles and configuration utilities for GNU screen