-- WilliamBreadenMadden - 2012-03-20

SSH

* description of SSH *

Screen

Happy birthday, Screen!

General description

There can be certain limitations when using SSH to connect to a server. For examples, in order to open a new window, a second SSH connection to the server must be created; if the connection is lost* during the SSH tunnel, progress is lost. Screen is useful in tackling both of these problems.

Screen is a useful tool that allows the use of multiple screens over one SSH connection. You might have separate processes running in each of these screens.

Screen is installed by default on most installations. The presence of screen can be verified using the "which screen" command. Screen can be installed easily using apt or yum.

Using screen

Screen can be started by entering "screen".

You should now be in a new screen, ready to enter commands. Screen has a few commands in order to manipulate windows, navigate through open ones, "detach" and "attach", among other commands. The entire command list can be shown by pressing Ctrl + a, followed by ?. (By pressing Ctrl + a, it is indicated that the next signal is to be sent to screen, as opposed to the shell.)

To start a new window press Ctrl + a, followed by c (for "create"). A new window should now be created. To cycle through the various open windows, press Ctrl + a, followed by n (for "next"). To cycle through the various open windows in the opposite direction, press Ctrl + p, followed by p ("for previous"). To view a list of all open windows (from which a particular window can be selected), press Ctrl + a, followed by " (i.e., you might press Ctrl + a, followed by Shift + '.)

To close a window, the "exit" command can be used (as in a normal shell). This will cause one to fall back to the previous monitor one opened, or to one's main prompt (from which screen was started).

One of the biggest advantages of using screen is that one can "detach" a screen session. This means one returns to the normal shell, but the processes started in screen remain active in the background. To detach, press Ctrl + a, followed by d (for "detach"). Basically, one can start a number of processes, exit the shell and return some time later to pick up the session started in screen. Further, should one be disconnected accidentally during a screen session, one can pick up the (detached) screen session. To "reattach", SSH to the shell of the server that screen was being run on. To present a list of the current screen sessions running, use the "screen -ls" command. To resume one of these listed previous screen sessions, use the "screen -r " command. One can actually type simply the first few characters of the session name (enough to identify that session) or, indeed, if there is only one session running, one need use only the "screen -r" ("r" for "resume") command. If a screen session is still attached, it can not be resumed before it is detached.

Example using CERN LXPLUS

SSH in to LXPLUS.

wbm@dungeonoflove:~$ ssh -Y wbm@lxplus429.cern.ch

Start a screen session.

[wbm@lxplus429]~% screen

Do something in this window.

[lxplus429] ~ % top

Create a new window by pressing Ctrl + a, followed by c.

Do something in this new window. (In this example, I have the terminal print the time and date every second until the process is stopped using Ctrl + z.)

[lxplus429] ~ % while date; do sleep 1; done

Try switching between your two windows by pressing Ctrl + a, followed by n, or by pressing Ctrl + a, followed by p.

Try viewing a list of your open windows by pressing Ctrl + a , followed by ".

Now, detach from your screen session by pressing Ctrl + a, followed by d.

You should arrive back at the main prompt (from which screen was started),

[wbm@lxplus429]~%

Now, disconnect from LXPLUS (or, for added fun, simulate an accidental disconnection by turning off your internet connection...).

[wbm@lxplus429]~% exit

You should now be back at your local computer.

Connection to lxplus.cern.ch closed.
wbm@dungeonoflove:~$

Now, SSH back to that same server you were using.

wbm@dungeonoflove:~$ ssh -Y wbm@lxplus429.cern.ch

Here comes the exciting part... Using the "screen -ls" command, you can see a list of the current screen sessions running...

[wbm@lxplus429]~% screen -ls
There is a screen on:
        17779.pts-45.lxplus429  (Detached)
1 Socket in /var/run/screen/S-wbm

Next, you can resume this screen session using the "screen -r name_of_session" command. Remember that you need use only the the first few characters (or you can simply use "screen -r" to resume is you have only one session running) of the session name to identify it...

[wbm@lxplus429]~% screen -r 17779.pts-45.lxplus429

You should now be back in your screen session!

You can exit your screen windows (and, eventually, your screen session when all screen windows are closed) using the "exit" command.

[lxplus429] ~ % exit

You can exit the SSH tunnel using the "exit" command.

[wbm@lxplus429]~% exit

Now, you are back at your local computer.

wbm@dungeonoflove:~$

That was fun, wasn't it?

Screen tips and tricks

Copy Mode

Enter the copy buffer mode by pressing Ctrl + a , followed by Esc.

In copy buffer mode, you can move the cursor freely using directional keys.

SSH tips and tricks

Unlock a frozen SSH session

To unlock a frozen SSH session (e.g. after connection timeout) without needing to open a new shell, press Enter, then tilde, then period.

Make "delete" work properly in text editing

When I ssh to lxplus machines at CERN and I try to edit a file, either with emacs, nano or pico, when I press the backspace key it does not remove the character to the left of the cursor as it should. The following solutions solves the problem in the xterm ran after the ssh command, but not in the initial window. You need to create this file in your home area

[abuzatu@lxplus401]~% emacs -nw ~/.Xdefaults

then add this line in the file

*VT100.Translations: #override \
<Key>BackSpace: string(0x7F)\n\
<Key>Delete: string("\033[3~")\n\
<Key>Home: string("\033[1~")\n\
<Key>End: string("\033[4~")
*ttyModes: erase ^?

At lxplus machines edit .zshrc

When I ssh to lxplus machines at CERN the default shell is /bin/zsh, so if you want to add aliases of your own, you have to edit the file ~/.zshrc.

-- Main.WilliamBreadenMadden - 2011-08-25

-- Main.AdrianBuzatu - 2012-02-01

Topic revision: r8 - 2012-03-20 - WilliamBreadenMadden
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback