Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
Site Navigation
Linux Forums
Linux Articles
Product Showcase
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Free Publications


Sometimes, you discover a particular program by chance. You have aproblem, post on a mailing list or forum and someone suggests usingprogram foo or program bar. Perhaps it's something that someone else isadvised to use, you read about it on your favorite mailing list and think, "Gee, that sounds interesting. Let me try it."

After you use it, you wonder how anyone can live without it and post to your favorite mailing list or forum, agog with the wonders of your new discovery.

Screen is one of those programs. I cannot count the times I've seen an excited post about it. It's not a glamorous application. It is, as its manpage says, "...a full-screen window manager that multiplexes a physical terminal between several processes." Ok, that doesn't sound too exciting, but it is one of the most useful applications you will find, especially in remote administration.

Most distributions, if they don't install it by default have it available as a package. If not, it can be gotten from Gnu's website. Then, it can be compiled and installed. Assuming you are going to accept all defaults, after downloading the package (version 4.0.2 at time of writing--your version may have a different number)

 tar -zxvf screen-4.0.2.tar.gz  cd screen-4.0.2 ./configure && make make install  

Note that the make install portion must be done as root or with root privilege.

Now that screen is installed, it's time to investigate its usefulness. To start screen, at a command prompt type

screen

You'll see a note about it, and a message to Press Space or Return to end. Do so.

Now it seems as if you're at an ordinary console (or your xterminal of choice) looking at your usual command prompt. Suppose you are in console. For our first example, we'll do something very simple.

You are in your $HOME directory. In there, you have a doc directory and you want to check your docs. Change into your doc directory and do ls.

Unsurprisingly, you see a list of your docs. Now, you need to do a ls of the directory above it, your $HOME directory.

The default prefix for a command while in screen is Ctl+a. For the rest of this article, it will be abbreviated C-a. So if you see C-a in any of the examples (and you will see many of them) don't type an upper case letter C, hit the Ctl key

 C-a c 

You are back at a blank console, in your $HOME directory. The C-a was the prefix to a command and the c is for "command". Now, you can do another ls, and have a list of the files in your $HOME directory. To go back to the first screen, hit

 C-a p 

You are back on the first screen, the one that is showing a list of files in your doc directory. You can go back to the second screen with

 C-a n 

Most of screen's command keys are straightforward. The C-a prefixes all commands, the c was for command or command prompt, the p was for previous and the n for next.

So far, this isn't terribly impressive. One can do almost the same thing with alt+F2, alt+F1, switching between virtual consoles. One could argue that screen's keystrokes are easier for a touch typist. This is even more true in NetBSD and OpenBSD where one switches between virtual consoles with Ctl+Alt+F2. However, this becomes far more useful when you are logged into a remote machine using putty or another ssh client. By using screen, you can work with several virtual consoles on the remote machine with one putty window.

One of screen's best features for those who have to remotely adminster machines is its detach function.

You have to log into a remote machine. After logging in, you have to tar and bzip an enormous directory, an operation which will take 45 minutes. Your connection is not the best, and you worry about being disconnected before the operation is complete. However, the remote machine has screen installed.

After logging into the remote machine, your first command is

screen

Begin the tarring and bzipping.

tar -cf enormous_directory.tar && bzip2 enormous_directory.tar

Now detach the screen

 C-a d 

The C-a as we know is the command prefix. The d is for detach. You will see a message reading

[detached]

and be back at a command prompt. Note that this command prompt is not in screen, it is your normal command prompt.

You can log off and log back in 45 minutes later. After you log in, type

screen -r

The -r means reattach and will put you back in the screen where you have been tarring and bzipping the large directory. If it is done, you can type exit, and you will see a message that screen is terminating.

If you have several things to do on the remote machine, you might lose track of which screen is doing what. When you log back in you can get a list of your screens with

 screen -ls 

This returns a list of screens. You might see something like

 There are screens on: 	6795.ttyp1 (Detached) 	26270.ttyp3.server (Detached) 	26315.ttyp5.server (Detached)	 3 Sockets in /tmp/screens/S-username 

