Screen - howto

By: John McFarlane <john.mcfarlane@rockfloat.com>
Last updated: 07/22/2008 @ 23:59

Abstract:
This document is a beginners guide to screen, a program that lets you detach and later re-attach to a running program.



1. Install screen

On gentoo installing screen is quite simple:
root# emerge screen
I'm finished with this step

2. Set some usefull preferences

Create the file ~/.screenrc with the following contents:

# Use visual bell
vbell on

# Set a big scrolling buffer
defscrollback 5000

# Set the caption on the bottom line
caption always "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= @%H - %LD %d %LM - %c"

# Support UTF-8
defutf8 on

# Don't show the slash screen
startup_message off
    
I'm finished with this step

3. Start your first screen session

Start screen itself (this will be the master screen, or number 0):
user# screen
Now open a program you want to be able to disconnect and connect to:
user# irssi
I'm finished with this step

4. Detach from the running screen session

Don't worry, irssi won't actually quit, but type: ctrl+a ctrl+d
At this point you will return to your shell prompt, but irssi is still running in the background, pretty neat!
I'm finished with this step

5. Reconnect to the running screen session

Here's how you reconnect to the master screen session:
user# screen -r -d
I'm finished with this step

6. Create another screen

This will be the first child screen, thus named 1
Type: ctrl+a c to create it. Now you should see a shell prompt again, try doing:
user# top
which will open up the performance monitor in this screen.
I'm finished with this step

7. Toggle between the two screens

Since you now have two screens running, you have two ways to toggle back and forth:
  1. Use ctrl+a n to go next, and ctrl+a p to go previous.
  2. You can also specify the screen name by number like this: ctrl+a 1 to toggle to "top" and ctrl+a 0 to toggle to "irssi".
I'm finished with this step

8. Assign a name to the currently active screen

Type: ctrl+a A which will give you a little prompt, it will probably have bash as the current name, hit the backspace key a few times, and then type in a name you will recognize. Use the enter key to save this screen name.
I'm finished with this step

9. Pull up a list of all running screens

If you get a bunch of screens running, you might need a list to find one faster (here your names are super helpfull). Do: ctrl+a " which will give you a list, you can either type in the number [0 - 9] or use the arrow keys to navigate, again just hit enter to choose it.
I'm finished with this step

10. Scrolling up/down in a screen

If you need to scroll up or down you need to enable copy mode. To do this do: ctrl+a ESC and then use the arrow keys and page up/page down keys to navigate. When you're finished hit ESC to return to normal mode.
I'm finished with this step

11. How do you close a screen?

Closing the program running in the screen should take care of it. For example, if you do ctrl+a ctrl+n which will create a new screen and leave you at a shell prompt, hitting ctrl+d to exit the shell will also close the screen. If you're ever not sure who many are running, just use ctrl+a " to view the ones running.
I'm finished with this step

12. Quick toggle between two screens

If you're using two screens and wanna quickly go back and forth between them, use: ctrl+a ctrl+a
I'm finished with this step

13. Fixing a screen that seems locked

If you hit ctrl+s or ctrl+a s you can lock the screen...

The fix is to hit ctrl+a q
I'm finished with this step

14. How to list running screens

You can always list the screens that you have by doing:
screen -ls
Unfortunately this doesn't show screens of other users. The only way I [currently] know how to do this is to look using ps:
ps -ef | grep SCREEN
I'm finished with this step

15. Shared screens

Temporarily make screen setuid (remove this when your finished):

root# chmod u+s /usr/bin/screen
root# chmod go-w /var/run/screen
    
Start a screen session:
user# screen
Then do (where guest is the userid of the person who's going to connect to the shared session, and owner is the userid of the person creating the shared session, aka the person who owns the computer):

ctrl+a :multiuser on          #Enable shared sessions
ctrl+a :acladd guest          #Give rights to guest
ctrl+a :aclchg guest -x "?"   #Revoke permission to execute commands
    
Then the other user will ssh to the box, and execute:
user# screen -x owner/
to join my session

Tip If you are unable to connect to the screen session try using the fully qualified name, like this: screen -x owner/9322.pts-2.monster

When you're finished with the session, clean things up by exiting the screen session, and do:

root# chmod u-s /usr/bin/screen
root# chmod 775 /var/run/screen
    
I'm finished with this step

16. Screwed up screens

So maybe I've had a problem where a screen session seemed to locup a time or two. Here's some really good advice from lizzie.spod.cx:

"If you accidentally freeze screen (usually this happens if you hit ctrl+s or ctrl+a s you can fix it by hitting ctrl+q or ctrl+a q. Another way to potentially break screen is if you it the pause/break button while using putty or ctrl+z, then you simply need to type fg at the command line to bring your irssi session back. The fg trick also works should you manage to make anything else in screen dissappear with ctrl+z or pause/break."

I'm finished with this step

17. Getting help on commands

You can get help by using: ctrl+a ?. The Gentoo wiki also has some very good documentation on using screen.
I'm finished with this step

Changelog: Date Description
10/05/2005 @ 01:00 Initial creation
10/30/2005 @ 16:00 Added initial info on shared screens
10/02/2006 @ 15:32 Added .screenrc file, replaced ctrl+a with ctrl+o
02/20/2007 @ 20:36 Added info on copy mode, link to Gentoo wiki
07/20/2008 @ 23:29 Switch back to ctrl+a (I'm no longer an emacs user)
07/22/2008 @ 23:59 Add link to lizzie.spod.cx (not sure how I missed that), and clean up a few things

This document was originally created on 10/5/2005


Disclaimer:
This page is not endorsed by gentoo.org or any other cool cats. Any information provided in this document is to be used at your own risk.