Difference between revisions of "Screen Tips"

From YobiWiki
Jump to navigation Jump to search
 
 
(11 intermediate revisions by 2 users not shown)
Line 19: Line 19:
 
<br>To attach without first detaching an active session:
 
<br>To attach without first detaching an active session:
 
screen -x
 
screen -x
<br>Very powerful, this last one!!
 
<br>For example, helping a friend via remote administration:
 
* ask him to install screen (of course), create a user for you, so you can log in and change your passwd
 
* ask him, being root in a xterm, to launch ''su your_username -c "xterm -e screen" &''
 
* you launch ''screen -x'' from your ssh session and now you are both on the same console!
 
* you type ''su'' and he types the root passwd, so now you can be momentaneously root on his machine, he did not have to discard root password and he can watch (and learn) what you are doing.
 
   
 
Sometimes when using '''mc''' through screen the graphical characters mess up the layout.
==Multisession==
 
 
<br>Try "mc -a", it should fix it.
To allow other users to join your session:
 
<br>Gives +s to screen bin:
 
chmod +s /usr/bin/screen
 
From screen session:
 
CTRL-a :
 
multiuser on
 
CTRL-a :
 
addacl user2
 
   
It adds user2 to the list of authorized users with full access (partial acces is possible, cf man)
 
<br>Then user2 can connect to screen session of user1 with
 
screen -x user1/
 
To see the list of sessions of another user:
 
screen -ls user1/
 
==Tips==
 
Sometimes when using __mc__ through screen the graphical characters mess up the layout.
 
<br>Try "mc -a", it should fix it.
 
 
==Guy notes==
 
==Guy notes==
   
Line 49: Line 28:
 
screen -r: to reattach to a session
 
screen -r: to reattach to a session
 
screen -x: to attach to a not detached screen session (multi display mode)
 
screen -x: to attach to a not detached screen session (multi display mode)
screen -d -r: to remotely force a session to be detached and attach it here
+
screen -d -r: to remotely force a session to be detached and attach it here (to cleanly reattach from another session)
  +
(to cleanly reattach from another session)
 
 
screen -D -R: Attach here and now.
 
screen -D -R: Attach here and now.
 
In detail this means: If a session is running, then reattach.
 
In detail this means: If a session is running, then reattach.
Line 61: Line 40:
 
C-a x: to lock your current session
 
C-a x: to lock your current session
 
C-a a: to obtain "Ctrl-a" (for emacs for example)
 
C-a a: to obtain "Ctrl-a" (for emacs for example)
C-a A: to set the title of the newly created window while in screen
+
C-a A: to set the title of the newly created window while in screen (equivalent to screen -t name_of_window)
  +
(equivalent to screen -t name_of_window)
 
 
C-a c: to create new window
 
C-a c: to create new window
C-a k: to remove the current window from the list and forcibly
+
C-a k: to remove the current window from the list and forcibly terminate whatever programs were running in it
  +
terminate whatever programs were running in it
 
 
C-a C-a: to switch between screen's windows
 
C-a C-a: to switch between screen's windows
 
C-a n: to switch to the next window
 
C-a n: to switch to the next window
Line 103: Line 82:
 
Also:
 
Also:
 
C-a :multiuser on : to activate multiuser mode
 
C-a :multiuser on : to activate multiuser mode
C-a :achchg username +rwx "#" : to give permission to username to connect
+
C-a :aclchg username +rwx "#" : to give permission to username to connect
 
Be especially careful with the execute permission as that will let other people create new windows and change the acl permissions themselves.
 
Be especially careful with the execute permission as that will let other people create new windows and change the acl permissions themselves.
   
Line 188: Line 167:
 
bind 'n' next # Next window
 
bind 'n' next # Next window
 
bind 's' screen -t bash@$HOST -ln -h 100 # Create new shell window
 
bind 's' screen -t bash@$HOST -ln -h 100 # Create new shell window
  +
  +
==Try also retty==
  +
You can try also package retty:
  +
  +
retty is a tiny tool that lets you attach processes running on other terminals. So you were running that mutt outside of screen at your home machine and now wanna check your mail? Attach it with retty, do whatever you want, detach it again and everything is as it was before. You don't have to run them all in screen just in case.
  +
