How to Use Linux Screen Command for Remote SSH

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. For example, if you want to do three things at once like '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.]" 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

What tool do you use for remote connection other than screen command? Please leave your comments

Read Also:

1 thought on “How to Use Linux Screen Command for Remote SSH”... add one

  1. I follow the instruction on this article, but I end up by losing as well my ssh connection to my Ubuntu server after enabling the firewall, as well Xrdp is not working for me, witch mean I lost all connections possible to my server :(
    Any advise on that? how I can connect back to my server ?


Leave a Comment