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?



Comments

I find screen invaluable for running a game server in the background, out of the way of everything with no sign (other than a small entry in ps to confirm it's still going) that it's running, and the same can be done with any program:

screen -d -m myprogramcommand

This automatically forks it into a hidden screen process. If I need to check on the server or issue commands I can simply type screen -r in a terminal or console, and there it is. Ctrl-A .... d puts it away again.

By RokenPrice

Thanks for the excellent comment, RokenPrice! I just tried out the command – it's one of the best ones I've learnt recently :)

By Abdullah Chougle

Thanks for the screen tips. For remote desktop sharing, try the excellent teamviewer package. Don't know if it's available on Ubuntu, but it works on Linux, Mac and Windows.

By djohnston

Yes it is available for ubuntu. I am using it and it amazing.

By Parthanil

Add new comment