Remotely Working Together on a Terminal Session in Linux

I often get a chance to work from home, and this has given me opportunities to look for ways to share sessions with my colleagues. Windows XP used to have the excellent NetMeeting tool, but I recently switched to Ubuntu and haven’t yet found an equivalent.

Yes, I’ve heard of VNC and used it a lot too, but I wasn’t happy with its remote desktop sharing performance. Ekiga is interoperable with NetMeeting calls, but doesn’t support screen sharing. Even Skype’s screen sharing does not seem to support remote screen controlling.

However, Linux’s power is at the command line, and this is true even in the case of remote sharing. So here comes screen to the rescue – this magical command allows you to flawlessly resume lost sessions and share them with multiple users at the same time.

Starting screen

Start screen using

screen -d -R session_tomcat

Replace session_tomcat with any other helpful name for the session you’re about to create.

Sharing your Session

Ask your friend to connect using (assuming they are logged in using the same user account):

screen -x session_tomcat

Now it’s simply magical. Multiple persons can type and work on the same terminal – it works best when you’re coordinating over the phone. Note that the dimensions of your terminal output will be the same for every user – to change it, press Ctrl-A and then capital F. This will make the screen output fit your current terminal size, and change it to that size for every connected user.

Detaching from a Screen Session

Important: To detach from the screen session so that you can resume later, simply close the window. If you type exit, you’ll end up terminating the screen session and the processes running within.

Why screen is better than nohup and tail

Here’s a comparison. Consider a hypothetical example in which you need to find a file that contains the word “cat” in its name. You know it will take long, so you run the following combination of commands:

$ nohup find / -name *cat* &
$ tail -f nohup.out

The nohup command is a request to not kill (nohup stands for “no hangup”) the process that you are just about to run in case you lose connection. The & at the end sends the process to the background, so that the prompt returns immediately. The tail -f follows the output of the command executed through nohup. If you do lose connection, simply run tail -f nohup.out again to see the running output.

To do the equivalent in a screen session, you’ll only need run the find command in an open screen session. If you need to reconnect after a lost connection, run screen -d -R <session_name> again, and you’ll be back in your session as though you never left it.

Far more neat, isn’t it? Of course, screen has much more to offer than being a simple replacement for nohup and tail -f. For example, it’s very easy to resume a CLI session running remotely – you’d simply reconnect without having to re-initialize.

Other Advanced Usages

Ctrl-A has a special meaning when running screen – it allows you to execute special screen commands.

To see the list of available commands, type Ctrl-A and then ?.

For example, one interesting feature of screen is its support for multiple windows. To create a window, type Ctrl-A and then c.

To switch to a window, use Ctrl-A and then the window number (from 0 to 9).

To see the list of available windows type Ctrl-A and then ".

The man page of screen reveals a great deal more to explore!

Which features of the screen command in Linux do you find the most useful?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>