Monday, November 16, 2009

GNU Screen Tutorial

GNU Screen Tutorial

GNU Screen is a free terminal multiplexer that allows console users to run any number of text-based applications, interactive command shells, curses-based applications, text editors etc, within a single terminal. Similar to VNC, screen enables you to start applications from one computer, reconnect from another computer and continue using the same application without having to restart it. If you are one of those Linux users that keep several console sessions opened at all times, you should really give screen a try. Screen also has the ability to keep the application running even after you have accidentally closed the terminal, logged out or even had a connection drop and allows you to later reattach to it, resuming your work right from where you left it.

Another powerful screen feature is its session sharing ability. Screen enables several users to connect to the same session at once, allowing collaboration between them by sharing the same console. Most likely, this feature is mostly being used for teaching and explaining purposes this way: the 'teacher' starts the screen and several 'students' will attach to that session, watching the 'teacher's commands and actions. The problem with this feature it that screen's documentation doesn't offer an obvious how-to use it. But I�ll try to explain it later on. First, with the basic commands.

Creating and switching between sessions

First of all, start a fresh screen session:

CODE
screen

You will be probably greeted by a welcome screen showing some information explaining screen's license, where to report bugs and so on. Press the space bar to bypass this screen. Optionally, you can disable it permanently by adding the following line to your $HOME/.screenrc file:

CODE
startup_message off

