jump to navigation

Linux : The screen command — A must for SSH November 4, 2010

Posted by Tournas Dimitrios in Linux.

If you’ve ever used SSH to connect to a server, you ‘ll know its limitations: if you want to open a new window, you’ll need to create a second SSH connection to the server. And if the connection breaks during the SSH tunnel, you’ve lost your progress. This is where Screen comes in. Screen, which calls itself a “full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” is a useful tool to use on SSH connections , but use multiple screens to work in . So you can have 1 screen to write some scripts in, another to tail a logfile and a third to check your IRC messages😉 .

The truth is that the “screen” command can also be used on usual terminal workflow , not only on ssh connections .Screen is not installed by default on all Linux distributions (on Centos 5.5 it’s not installed ), you can verify this by running the “which screen” command , or on Centos type ” rpm -qi screen” . To install it on your Red-hat based distribution run “yum install screen ” .

After the installation is completed , you can start using ” screen” by typing: “screen” .This makes sense, right?🙂
You’ll probably notice that not much happens if you type that. At least, it seems like not much as happened. In fact, you’ve just opened a new “screen” to type your commands. The program “screen” has a few commands of its own, in order to create a new window, and navigate through the open ones.

Once you’ve opened “screen“, you can see a command list by typing “CTRL + A”, followed by “?” (the question mark). By typing “CTRL + A” you state that the next signal is to be sent to the program “screen“, in stead of to the Shell (like you would in a normal shell). You’ll see a list of all bindings known to “screen“.

Common screen commands (from the root  terminal)

Root terminal command Task
screen -S  namexx Create a new screen – terminal (session) with the session-name “namexx”
screen -ls List all screen terminals :
the list will contain the following information :

  • the state of the screen terminals ( Attached , Detached)
  • the PID of of each screen terminal
  • the name of each screen terminal
screen   -R If only one screen terminal is in “detached” state , it will be enable the screen session.
If more screen terminals are in “detached” state , it will list them to make a selection .
screen  -R  namexx Enable a session with screen terminal  “namexx”
screen -d  -R namexx If the “screen terminal ” is already attached on another root terminal , it must first be detached with the “-d”  , before it can be attached on this terminal
screen  -r  nnnn Enable a session with screen terminal that has PID  “nnnn”  (see screen -ls)
screen -t namex -S nameyy Create a new session with screen-terminal with session-name “nameyy” , but also name the terminal “namex ” .From inside a session terminal press :CTL +a and w ” , this will reveal the name of the session terminal . This is very handy functionality .

Common screen commands (from inside the screen session)

screen command Task
Ctrl+a c Create new window
Ctrl+a k Kill the current window / session
Ctrl+a w List all windows
Ctrl+a 0-9 Go to a window numbered 0 9, use Ctrl+a w to see number
Ctrl+a Ctrl+a Toggle / switch between the current and previous window
Ctrl+a S Split terminal horizontally into regions and press Ctrl+a c to create new window there
Ctrl+a :resize Resize region
Ctrl+a :fit Fit screen size to new terminal size. You can also hit Ctrl+a F for the the same task
Ctrl+a :remove Remove / delete region. You can also hit Ctrl+a X for the same taks
Ctrl+a tab Move to next region
Ctrl+a D (Shift-d) Power detach and logout
Ctrl+a d Detach but keep shell window open
Ctrl-a Ctrl-\ Quit screen
Ctrl-a ? Display help screen i.e. display a list of commands

Read as : First Ctrl and a —  then release Ctrl and a  and then options vs c

It sounds confusing ? …let’s do a demonstration with screenshots .

