Adding Extra Users to a Screen Session

Although I am assuming you know what Screen is and how to use it I will quickly cover the basics just in case. Screen is a wonderful program that creates a “session” in which several “windows” exist. In each window is a shell prompt, from which commands can be executed and programs can be run. The man page for screen can be seen here

It has 2 major uses. The first is that you can open up multiple screens in one ssh session and tab through them all. This decreases the need for multiple ssh sessions. Its especially helpful if you are working with a server that has a limit on the number of ssh connections at any given time. The second and most fantastic reason to use screen is that it allows you to start a session, excecute a script or commands and reattach later to see the results. A example of this would be if you had to excecute a script which may take a few hours, you could start a screen session and the periodically re attach to check on the progress.

So, the example I am going to use today is on a Gentoo linux server. As many people know one of the reasons users do not like gentoo is becasue each package must be compiled from source and given the appropriate USE flag variables. Many times there will be package conflicts and all sorts of other things especially when moving from kde3.5 to kde4 or some other major shift.

Ok lets get started. Gentoo uses the emerge package manager but screen can be useful on any distro which updates. So to start a screen session I type:

bash

screen -S <name of session>

I will name my session emerge so:

bash

screen-S emerge

Now this will just take me to a “virtual” terminal but now we can run our command. In this case I am going to emerge openoffice

bash

r00t@infected ~ $ sudo emerge openoffice

As you can imagine openoffice will take a while to compile so this is a perfect time to detach from our screen with the Ctl-a-d key combination. Screen has lots of options and default key bindings but I am only going to cover the basics. So we go to lunch and we come back and want to see how the compile is going. We ssh back to the box and re-attach to the session by typing:

bash

screen-rd<name of session>

In case you have forgotten the session name or you have multiple screen sessions going you can type:

bash

screen-l

This will give you a list of sessions. My sesion was named emerge so heres what I would do:

bash

screen-rd emerge

And what I would then see is either the compile still going or I would see the prompt if the compile was finished. Neat huh?

So what was this post about? Well think of a situation where you are in a terminal ssh session but you would like more users than your self to see whats going on. This can be useful in many situations. I will stick with my emerge example. Say my compilation of openoffice failed at some point and I needed someone to remote in and give me some help. Naturally I would want to see what they were typing so I could allow more users to attach to my screen session. Its really simple. After you start your screen session just have each of your users ssh in a execute the following command:

bash

screen-rx emerge

Now I can add as many people as I want to the terminal session and we are all seeing & typing in the same terminal. I have used this in many situations like showing 4-5 people all at once how to compile a C program or something along those lines. The uses for screen are endless and it is by far one of the most valuable shell based tools.