<br>Note that the tool is only very lightly tested, so take some care. Always check first if attaching given application works before you will do it for real.
  +
  +
Example:
  +
* Run top in e.g. a xterm
  +
* Find its PID
  +
* in another term, type "retty <PID>"
  +
* to quit properly, go to another term and kill nicely retty, top will be attached back to its original tty
  +
  +
Another example: you want to run a process detached
  +
* "top &" ? Nope this fails and top stops immediatelycile.be/frame.html
  +
* "nohup top"? Nope this fails with "top: failed tty get"
  +
* Try "setsid blindtty top"
  +
* Now you can attach it temporarely with retty, detach it and it is still running :-)
  +
* Note that "blindtty top &" does nearly the same but keeps the process group while setsid detaches the process and attaches it directly as a child of process init.
  +
==Try also reptyr==
  +
[https://github.com/nelhage/reptyr reptyr] acts the same as retty but is also available on 64-bit platforms while retty is only for i386.
  +
==Ubuntu==
  +
Note that to use retty or reptyr on recent Ubuntu you've to allow any process to debug another process, which is not allowed by default (only children of a process can be debugged by it):
  +
echo 0 > /proc/sys/kernel/yama/ptrace_scope

Latest revision as of 12:28, 23 March 2014

Introduction

Some usages of "screen", a very powerful tool!

To create a screen session

screen

The shortcut to access screen commands is CTRL-a
There you can create other windows with CTRL-a c
Go to next window with CTRL-a n

If the link is broken (kill xterm, ssh fails,...) no prob the session is still runnind detached from any terminal.
You can also detach with CTRL-a d
To resume a detached session:

screen -r


To see the list of sessions:

screen -ls


To remotely force a session to be detached and attach it here:

screen -r -d


To attach without first detaching an active session:

screen -x

Sometimes when using mc through screen the graphical characters mess up the layout.
Try "mc -a", it should fix it.

Guy notes

screen:  to start screen
screen -r: to reattach to a session                                             
screen -x: to attach to a not detached screen session (multi display mode)
screen -d -r: to remotely force a session to be detached and attach it here (to cleanly reattach from another session)
screen -D -R:   Attach here and now.

In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user. This is the author's favorite.

C-a d:   to detach from the session
C-a ?:   to obtain a list of commands
C-a x:   to lock your current session
C-a a:   to obtain "Ctrl-a" (for emacs for example)
C-a A:   to set the title of the newly created window while in screen (equivalent to screen -t name_of_window)
C-a c:   to create new window
C-a k:   to remove the current window from the list and forcibly terminate whatever programs were running in it

C-a C-a: to switch between screen's windows
C-a n:   to switch to the next window
C-a p:   to switch to the previous window
C-a N:   to switch to the corresponding window (N in [0,9])
C-a ":   to get a full-screen list of windows
C-a w:   to get a small, non-interactive list of windows
C-a S:   to split the region in two
C-a tab: to switch from on region to the other one
C-a N:   to display any of your window in the current region (N in [0,9])
C-a c:   to create a new shell in the current region
C-a Q:   to make the current region the only one visible
C-a X:   to kill the current region (and the programs running in it)
C-a [:   to enter "copy mode"
C-a ]:   to paste the text previously copied
C-a _;:  to start or stop monitoring the current window for 30 seconds of silence
C-a M:   to start or stop monitoring the current window for activity
C-a C-\: to force closing of screen (not recommended)

Remark 1: collaborative session

One other good use for the detach and reattach is as a console-mode "remote desktop" feature.
You can detach from a screen session at work, shell into the machine from home, and reattach.
With a bit of extra work, you can even have a number of terminals all attached to the same session (great for collaborative efforts and meetings).

For example, helping a friend via remote administration:

  • ask him to install screen (of course), create a user for you, so you can log in and change your passwd
  • ask him, being root in a xterm, to launch
su your_username -c "xterm -e screen" &
  • you launch screen -x from your ssh session and now you are both on the same console!
  • you type su and he types the root passwd, so now you can be momentaneously root on his machine, he did not have to discard root password and he can watch (and learn) what you are doing.

Also:

C-a :multiuser on           : to activate multiuser mode
C-a :aclchg username +rwx "#" : to give permission to username to connect

Be especially careful with the execute permission as that will let other people create new windows and change the acl permissions themselves.

Now the user you granted access to can connect with:

$ screen -r stan/

At any time, you can delete other users with acldel username

Remark 2: allowing other users to join your session

chmod +s /usr/bin/screen: to allow other users to join your session

From screen session:

CTRL-a :
multiuser on
CTRL-a :
addacl user2

It adds user2 to the list of authorized users with full access (partial acces is possible, cf man)
Then user2 can connect to screen session of user1 with

screen -x user1/

To see the list of sessions of another user:

screen -ls user1/

Remark 3: bunch of screens at startup

If you want a bunch of screens at startup, edit your ~/.screenrc.

shell -$SHELL
screen -t SHELL0  0
screen -t ROOT    1
screen -t SHELL2  2
screen -t SHELL3  3
screen -t SHELL4  4
screen -t SHELL5  5

The first line starts the current $SHELL in login mode.
The others set up six windows and gives them titles.

Remark 4: logging

Screen is also capable of logging to files, but that's beyond the scope of this tutorial.

Remark 5: Customizing screen -> $HOME/.screenrc

Here's a short example $HOME/.screenrc file containing comments for each directive:

# set some options
activity "activity: window ~%"  # Message when activity occurs in a window
vbell_msg "bell: window ~%"     # Message for visual bell
vbellwait 2                     # Seconds to pause the screen for visual bell
allpartial off                  # Refresh entire screen on window change
autodetach on                   # Autodetach session on hangup instead of
                                # terminating screen completely
bufferfile /tmp/screen-buffer   # Filename for the paste buffer
chdir                           # Change to the home directory
escape "``"                     # Redefine the prefix key to ` and define a
                                # literal ` as ``
shelltitle $HOST                # Set the title of all shell windows
defflow off                     # Set the default flow control mode
defmode 0620                    # Set the default mode of each pseudo tty
defscrollback 200               # Set the default number of scrollback lines
deflogin off                    # Do not register the window in utmp
startup_message off             # Disable startup messages


# virtual windows to start when screen starts
screen -t emacs@$HOST -h 0 1 /usr/local/bin/emacs -nw
                                # Start emacs in window 1 with a scrollback
                                # buffer of 0
screen -t bash@$HOST -ln -h 100 2
                                # Start a shell with the title of
                                # tcsh@.  turn off login mode
                                # (remove the window from utmp).  Use a
                                # scrollback of 100 lines and start the shell
                                # in window 2 (or the next available window)
monitor on                      # Monitor the above shell window
# keymap for use with the prefix key (backquote)
bind ' ' windows                # Show listing of all windows
bind 'a' prev                   # Previous window
bind 'c' copy                   # Copy paste buffer
bind 'e' screen -t emacs@$HOST -h 0 1 /usr/local/bin/emacs -nw
                                # Create new emacs window
bind 'i' info                   # Show info about the current window
bind 'n' next                   # Next window
bind 's' screen -t bash@$HOST -ln -h 100  # Create new shell window

Try also retty

You can try also package retty:

retty is a tiny tool that lets you attach processes running on other terminals. So you were running that mutt outside of screen at your home machine and now wanna check your mail? Attach it with retty, do whatever you want, detach it again and everything is as it was before. You don't have to run them all in screen just in case.
Note that the tool is only very lightly tested, so take some care. Always check first if attaching given application works before you will do it for real.

Example:

  • Run top in e.g. a xterm
  • Find its PID
  • in another term, type "retty <PID>"
  • to quit properly, go to another term and kill nicely retty, top will be attached back to its original tty

Another example: you want to run a process detached

  • "top &" ? Nope this fails and top stops immediatelycile.be/frame.html
  • "nohup top"? Nope this fails with "top: failed tty get"
  • Try "setsid blindtty top"
  • Now you can attach it temporarely with retty, detach it and it is still running :-)
  • Note that "blindtty top &" does nearly the same but keeps the process group while setsid detaches the process and attaches it directly as a child of process init.

Try also reptyr

reptyr acts the same as retty but is also available on 64-bit platforms while retty is only for i386.

Ubuntu

Note that to use retty or reptyr on recent Ubuntu you've to allow any process to debug another process, which is not allowed by default (only children of a process can be debugged by it):

echo 0 > /proc/sys/kernel/yama/ptrace_scope