Open a terminal as user “root” and run “who “

  • Lets start a “screen ” with id-name dimitrios (name it as you prefer) .Actually it’s not required to name the screen that we create .But in case that we need to create multiple separated screen terminal , we have to name each terminal unique , so that we can refer to each separately

  • A new “screen” terminal is created inside the “root” terminal .

  • Lets create another terminal inside the root terminal  (watch the “who” command)

  • Lets create another terminal inside the root (watch the name of the terminal and “who” command)

  • Lets create another terminal inside the root (watch the name of the terminal and “who” command)

  • Lets create another terminal inside the root (watch the name of the terminal and “who” command)

  • We have created four terminals inside the root terminal .As you have mentioned , each terminal has a unique name .So it is very easy to navigate through each terminal separately without losing the command that is executed on each terminal .The navigation is accomplished through the key combination Ctrl + a  (read as : firs press Ctrl and then a) followed by the unique name of the terminal that we wish to access.So for accessing terminal “screen 4” , use the combination of                Ctrl + a +4  . An alternative method to navigate thtough the screen sub-terminals is to use Ctrl +a and  n   (n stands for next ) or  Ctrl + a and   p   ( p stands for previous) .To access the root terminal , we have 2 options : 1) inside from any sub-terminal run the command “exit”  . But this terminal will be killed . 2) inside from any sub-terminal run “screen -d ” (detached) , this terminal will be suspended  (  running in the background ) and your cursor returns to the root terminal .

  • Here we are again back on the first root terminal

  • The command “screen -ls ” will list all screen-terminals that are alive (we exited  from them with the “screen -d ” command and not with “exit”

  • Now how can we use a suspended (detached) terminal ? ….  Very easy , just use the command “screen -r ” and the name of the screen – terminal .

  • See ….. that was  so simple .

Now let’s demonstrate a different scenario that involves SSH :

It ‘s late at work and we have to download  and install an application  that will be used from the IT staff . The download process is very – very slowly , it will take a couple of hours . We will begin the download process before we leave our work , and the installation will be made from our home . And of course we are very concerned about security issues , so the connection will be made through SSH . First we create a screen terminal with the id name of downloadUtility (the name is up to you) .

screen -S downloadUtility

  • We will download “wireshark” , yes ….yes ….. the utility that is used mainly from good guys .The installation will be made with the Yum installer .You know “yum search wireshark”   …… and ” yum install wireshark ” . I suppose that these commands are familiar to you , otherwise SSH will be useless for you . Now that the installation process is began , we can return to our home .

  • After a couple of hours we are back home . Lets connect with the computer at work , and finishing the installation process. The computer at home is a Windows XP machine  . So we will using Putty , the graphical ssh client to connect at work. My previous article demostrated how to install Putty on a USB device . Lets get started ……  First the connection will be made with the help of the graphical Putty .It does’nt mater that on my demostation a LAN address is used . The same process goes also via a WAN connections , care must be taken that port 22  (SSH) on the router is configured to go through .

Using username and password and Vualla we are in ……

  • What was the name of the screen terminal we left back on the computer at work ??????? No problem , just run  “screen -ls ” . Oh jes the name was “downloadUtility ” . Lets use the screen terminal  “screen -R downloadUtility ”  on some cases “screen -d -R downloadUtility”

  • Here we are inside the screen terminal ,and ready to take over the process that was left at work

  • Run any command is needed , in our case “yum install wireshark” .After finishing the job use “exit “

  • Vouala we are back on our root terminal again  . Use “exit ” to terminate the SSH connection ………

Alternative utility :

Links :


1. Deepak - September 24, 2012

nice tutorial… thanks!

tournasdimitrios1 - September 24, 2012

You are welcome .

2. Martin Maas - May 29, 2013

Very helpfull

3. Preparando la mina - April 13, 2015

[…] Por último sólo será necesario dejar nuestro anterior script corriendo en el servidor para que fuese actualizando la BD. Si lo hacemos desde SSH, deberemos usar la instrucción screen para evitar que el script se detenga al cerrar la conexión SSH. Más info: https://tournasdimitrios1.wordpress.com/2010/11/04/linux-the-screen-command-a-must-for-ssh/ […]

4. Screen | Hula hula - June 30, 2016

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s