Screen Guide


This is a summary of a thread on the mailing list in late November 2005


said: Screen is like having multiple virtual screens all running in a terminal. Think of it as a sort of "pager" (like the desktop switcher in KDE/GNOME) but for text sessions. You can also do amazingly smart stuff with it like start a session, detach it, and then reconnect later from a different terminal - great for catching up on work at home. And it also keeps things running, so you can start a long job and you never have to worry about getting cut off through SSH, network outages etc.

Basic usage

Make sure you have the "screen" package installed - also note that the screen binary will need to be SUID root. The basic usage of it is just to type "screen" at the prompt and hit enter. Depending on your set up, you may get a usage screen, which you can dismiss, and then that's it. You're in a "screen" session. The usage, once you're in screen is to press the screen hotkey combination, and then some other key, depending on the function you want to use. By default, the screen combination is Ctrl-a. Try pressing Ctrl-a followed by ? - you should get a page or two of help. Press return to end, and once back at the prompt, type Ctrl-D to exit and you should see "screen is terminating".

Tweaking defaults

I normally make a few changes to the configuration files so that screen is a little easier to use and provides more of a visual clue what it's doing. Firstly, add the following to your login file (.bashrc or .profile etc) everything between the --cut-- lines : --cut-- tty > /dev/null && stty -ixon -ixoff --cut-- Then, create a file in your home directory called ".screenrc", and put the following in (everything between --cut--): --cut-- chdir startup_message off vbell off caption always "%{= bb}%{+b w}%n %h %=%t %c" hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<" activity "Activity in %t(%n)" escape ^Ss shell -/bin/bash --cut-- This does several things. It firstly remaps the screen hotkey to Ctrl-s instead of Ctrl-a, which I find easier to use and also lets me use Ctrl-a from inside screen (Ctrl-a by default lets you jump to the start of a line in bash). The tty line stops the Ctrl-S combination from messing up your terminal (it stops ouptput from being displayed otherwise). It also sets up some visual stuff like a status bar, turns off the startup message, switches to your home directory and runs /bin/bash as your login shell. Obviously, tune this to your needs. Log out and back in again (or just source your .profile), and re-run screen. You should now get a fetching blue bar along the bottom with the time displayed in the far right.

Using screen

Try this - run "top" in your new screen session. You'll get some output on system statistics being displayed etc. Now, type Ctrl-s followed by "c" (short cut for new window), and you'll be in a new session. The "top" command is still running in window 0 - try switching back again using Ctrl-s followed by another Crtrl-s. This toggles between the last two viewed windows. Try creating another new window - you should have 3 open, and you can see them all listed at the bottom. Jump between them by using Ctrl-s and then the window number (eg 0-2). To make it clearer what's running, try Ctrl-s/A (Ctrl-S followed by a capital "A") - you can give your windows names. So you can set it up so you have a Mail session in one window, a command prompt in a nother, system stats in another, IRC in yet another - you get the idea. It's like having multiple terminals all open at once through a single session. Now for some more funkiness - Try Ctrl-s/d. This will detach your session and leave it running in the background. Log out of your server totally and then log back in. Run "screen -r " which will reattach to your screen session - you can see that the "top" process and anything else you were running is just as you left it. If you accidentally kill your login, or your connection dies while you are working in a screen session, you can reattach using "screen -dr", which says to detach the running session and reattach it to the current console you're using. Go back to the "top" window. Now do Ctrl-s/S (capital S) - this splits the window in two. Press Ctrl-s/tab (tab key) to switch between the two halves. Once you're in the new bottom half, either use Ctrl-s/c to create a new window, or use Ctrl-s and the number of an existing window to bring it into the split screen. Ctrl-s/X then closes the window you're in. (if you want to close a window, you can either get back to the command prompt and type Ctrl-D as usual, or you can use Ctrl-s/k to kill the window and processes running in it) That's just the start of it - just use Ctrl-s/? and have a play with some of the options. Mark

Thomas Adam Replied:

Cool. You might want to also look at the HantsLUG wiki page for screen:

> --cut-- > tty > /dev/null && stty -ixon -ixoff > --cut-- This really is not something that I would recommend. If you have messages going to a specific TTY, then they're probably going to be quite important. At best, you should probably do:

 mesg n

which should stop anybody trying to write to the specific TTY. Of course, this won't stop root broadcast messages, but then that's a good thing. Of course, your command is potentially fatal, in one's ~/.bash{rc,profile}, since that implies any process's information is ditched. I tend to use (^A") which will bring up a list of window that I can then select. Note that, ^A at the shell only works where one has: set -o emacs (Admittedly, this is the default, although I set mine to 'vi). Thomas Adam

Mark replied:

Interesting. Could you elaborate a little more on why that's A Bad Idea ? As far as I was aware, all it did was turn off START/STOP output control for the terminal (The Ctrl-s / Crtrl-q keys) if "tty" returns any output (ie: you are running from an interactive terminal) Mark Round (See also SharingTerminals)