The trouble with this is remembering which screen is doing what. The first number is the PID. So, if you remember that you used the first screen to tar and bzip a large directory, the second screen to copy some large files and the third screen to do something else, then it's easy. You can type

screen -r 6795

and be back at the screen that is tarring and bzipping a file.

If you are going to run several screens, a better way to do this is to start each session with a descriptive name. Say one session will be used for the tar and bzip, another to copy a directory of data and the third session is to copy a large directory of images. Start each session with the -S (as in session name) option and give it a descriptive name. In this case, we'll call them tarbzip, cpdata and cpimage.

screen -S tarbzip

Begin the operation, and then detach the screen with C-a d. Now, do the same thing for the other two screens, starting one with screen -S cpdata and the other with screen -S cpimage. Now, if you log back in and want to check your list of screens, doing screen -ls will show something like

 	6795.tarbzip(Detached) 	26270.cpdata (Detached) 	26315.cpimage(Detached)	 3 Sockets in /tmp/screens/S-username 

So, as the cpdata screen is probably done already, you first check that one with screen -r cpdata. It makes it much easier to keep track of your screens.

There are plenty of other uses for the detach and reattach feature whether or not you do remote administration.

If you're like me, frequently switching between X and the console, it's handy for starting anything that might take a bit of time, whether it be compiling a large program or chatting on irc with irssi or another client that can be used in console. You can begin a screen session while in console, detach it, start X, open an xterm and reattach the session. Suppose, after logging in, I decided to fetch a large file using ncftp, wget or some other program. Now, I decide I want to do some web browsing with firefox. I can detach the screen session where I've begun the download, fire up X and do my browsing. If I want to check on the download's progress, I can open an xterm, type screen -r and see how it's doing.

This is the sort of handy little thing that makes screen such a convenient program. Are there other ways to do that? Of course. In Unix (and by inference, Linux, the BSDs and any other Unixlike system) there's always more than one way to do something. (The corollary is that someone is sure to think your way is completely wrong.) I could start the download in console, then hit Alt+F2 to open another console. In that console, I could start X

Then, I could hit Ctl+Alt+F1 to return to console. It would bring me to the screen where I started X. I would hit Alt+F1 to bring me back to the console where I was doing the download, check on it, then hit Ctl+Alt+F7 (F9 in FreeBSD) to go back to X. I think using screen is easier.

Screen can also open multiple windows in one console. The usefulness of this is obvious. I'll sometimes use it when installing a system where I haven't yet installed X, or don't plan to install X. Sometimes, I forget syntax and want to check the man page, or perhaps, I have a text based browser installed and want to check a particular web page to remind myself how to do something. With screen, this becomes simple. Rather than having to switch back and forth between consoles, hoping I don't forget what was in the second console, I can split the console window. Then, I can work in the top window and in the bottom one, I can open up w3m (my text based web browser of choice) and go to the web page I need, or perhaps ssh into another machine with a configuration file I want to examine. To do this, open a screen session. Then

 C-a Shift+S 

The console window will now be split into a top and bottom window. (The S stands for "split".) Your cursor will be in the top window. To move to the bottom window, use C-a and then hit the Tab key. The cursor will now be in the bottom window. Then, C-a and c for command, and you can type in that second window. To move back and forth between the two windows, use C-a and Tab.

You can actually use more than two windows if you wish. While in either of the two windows once again do C-a Shift+S and you will now have three windows. Do it again, and you'll have four windows, but of course, even with a large monitor none of the windows will have more than a few rows. In practice, I seldom use more than two windows in console.

Sometimes, while trying to tab between windows, you'll accidentally hit C-a n or C-a p and suddenly find that the two windows are identical. If this happens, just hit C-a p or C-a n again and you'll find yourself back where you wanted to be. Obvious though this may seem, it can get the newcomer--suddenly, they find themselves with two identical windows on their split screen and not realizing why it happened, they don't know how to get out of it.

To end a split window screen session, simply close them one at a time by typing exit. When you've typed exit in the last window, you'll get the usual message that screen is terminating.

For a program that doesn't seem to do very much, screen is an extremely handy utility. This article only gives the most basic uses of screen. Take a look at the screen man page. You'll see some of the other things it can do.

Rate This Article: poorexcellent
 
