How to Use Linux Screen Command For Remote SSH

October 9, 2013 | By
| Reply More

When you are using ssh command, it provides you only one screen(terminal). If you lose that screen, you lose all you were doing on the remote computer. Sometimes it happens when a network glitch breaks the ssh connection and you lose what you were doing and you have to reconnect the remote server again. That can be very bad if you were in the middle of something important. And if you want to do three things at once, for example vi httpd.conf, tail -f /var/log/messages, and service httpd reload, you need to open three separate ssh sessions.

The GNU screen utility is a terminal multiplexer. If you are a system administrator working on remote servers, screen is a great tool for managing a remote computer with only a command line interface available. It lets you disconnect from it, and then reconnect to that same screen session later.

It is installed by default in Ubuntu, for RHEL based system, you can install screen with the help of yum command

# yum install screen

To use screen simply type the following command:

$ screen

You might see a welcome message if it's there, and then see a regular bash prompt in the window. To control screen, press the Ctrl+a key combo, followed by another keystroke. For example, Ctrl+a followed by ? (noted as Ctrl+a, ?) displays the help screen.

Here are some commands and control keys you can use to operate screen.

Screen Control Keys

$ screen -ls
There is a screen on:
7089.pts-1.server10 (Attached)
1 Socket in /var/run/screen/S-francois.

Change window title
$ Ctrl+a, a
Set window's title to: My Server10

Create a new window
$ Ctrl+a, c

Show active window titles
$ Ctrl+a, "
Num Name Flags
0 My Server Up/down arrows change windows
1 bash

Detach screen from terminal

$ Ctrl+a, d

The screen session shown above resulted in two windows (each running a bash shell) being created. You can create as many as you like and name them as you choose. Also, instead of detaching from the screen session, you could have just closed it by exiting the shell in each open window (type exit or Ctrl+d).

Reconnecting to a Screen Session

When the screen session is detached, you are returned to the shell that was opened when you first logged into the server. You can return to that screen again later (even after you log out and disconnect from the server). To reconnect when only one screen is running, type the following:

$ screen -r

If there are several screen sessions running, screen -r won't work. For example, this shows what happens when two detached screen sessions are running:

$ screen -r
There are several suitable screens on:
2242.pts-4.server10 (Detached)
3354.pts-4.server10 (Detached)

Type "screen [-d] -r [pid.]tty.host" to resume one of them.

As the output suggests, you could identify the screen session and select which screen you want to connect to:

$ screen -r 2242.pts-4.server10

Naming screen Sessions

Instead of using the default names, you can create more descriptive names for your screen sessions when you start screen. For example:

$ screen -S testsession
$ screen -ls
There is a screen on:
26523.testsession (Attached)

Sharing screen Sessions

This is very important stuff for system administrators. The screen command also allows the sharing of screens. This feature is great for tech support, because each person connected to the session can both type into and watch the current session. Creating a named screen, as in the preceding section, makes this easier. Then another person on a different computer can ssh to the server (using the same user name) and type the following:

$ screen -x testsession

Just as with screen -r, if there's only one screen running, you don't need to specify which screen you're connecting to:

$ screen -x

 

Filed Under : LINUX HOWTO

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.