Use screen to create a virtual terminal in Linux

Vincent Danen illustrates how the Linux tool <i>screen</i> can help you create virtual terminals.

Remote administration with SSH is something every sysadmin is familiar with. Likewise, doing multiple things at the same time, including lengthy compiles or long tasks is also something many of us are familiar with. Freeing up a console usually involves opening a new terminal and initiating a new SSH connection to get further work done, or—if observation of a task isn't critical—you can send the task to the background to free up the terminal.

Another solution exists in the form of the screen tool, a program that is included with every Linux distribution. Screen creates virtual terminals that you can control and interact with via one terminal. What's even better, a screen session can be disconnected without killing a running task. Imagine starting a long compile on a remote server and the connection dies; when the connection dies, so does your task. Screen works around this by allowing you to detach from a running session, log out, and resume it later, even from a different location.

To get started, make sure the screen package is installed using your distribution's package manager and then type:

$ screen

This will start screen and open a new session. To disconnect from a session, type CTRL-A then d. You will return to the prompt from which you issued screen, but everything you have done in screen is still available. If only one screen session is running you can reconnect to it using:

$ screen -R

If there are multiple screen sessions running, this won't work; but you can view a list of running screen sessions by using:

$ screen -list
There are screens on:        (Detached)        (Attached)
2 Sockets in /home/joe/tmp.

Here, you can see there are two sessions running. To connect to the detached session from a different location, you would use (after SSH-ing to that machine, of course):

$ screen -r 13995

where 13995 is the process ID of the screen session you wish to attach to.

There is a lot of help available for screen, and a lot of things you can do with it. You can view the screen manpage, the output of screen --help, and within a screen session, type CTRL-A then ? to get a list of commands you can use when in command mode (invoked by CTRL-A).

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!