Comments about this article
C-a c stands for create
writen by: Annymous Coward on 2006-03-02 02:33:30
According the documentation, the 'c' stands for 'create', which makes a lot more sense to me anyway
RE: C-a c stands for create written by Annymous Coward:
Indispensible
writen by: Andrew on 2006-03-02 02:45:58
Scott is right. Screen doesn't sound like much but once you start using it you'll never go back. Here's a page of screen resources & links http://aperiodic.net/screen/
RE: Indispensible written by Andrew:
Use of screen over unreliable connection
writen by: Richard on 2006-03-02 03:21:03
Best use of screen is that it survives disconnection of the terminal. If working over ssh over an unreliable connection, or if you just want to pause your ssh connection for a while and come back to it later, you can use screen knowing you won't lose your work when either the ssh connection goes down or you explicitely detatch using CTRL-A CTRL-D. Multiplexing multiple sessions over one ssh connection is an excellent feature. You can also use it to reduce bandwidth usage by switching away from that verbose tar command or apt-get to a blank screen or something less verbose, then switching back later to see how it's getting on. I'll have to try the split mode. That looks useful. Thanks.
RE: Use of screen over unreliable connection written by Richard:
Cut and paste!
writen by: Ian Rowan on 2006-03-02 12:04:35
I first started using screen in Slackware Linux over ten years ago, just so I could cut and paste over dialup and/or with no mouse. Then I started using the multiple command lines. I've never looked back, and continue to use it to this day, with or without a graphical environment.
RE: Cut and paste! written by Ian Rowan:
Odd notation
writen by: DrCR on 2006-03-16 07:58:20
I personally would find it a much more simplistic read if Ctrl+a or Ctl+a instead of C-a was used in the documentation. A moot point, but the Ctrl or Ctl notation is much more prevalent than the C notation used - in my noob, limited experience. Thanks a ton for the write-up! Screen is officially on my "next app to try out" list! :)
RE: Odd notation written by DrCR:
writen by: Steve on 2006-03-21 13:27:41
Amazing, simply amazing! I'm in shock and awe at the clarity of the tutorial, and the power of the simple little utility called 'screen'! From the first few minutes that I experimented with it, I realized the immense usefulness of this program. Thank you, thank you, Scott Robbins, for such a well written tutorial!! You showed enough to get going in a practical way, but not so much that the reader is left confused.
RE: written by Steve:
response to comments
writen by: Scott Robbins on 2006-03-24 17:16:33
Thanks folks, for the kind words. I've had a bit of trouble sending these comments, so hopefully, they won't show up 5 times. As for C-a c being create, according to the docs, you are correct. I've always thought of it as command, however, and for me, as a mnemonic, c for command works better for me. However, as you wrote, the official word from the docs is that it stands for create. With C-a instead of ctl+a, well, as you wrote, it's a moot point. It's the way their docs do it, as do the docs for emacs and some other Gnu docs. I'd just say, get used to it, you'll see it again. Why do they do it that way? I don't know, but I have to admit, it is a bit easier to type for someone writing the docs. :)
RE: response to comments written by Scott Robbins:
Custumize the shortcuts
writen by: rc on 2006-04-03 12:39:13
I would like to find an article where to find howto change the keywords shortcuts. I like screen but I have problems depending of the linux distrubution to get things like the clasical CTRL + e and CTRL + a working fine.
RE: Custumize the shortcuts written by rc:
Amazing !
writen by: Bux on 2006-04-08 08:22:21
I've read about the usefulness of screen and been very curious about it, but never tried it, mostly because I had not found a 'few steps beginning guide' - forcing me to read documentations. Well, this tutorial is very impressive in the sense that it is very straightforward. What I've been doing while administering a linux server remotely is exactly what is written above: creating various sessios so I can do multiple jobs on it. Now server administration is going to be much better with screen ! :) Thanks Scott for this tutorial !!
RE: Amazing ! written by Bux:
Are there other ways to do that?
writen by: okcazure on 2006-04-21 00:02:11
I usually get my similar functionality by using jobs. For instance if using vi on mycode.c I would use Ctrl-Z to background vi and then "man -S 7 socket" to lookup any particulars. I can then Ctrl-Z to background the man page lookup. You can easily switch between multiple jobs with %1, %2, ect and the jobs command if you forgot which job number you wanted. This is nothing new but I find it easy to use and fits in with using multiple virtual terminals/screen/other ways of doing it. That being said the detach function of screen would be useful. I will use it if I ever have an unreliable connection. Great article!
RE: Are there other ways to do that? written by okcazure:
Good content
writen by: Rott_En on 2006-05-08 17:41:00
Thank you for the neat content. As a friend told me once : who needs bouncers when we have screen :-) Keep up the good content writing!
RE: Good content written by Rott_En:
Detach not just for unreliable connexion
writen by: axel on 2006-05-13 05:36:10
The detach ability of screen is not just useful for unreliable connexions. It easily facilitates moving between computers... I might be working on a desktop and then decide to sit in the garden and use my laptop. With screen this presents no problem with regard to existing processes, especially something such as reading mail or Usenet - often keep these programs open for a long time as I may get distracted. It is worth looking at the screen man page for details of the .screenrc file - I like to start several screens at once. Plus either by using the -e option or hard-coding the .screenrc file, it is possible to specify a different control character that the default Ctrl-A to be used. Why would one do this... using a screen on the computer in front of you and then a screen on another machine to which you have an ssh connexion.
RE: Detach not just for unreliable connexion written by axel:
Executing a command with screen
writen by: Yaswanth N on 2006-10-16 00:12:43
Hi I want to execute a command as soon as screen starts. Something like 'perl somefile.pl'. This is coz I want to put this screen command in Init and the scripts start executing in screen as soon as the machine starts. Any idea how to do this? Yaswanth
RE: Executing a command with screen written by Yaswanth N:
writen by: Reid Orsten on 2006-11-10 07:23:05
RE: written by Reid Orsten:
screen <command>
writen by: Crimson on 2006-11-19 17:14:45
RE: screen <command> written by Crimson:
read the docs, really
writen by: painquin on 2007-06-01 08:06:47
RE: read the docs, really written by painquin:
Blank terminal after few mins and cursor
writen by: Saibal Saha on 2008-02-22 01:40:39
Hi all, I am writing for the first time here. I have a strange problem. Every time after few min my console gets blank and also cursor disappears. I can't type any thing in console, but I can type in open office, in address bar. I have to close the console and have to open it again, then it works. I don't know what is the reason. Can you please help me out? -Saibal Saha Linux Administrator
RE: Blank terminal after few mins and cursor written by Saibal Saha:
Screen drops on FreeBSD 7.0
writen by: Paul Otto on 2008-05-09 10:15:30
I've been using screen for years. An excellent tool you can't do without. However, yesterday was the first time in my experience as screen user that the application dropped without any apparent reason. It was running a Perl script that crawls the Web inside the screen and after a couple od hours screen being detached closed without any warning! Do you have any idea what could have caused that? Paul
RE: Screen drops on FreeBSD 7.0 written by Paul Otto:
commands
writen by: Anand on 2009-01-13 14:22:00
This indeed is a wonderful tool, especially when connecting to server with unstable connections. based on experience of using screen, people might have developed some useful init configuration which can be put on .screenrc, if any one has it please share it, i dont want to reinvent wheel. Thks Anand
RE: commands written by Anand:
Reset screen password
writen by: Narcissus on 2009-03-03 19:42:04
Hey, I recently added a password to my screen session running irssi, but then I forgot it >_< is there a way to reset this? or go around it via root? Thanks Narcissus
RE: Reset screen password written by Narcissus:
writen by: D Ashton on 2009-03-16 01:07:53
Great write up; what an awesome tool. One thing, how about scrolling when in split screen mode? I had a quick play but couldn't work it out. Cheers, DA
RE: written by D Ashton:
writen by: D Asthon on 2009-03-16 01:30:24
Guys, there is a scroll feature, and it's better than I could have ever hoped. (search, copy paste, VI-like shortcuts) http://www.samsarin.com/blog/2007/03/11/gnu-screen-working-with-the-scrollback-buffer/ Amazing.
RE: written by D Asthon:

Comment title: * please do not put your response text here