Every new created screen window is identified by an unique number. The first window (the one you're actually using) is numbered as 0, while future windows will be labeled as 1, 2, 3 etc. In this window, type a simple command (w or uptime) so you can recognize it later. Keep in mind that screen's default command character is Ctrl+a (press the Ctrl key, hold it and press a, then release them both). Moreover, the command (letter) entered after Ctrl+a is case sensitive, so for example Ctrl+a n is a different command from Ctrl+a N.

To create a new window, type Ctrl+a (to get screen's attention) and then press c (create).

Now, try to switch between them using either of the following methods:
1. Press Ctrl+a, then n to switch to next window and Ctrl+a, then n to switch to the previous one.
2. Press Ctrl+a, then a number from 0 to 9, to switch to the corresponding window.
3. Press Ctrl+a, then " (Shift+') to get a full-screen list of windows. You can navigate through this list with the up and down arrow keys and select the window by pressing Enter while highlighted. Also, Ctrl+a and w will give you a non-interactive list of windows.

To rename a window for easier recognition, press Ctrl+a, then A (that's an uppercase a so either press shift+a or enable capslock and press a) while attached to a session. You will be prompted for a new window name.

To kill not responding programs, use Ctrl+a K.

Detaching and reattaching

After playing a little with basic screen commands, you should now at least know how to create a screen session, so create one if the ones earlier were ended.

To detach from this session, press Ctrl+a d. Or, in a more improper way, just close the terminal that contains the session, but keep in mind that neither of these methods actually end your session, all they do is unbind your session from the current terminal. So basically, if you had a program running while pressing Ctrl+a d, that program is still running and you can resume working with it at any time.

To re-attach to a detached session, open a terminal and type

CODE
screen -r

If only one screen session is detached, you will immediately be attached to it but in case several other screen sessions are detached, you will be presented with a list that looks like this:

CODE
# screen -r
There are several suitable screens on:
4870.pts-4.localhost (Detached)
4638.pts-1.localhost (Detached)

Pick one and type this command to reattach to it:

CODE
screen -r 4870.pts-4.localhost

Sharing a screen session

To share a terminal with one or more users, you have to:

1. Set the screen binary setuid root:

CODE
chmod +s /usr/bin/screen

2. Login as root and start a screen session and give it a name:

CODE
screen -S sessionName

3. Enable screen's multi-user access function by pressing Ctrl+a then :multiuser on (to type ":", press "shift+;")
4. Grant permissions to the second user by pressing Ctrl+a: acladd username, where username is the Unix user for the second user.
5. Ask the second user to connect to your computer through SSH and type

CODE
screen -x root/sessionName

Scroll back, Copying and Pasting

It may not be immediately obvious to the new screen user, but this is one of screen's key features. You won't be able to use your terminal's scroll features while using screen so you'll have to use screen's copy mode.

To scroll back through screen's history, first enable the copy mode by pressing Ctrl+a [ . Now, use either one of the arrow keys, page up, page down, home and end keys to scroll through screen's history.

Pressing Enter will start marking text for copying. Pressing Enter again will copy the selected text into the clipboard. Now, to paste that text, press Ctrl+a ].

Moreover, screen has the ability to log its output to a file on the disk. To enable screen logging, press Ctrl+a H to enable it and again to disable it. By default, the log file will be saved as $HOME/screenlog.1 .

Splitting the screen in half horizontally

Screen has the ability to split the window into two or more half horizontal windows. This feature will enable you, for example, to work in a half screen, while viewing the system resources ("top") in the remaining half.

To split the screen in two, first create two screen sessions. Then press Ctrl+a S which will split the screen in half, giving you a session on the top that you're already using and a blank session at the bottom.

To move to the bottom half, press Ctrl+a, then the TAB key. This key combination will move the console cursor to the lower half. Once in the blank half, you have to set this bottom half of the screen to use one of the two earlier created sessions. To do this, press Ctrl+a, then " (shift+'). You will be presented with a list of available screen sessions, pick one using the arrow keys (be careful not to pick the one already at the top or you'll have a mirror console), and activate it by pressing Enter. Now type top and switch to the upper half by pressing again Ctrl+a TAB. You should now have a bash prompt waiting for commands in the upper half and system resources monitoring in the bottom half.

To return to full size screen, switch to the half you want it to remain and press Ctrl+a Q.

Exiting the screen

This is the easiest part. Close any running programs in your screen session and type exit. Do this for each detached screen session.
Read by 43,387 user(s) | Add comment | Link to this article TWEET THIS
Share, bookmark, add
Digg this!Digg this! Buzz up!
Bookmark this post to del.icio.usAdd to del.icio.us Stumble!Stumble!
Submit to Reddit!Submit to Reddit Submit to Reddit!Add to Technorati
Furl this!Furl Slashdot it!Slashdot it
PDF VersionDownload the PDF version of this article


Article rating:

*
*
*
*
*
*

Good (3.9/5) 22 vote(s)

Subscribe to news | Print article | Send to friend

© Copyright 2001-2009 Softpedia
Contact:




SEARCH THE NEWS ARCHIVE :



Today's News | Yesterday's News | News Archive



MORE RELATED ARTICLES:

Setting Up A Central Syslog Server

Creating a Local Update Repository for FC6

Wolfenstein: Enemy Territory Server Setup

How to Bind a Range of IPs

Screencast Guide: Capure Your Linux Desktop on Video!

How to Setup an Encrypted Filesystem

Grant Root Privileges to Regular Users

Setting Up a SHOUTcast Server + DSP Plug-In
User opinions:

Comment #1 by: anonymous on 10 Dec 2007, 13:42 GMT reply to this comment

there are some errors in this text.

for example:

- you use C-p to switch to the previos window not C-n
- split windows do not contain different screen sessions but different windows of one session

Comment #2 by: Jesús Gómez on 26 Jan 2009, 01:38 GMT reply to this comment

The best explanation of screen i'v readed. There is not enough documentation. Thank you.

The C-n switch you to the previos window because there are only two windows in the example, so it's correct. But yes, C-p switch you to the _p_revios windows.

Comment #3 by: linuxnewbee on 05 Mar 2009, 19:19 GMT reply to this comment

Excellent documentation about Screen. Thank you.

Comment #4 by: Maj on 25 Jun 2009, 01:29 GMT reply to this comment

That's a vertical split, not a horizontal one.

A horizontal split would be dividing two regions into left and right.

Comment #5 by: chigurh on 14 Jul 2009, 16:24 GMT reply to this comment

Very good rundown. Horizontal is left to right, by the way.

Comment #6 by: GeekBoi on 23 Jul 2009, 16:17 GMT reply to this comment

One thing about the multi-user section. Once you set the screen executable to SUID root you do not need to become root and run screen. Any normal user account can run screen SUID root and add permit other users to connect to that screen session.

Running screen as root and permitting other to attach to the session is very dangerous as EVERYONE that is attached to that session now has root access. Unless you lock them to one screen and monitor every keystroke they can do anything they like in another screen while your not looking.

Charlie

Comment #7 by: Mikhail on 27 Oct 2009, 12:18 GMT reply to this comment

"Horizontal split" is "left to right" only in Emacs. In Vim and in Screen "horizontal split" is two horizontal windows, one above another.

No comments: