Screen: detachable and shared command line console

summary: GNU Screen is a useful tool for server maintenance.

Introduction

Imagine you are working in your favorite cafe and need to relocate shortly. You're ssh logged into a server and perhaps watching a log file or in the middle of editing some configuration files. It would be great if you can log out but return exactly to what you are looking at when you log back in later. That greatness is explained on this page.

Imagine another scenario where you are working on debugging a situation with a co-worker. Ideally you can pull your chair up next to co-worker and look at the code together, but likely your located in different cities and/or time zones. Common solutions to this is to use desktop sharing features incorporated into software such as Skype or BBB but those can sometimes be slow. If all you really need to look at together is the command line, wouldn't it be awesome to have zero-lag terminal session sharing. Awesomeness follows.

Screen Primer

In most any Unix system there is a command called screen. Try running this command in your terminal or while logged into a server. Simply type screen and enter. You might see a version and license notice, after which you have a command prompt again. You can exit this command prompt by typing exit and that will end the screen session (but what fun is that?). Type something like this to help keep track:

echo this is in a screen

All screen commands use an escape key sequence that starts with Ctrl-a then another letter (not sure what the equivalent is on a mac). The common notation for this is ^A even though a lower case 'a' is used. To see a list of key bindings while in a screen do ^A ? (which is Ctrl-a then '?')

Detach from your screen session with ^A d. Now you should see the command prompt as it was before you typed screen. If you do this on a server, you can log out and your screen session lives on.

Reattach to your screen with the following command:

screen -r

List current screen sessions:

screen -ls

This is helpful if multiple screen sessions are running. If there is more than one running then "screen -r" will not know which one to reattach to. In this case you have to include the name of the session from the list that you want to connect to after the -r. For example:

screen -r 30889

will reconnect to the specified screen. Note you only need to type the length of screen name so that is unique from the others. If you're not sure which session you want, the (Detached) in the listing might be a good clue.

After you typed screen -r you should see the echo command that you did earlier or whatever you had in your screen. When you are done your work you should exit, and thus finish your screen session, so that there are not forgotten sessions lying around.

Sharing your Session

Now that you have learned '-r' to reattach you can disregard it because '-x' does that too and more. Sharing a session is simply reattaching to a screen session to which someone else is already attached. Both users attached to the screen have full read/write control of it. Use the '-x' similar to '-r' as explained above. In most cases there is only one session going so just type the following:

screen -x

...and you should then be attached to it.

Let's say you and a co-worker are to share a screen. You each need to log in as your own user then switch to the project user and join the screen session. This can be done quite simply, for example:

ssh r_david@home.civicactions.net
su -lc "screen -x" w_home

Start a new screen session do the same as above but without the '-x' in the screen command. You could also 'su' as you normally do and execute the screen -x command separately but one-liners are fun.

Another example is to reconnect to where you left off. This can be done in one ssh line using the -t option which forces pseudo-tty allocation, IOW makes it so screen executes:

ssh -t w_home@home.civicactions.net screen -x

Note: it is important to remember the difference between exiting the session and detaching. You can't reattach/join if the session is closed.

Further reading: Here is a nice quick reference for more learning more screen commands.

Tags: 

Groups audience: 

Openness: 

Public - accessible to all