rackAID

Call Sales: 877-435-2445

Navigation

Resources

Linux Screen Tutorial and How To

Posted by Jeff H. 01/01/2008



Using Linux Screen for Session Management

Lost your shell connection? Need multiple shell sessions?

You are logged into your remote server via SSH and happily plucking along at your keyboard and then it happens. Suddenly, the characters stop moving and then you get the dreaded “Connection Closed” message. You have just lost your session. You were halfway through some task and now you have to start over. Ugh. Well you can prevent this from happening by using screen. The Linux screen tool can not only save you from disconnection disasters, but it also can increase your productivity by using multiple windows within one SSH session.

Linux Screen for Session Management!

Screenshot of Linux Screen Terminal

Linux Screen showing the GNU page for Linux Screen Itself

Linux Screen Can Save you from that Lost Connection

What is Screen for Linux?

As the man page states, “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” This can be a life saver when working on your dedicated server. Screen has a several great features for helping you administer your server more productively and safely. I am going to discuss the three features (multiple windows, logging, sessions) that I use the most but be sure to see the man page for full details.

Installing Screen on Linux

Chances are that you already have screen on your system. On most Red Hat distributions you can find it in /usr/bin/screen. To see if screen is in your path, you can use the which command:

[admin@ensim admin]$ which screen

If you do not have screen, then you can install it easily from an RPM or the package file for your system. On Cobalt Raq servers, you can safely use the RedHat RPMS appropriate for your system.
Screen RPMs: rpmfind
Screen Web site: GNU Screen

As you probably already have screen or can use an RPM, I am not going to cover the building of screen from source. Lets get on to how to use screen.

Using Screen

Screen is started from the command line just like any other command:

[admin@gigan admin]$ screen

You may or may not get a text message about screen. If you do not, then you probably think nothing has happened, but it has. You are now inside of a window within screen. This functions just like a normal shell except for a few special characters. Screen uses the command “Ctrl-A” as a signal to send commands to screen instead of the shell. To get help, just use “Ctrl-A” then “?”. You should now have the screen help page.

Screen key bindings, page 1 of 2.
               Command key:  ^A   Literal ^A:  a
