Screen

From Sibwiki
Jump to: navigation, search

Introduction

Screen is a terminal session(s) manager. The program can manage several terminal in one instance, allowing user to disconnect/reconnect and even share session(s). It's available by default on Linux and MacOSX platform. The program is easily customizable and configurable and don't require any graphic user interface only a shell access (mostly via ssh).

First Launch

Launching screen is as easy as:

screen

This will create an instance of screen. You can run a simple command like top in it then "detach" from this screen with the binding: 'Ctrl + A' then 'D'

afortier$ screen
[detached]

You can list screen instance with:

afortier$ screen -ls
There are screens on:
	27839.ttys021.yourhost	(Detached)

You can reattach to this screen session with the following commands:

screen -r

You'll then re-enter your screen session with top still working. This works smoothly because we only have one instance of screen :) see on the next chapter how to name and reconnect on named screen session.

Configuration

This was a simple introduction to screen, let's go further.

First thing to know is the special keystroke used in screen to initiate a command: Control + A (this can be changed in the screen configuration file) abbreviated C-a (as it is a lower case 'a' letter), all command will be in the form C-a then Key(s).

Command list (main ones)

  • Create a new terminal in screen: C-a c
  • List of terminal(s) in this screen session : C-a "
  • Navigation through terminals:
    • Previous: C-a p
    • Next: C-a n
    • Number: C-a [0-9] Note: the number referring to the terminal begin at 0. You can have more than 10 terminal in a screen session but you'll need to use C-a " or C-a n|p to navigate
  • Naming a terminal: C-a A
  • Completely close all terminal (exit screen): C-a :quit
  • Scrolling: C-a esc to enter scrolling mode then use PageUp/PageDown or C-u / C-d
  • Adjust the terminal size: C-a F
  • Monitor activity for 15 seconds:C-a _
  • Monitor activity: C-a M
  • Source another or a modified screenrc config file: C-a :source /path/to/your/config/file
  • Enter copy mode (to copy paste lines in the history buffer): C-a Escape then use arrow keys / Page Up | Page Down to navigate
  • Enter scrollback mode (to see the history of your terminal or search for words): C-a [ then use vi-like h j k l or arrows to navigate, /<what you search> to find something (check here for full explanation)

Command line options

  • Use a different configuration file than /etc/screenrc or ~/.screenrc: screen -c /path/to/your/config/file
  • List all screen sessions: screen -ls
  • Name a screen session: screen -S whatever
  • Attach to a named session: screen -r whatever
  • Attached to an unnamed session: screen -r pid.ttys021.yourhost Note: it also works with screen -R pid even typing the first unique number instead of the pid will work too
  • Enter and re-size the terminal(s): screen -A
  • Lock the current screen: C-a x you'll be prompted for a password. (See http://wiki.gcu.info/doku.php?id=unix:screen_password (In French) for another method)
  • Multi-user: screen -x pid.tty.host or screen -x ScreenName (See Screen#Usage for more informations)
  • Direct connection on remote host: ssh -t HostName screen -r (Instead of doing ssh login@HostName and then screen -r ... You know every character counts :) )

Customization

You can create configuration files to change/create bindings, change screen's behaviours, auto-launch commands on specified terminal and customize the 'statusbar'

Here are some useful options:

  • Deactivate visual bell
vbell off
  • Auto create a terminal, set its number and the command launched:
screen -t TITLE # <command>

This will create a terminal named TITLE with number # and auto-launch the <command>

Example:

screen -t rss        2   raggle
screen -t root       1   su -

Here we have 2 terminals: term 1 called "root" with 'su -' command launched, term 2 called rss with raggle launched in it.

Attention: when you declare a command in a dedicated terminal, when you stop this command it will also terminate the terminal!

To avoid this, you can use this syntax:

screen -t SYSTEM        1
stuff "top\015"

This will launch top in the terminal number 1 and if you quit top the term is still active.

  • Create a cool statusbar
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

Who says it's not human readable ;) ... anyway this will create a status bar with the name of your host, terminal number-name, put red brackets around the active terminal and at the far right, the date and time. You can find tons of example/hack in the reference section below.

  • Reloading your configuration

If you update a configuration file while screen(s) are launched, you can use your new config file by doing in each screen: C-A :source /home/user/.screenrc-config-whatever

Sample configuration files

vbell off
screen -t NFSSERV2   9 ssh -l root nfsserv2
screen -t NFSSERV1   8 ssh -l root nfsserv1
screen -t RSERV      7 ssh -l root rserv
screen -t DEVFRT     6 ssh -l root devfrt01
screen -t DAFPAF2    5 ssh -l root dafpaf2
screen -t DAFPAF     4 ssh -l root dafpaf
screen -t IDNS       3 ssh -l root idns
screen -t EDNS       2 ssh -l root edns
screen -t FRT        1 ssh -l root frt
screen -t LOCAL      0
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

Notes: The definition of the term is inverted (from 9 to 0) because the last line read is the one active. Don't copy/paste without modifying a bit as you probably don't have root access over those servers ;). The last term (LOCAL) has no command and will just output a shell prompt.

Feel free to add yours there :)

Usage

One thing I like to do is to name a screen session, to be able to reconnect to it easily or create it when it's not already alive.

screen -Rd MONITOR

This will connect to the screen session named MONITOR or create it and connect into it.

screen -ARd MONITOR 

With the 'A' parameters this will resize the output of the session if it exist to fit in your new terminal

screen -Ax MONITOR

You can connect to your screen session from ssh and then have the exact replication simultaneously (useful to debug or for help desk purpose) You should add "multiuser on" and "acladd root" to the user's screenrc to allow root to attach to the session. Then root can attach with

screen -x <username>/MONITOR 

the slash (/) is mandatory here

More informations on managing multi-user's features: http://aperiodic.net/screen/multiuser

Presentation

References

There's also possibilities to copy paste stuff from one term to another in command line even across several screen instances. To know everything about screen:

--Afortier 15:00, 21 October 2009 (CEST) Feel free to edit/comment