break      ^B b       lockscreen ^X x       reset      Z         
clear      C          log        H          screen     ^C c      
colon      :          login      L          select     " '       
copy       ^[ [       meta       a          silence    _         
detach     ^D d       monitor    M          split      S         
digraph    ^V         next       ^@ ^N sp n suspend    ^Z z      
displays   *          number     N          time       ^T t      
fit        F          only       Q          title      A         
flow       ^F f       other      ^A         vbell      ^G        
focus      ^I         pow_break  B          version    v         
help       ?          pow_detach D          width      W         
history            prev       ^P p ^?    windows    ^W w      
info       i          readbuf    <          wrap       ^R r      
kill       K          redisplay  ^L l       writebuf   >         
lastmsg    ^M m       remove     X          xoff       ^S s      
license    ,          removebuf  =          xon        ^Q q      
                 [Press Space for next page; Return to end.]

Key bindings are the commands the screen accepts after you hit “Ctrl-A”. You can reconfigure these keys to your liking using a .screenrc file, but I just use the defaults.

Multiple Windows

Screen, like many windows managers, can support multiple windows. This is very useful for doing many tasks at the same time without opening new sessions. As a systems manager, I often have four or five SSH sessions going at the same time. In each of the shell, I may be running two or three tasks. Without screen, that would require 15 SSH sessions, logins, windows, etc. With screen, each system gets its own single session and I use screen to manage different tasks on that system.

To open a new window, you just use “Ctrl-A” “c”. This will create a new window for you with your default prompt. For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top. (Note: I have truncated some screens to save space.)

Start top

  Mem:   506028K av,  500596K used,    5432K free,       
    0K shrd,   11752K buff
    Swap: 1020116K av,   53320K used,  966796K free        
              393660K cached
     PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %ME
     6538 root      25   0  1892 1892   596 R    49.1  0.3
     6614 root      16   0  1544 1544   668 S    28.3  0.3
     7198 admin     15   0  1108 1104   828 R     5.6  0.2

Now open a new window with “Ctrl-A” “c”

[admin@ensim admin]$ 

To get back to top, use “Ctrl-A “n”

   Mem:   506028K av,  500588K used,    5440K free,       
    0K shrd,   11960K buff
    Swap: 1020116K av,   53320K used,  966796K free        
              392220K cached
     PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %ME
     6538 root      25   0  1892 1892   596 R    48.3  0.3
     6614 root      15   0  1544 1544   668 S    30.7  0.3

You can create several windows and toggle through them with “Ctrl-A” “n” for the next window or “Ctrl-A” “p” for the previous window. Each process will keep running while your work elsewhere.

Leaving Screen

There are two ways to get out of screen. The first is just like logging out of a shell. You kill the window with “Ctrl-A” “K” or “exit” will work on some systems. This will kill the current windows. If you have other windows, you will drop into one of those. If this is the last window, then you will exit screen.

The second way to leave screen is to detach from a windows. This method leaves the process running and simple closes the window. If you have really long processes, you need to close your SSH program, you can detach from the window using “Ctrl-A” “d”. This will drop you into your shell. All screen windows are still there and you can re-attach to them later.

Attaching to Sessions

So you are using screen now and compiling that program. It is taking forever and suddenly your connection drops. Don’t worry screen will keep the compilation going. Login to your system and use the screen listing tool to see what sessions are running:

[root@gigan root]# screen -ls
There are screens on:
        31619.ttyp2.gigan       (Detached)
        4731.ttyp2.gigan        (Detached)
2 Sockets in /tmp/screens/S-root.

Here you see I have two different screen sessions. To re-attach to a session, use the re-attach command:

[root@gigan root]#screen -r 31619.ttyp2.gigan

Just use screen with the -r flag and the session name. You are now re-attached to the screen. A nice thing about this, is you can re-attach from anywhere. If you are at work or a clients office, you can use screen to start a job and then logout. When you get back to your office or home, you can login and get back to work.

Screen Logging

As a consultant, I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy. Using “Ctrl-A” “H”, creates a running log of the session. Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.

Linux Screen Tips

Just wanted to mention to other cool tricks you can do with screen. Screen can monitor a window for activity or lack thereof. This is great if you are downloading large files, compiling, or watching for output. If you are downloading something or compiling, you can watch for silence. To start the monitor, go to the screen you want to monitor and use “Ctrl-A” “M” to look for activity or “Ctrl-A” “_” to monitor for silence. Then open or switch to a new window. When the monitor detects activity or silence, you will get an alert at the bottom with the window number. To quickly go to that window, use “Ctrl-A” “ (thats a quote mark, ctrl-a then a “). After you do this, just type in the number of the window and enter. To stop monitoring, go to that window and undo the monitor with the same command. For example, to stop monitoring for activity you would use “Ctrl-A” “M” again.

Reference

Screen was covered recently in Linux Magazine by Adam Lazur (Jan 2003, Issue 105). Much of his information was adapted for this rackTIP. Other information was collected from the man pages.

Bookmark and Share

Comments

maks commented on Linux Screen Tutorial and How To

maks · 11/19/2009 05:10 am

Fyi, you can attach with just the pid i.e. “screen -r 12345”

Great tut!

How 2 dev commented on Linux Screen Tutorial and How To

How 2 dev · 11/24/2009 10:49 am

I used to work with nohup until some problems with it made me use screen which is far better from nohup.By the way nice post especially for newbies.

Julian commented on Linux Screen Tutorial and How To

Julian · 12/07/2009 06:32 pm

Thanks for tutorial! Close (x) will close screen on not ?

rackAID commented on Linux Screen Tutorial and How To

rackAID · 12/08/2009 09:51 am

I typically just exit from the screen by typing “exit”.

lockedout commented on Linux Screen Tutorial and How To

lockedout · 12/22/2009 11:27 pm

Installed screen on an ubuntu server and couldn’t log back in.

Guessing it messed up some pam config.

Searching for anything with “screen” in it is an exercise in aggravation.

chusty commented on Linux Screen Tutorial and How To

chusty · 01/24/2010 07:24 am

I have screen installed on 3 or 4 dedicated servers ... it’s silly but on one of them I have a bar in the bottom of it - which is very handy… and don’t know how to turn it on on the rest of my servers smile

coskan commented on Linux Screen Tutorial and How To

coskan · 02/08/2010 06:51 pm

extremely helpful and definitive. thank you for sharing

Laetitia commented on Linux Screen Tutorial and How To

Laetitia · 02/18/2010 04:27 pm

I am using ssh and screen to work remotely on a linux server with my macbook. It works quite well to some extend. The only problem I have is that there is nothing like “Ctr-A” on a mac keyboard. So I cannot use “K”, “p” and “n” directly. Does anybody know what shortcut I need?
Thanks.

Johan commented on Linux Screen Tutorial and How To

Johan · 03/09/2010 09:55 am

@Laetitia - I use a macbook (intel osx 10.4) and have no trouble… im pretty sure you will find a “Ctrl” key on your mac keyboard, somewhere to the left of space bar and below your shift key.
http://images.apple.com/keyboard/images/gallery/wired_1_20070813.jpg

garvald commented on Linux Screen Tutorial and How To

garvald · 03/09/2010 06:46 pm

laetitia - mac keyboards have a “ctrl” key and of course the “a”  key - surely you dont mean that?

Juli Z commented on Linux Screen Tutorial and How To

Juli Z · 03/15/2010 10:20 am

We just got our first Mac in our office last week.  Will have to ask our new staff member how to do it.

Radu D commented on Linux Screen Tutorial and How To

Radu D · 03/18/2010 06:52 am

There is any way to run this tool on a linksys router wrt54GL with dd-wrt firmware on it?

Richard commented on Linux Screen Tutorial and How To

Richard · 03/19/2010 09:49 am

Instead of using Ctl-A for commands, I use the backtick key (key to the left of the ‘1’ key).
To do this I entered the following commands into my .screenrc file:
defescape ``
escape     ``
(those are 2 backticks for each entry)
Now to create a new screen window, all I have to enter is `c.
Great write-up.  Just enough for me to go and find additional information & uses for the screen command.

John Doyle commented on Linux Screen Tutorial and How To

John Doyle · 04/01/2010 12:10 pm

Nice I used this to run fsck and remote back in later on from home as it requires user input and you cannot use nohup with fsck easily if you want user input to fix errors etc.
Thanks

supi007 commented on Linux Screen Tutorial and How To

supi007 · 04/09/2010 04:05 am

This is a good tutorial. Thx.

jacco commented on Linux Screen Tutorial and How To

jacco · 04/15/2010 04:54 am

chusty: Add “hardstatus alwayslastline” to your .screenrc

dsb45 commented on Linux Screen Tutorial and How To

dsb45 · 05/01/2010 05:32 pm

Hi,
What about env variables with screen command ?  Do you export all you need in new window, or what procedure should one follow ?
Thanks !

quenenni commented on Linux Screen Tutorial and How To

quenenni · 05/03/2010 02:22 pm

Thanks for your article. I discovered control keys I didn’t know. The logging thing sounds great.

2 points I wanted to add:
Another nice thing with screen is that several access can be done at the same time.
That means several people can connect to the same screen and see live what others are doing.
Great stuff when ie you want to explain how something is working.. a bit like a live tuto.

And you can also use the .screenrc file to change the way it shows the sessions. Ie, all the active sessions are shown at the bottom and colored, you can give them a name, you can choose the number of sessions to launch when starting screen and execute a command in these sessions, like a ssh tunnel or a monitoring software.
you can greatly improve the layout of screen by changing them.

As it was someone who gave me that config file, I don’t know what options exactly do these tricks.
If you are interested to have mine to analyse the options, contact me.

Matthew Kress commented on Linux Screen Tutorial and How To

Matthew Kress · 05/21/2010 06:43 am

quenenni, I would love to see you config file.  Could you please post it?

Alex commented on Linux Screen Tutorial and How To

Alex · 05/21/2010 08:15 am

Thank you, a great article, very helpful!

mehrdad commented on Linux Screen Tutorial and How To

mehrdad · 05/24/2010 04:50 pm

Thanks, It was very helpful.

Mech commented on Linux Screen Tutorial and How To

Mech · 06/07/2010 01:09 pm

Thanks, extremely well writen

dsb45 commented on Linux Screen Tutorial and How To

dsb45 · 06/07/2010 07:17 pm

quenenni,
I would also be interested in seeing
your config file.  As Matthew requested, can you post it, or a url to where we can view it ?  Thanks for your input.

quenenni commented on Linux Screen Tutorial and How To

quenenni · 06/09/2010 06:59 pm

Yop yop..

I sent the file to the website owner already few weeks ago, but it seems something went wrong.

Anyway, here it is:
http://quenenni.domainepublic.net/screenrc.txt
Put it in the home directory under the name ‘.screenrc’ .

Jeff H. commented on Linux Screen Tutorial and How To

Jeff H. · 06/10/2010 10:34 am

I know you submitted a ticket in our sales area but I never saw the other email with the screen file. I wonder if the security system flagged it or someone else here deleted by mistake. Thanks for posting it again.

steevo commented on Linux Screen Tutorial and How To

steevo · 06/18/2010 05:13 am

Thanks a lot, a friend showed me once how to use screen, and this is a great refresher/reference. It should be installed as default when you apt-get ssh-server

Matthew Wilcoxson commented on Linux Screen Tutorial and How To

Matthew Wilcoxson · 06/29/2010 11:20 am

Yep, screen is a really useful tool.

At the moment I use the enhanced version called “byobu”, which adds a cool bar at the bottom of the window with useful information. (That’s probably what @chusty has found)

Aaron commented on Linux Screen Tutorial and How To

Aaron · 07/07/2010 04:47 am

This tutorial is very clear and really helpful.  Sometimes there are just a few small things that you need to get over a learning curve and this did it for me.
I am using screen on my Dreamhost hosted site to run Nodejs to experiment.  I can log out of my ssh session and node is still chugging along.  Thank you!

Jeff H commented on Linux Screen Tutorial and How To

Jeff H · 07/07/2010 09:28 am

Glad you liked our tutorial.  I think there are some newer tools out there, but I always fall back on screen for its simplicity and easy of use.

Add Comment

Notify me of comments?