how to use screen to protect your ssh session from abrupt internet deconnection

Hello Fellas!

I must admit I have been lazy to learn this “screen thing” my boss has been telling me about. Foolishly I quickly installed the application on my desktop fedora thinking I got it started 😀 . Surprisingly not much of all the posts I have come across have painted it clearly that screen is meant to be installed on a SERVER one connects to.

So people! it’s for the server you connect to remotely using ssh command. OK?

All this became very relevant withing 10 mn when my connection got broken in the middle of a frightning night of a major upgrade of one of our products were I have been forced to manually make a copy of a mongodb database. The wicked connection gave up on me at 97% of completion of the task. With the help of Stephen Braimah I learned this in 2 minutes.

In fact his post is not about explaining all the capabilities of screen but to get you going with its basic benefit.I wish I had a post simple like this in my night of frustration.So in this post we will learn:

  • How to install screen on Linux
  • How to start a screen session
  • How to detach a screen session
  • How to reattach a screen session
  • How to kill a screen session

How to install screen on Linux


Screen can be installed from package manager for ubuntu and fedora. Log into your server and issue the commands below:

Ubuntu
[shell]
sudo apt-get update
sudo apt-get install screen
[/shell]

Fedora
[shell]
# in root mode or if you have enabled sudo functions then precede the commands with sudo keyword
yum update
yum install screen
[/shell]

How to start a screen session

There are various ways of starting your screen session. The simplest one is just as type screen to be prompted with a version number and a license statement. At the end of the writeup there is the line below.

[Press Space for next page; Return to end.]

That said, let’s create our session called kodjo.sagemode (I refer to sage mode when I switch to root user), the command is as below:
[shell]
screen -S kodjo.sagemode
[/shell]

That’s it, we are in but it feels like nothing happened because it shows the same linux shell prompt. To be sure that we are in an active session let’s list for all sessions.

[shell]
screen -ls
[/shell]

It should output something similar to what is shown below:

There is a screen on:
19220.kodjo.sagemode (Attached)
1 Socket in /var/run/screen/S-kodjo.

Apparently it’s a big issue to know the actuall current screen name one is in.Since my method doesn’t really answer the question when there are multiple sessions are in use (Yes you can have multiple sessions and run multiple different concerns on each of them). With a little google search, I have found on superuser
that it could be done with this command after the first session is created:

[shell]
screen $SHELL -c ‘screen -X caption always "$STY"’
[/shell]

The command above displays at the bottom of the terminal the name for the current session.

How to detach a screen session

After all is done and you would like to disconnect from the actuall session because some long process is running, and maybe to start a new one , you can run the command below

[shell]
# press and keep control then shift and a and then press d
ctrl+shift+a d
[/shell]

It supposed to show the text below:

[detached from 19220.kodjo.sagemode]

If you want to verify that you have successully disconnected from the session you can use the same screen -ls command. It should output something similar to the text below:

There is a screen on:
19220.kodjo.sagemode (Detached)
1 Socket in /var/run/screen/S-kodjo.

How to reattach a screen session

There are 2 situations leading to a need to reattach a session. Either the communication got interrupted abruptly or an attempt to revisit a detached session.

Normal re-attachment

[shell]
screen -r 19220.kodjo.sagemode
[/shell]

Attachment after broken link
[shell]
screen -d -r 19220.kodjo.sagemode
[/shell]

what we have done above is to detach the session first. Why? because the session is probably attached when the link got broken. So we are detaching it before reattaching.

So with screen, if during a ftp transfer the link got broken, the transaction would still continue and probably finish without you monitoring.After reattaching ,one should see the same state of the terminal before the link got broken.

How to kill a screen session

If you are done and maybe about to disconnect from the server and for some reason you would like to close screen session as well, it’s also easy and very similar to the detaching process:
[shell]
# press and keep control then shift and a and then press d and then confirm by pressing y
ctrl+shift+a k
[/shell]

The command above prompts for a confirmation as shown below. To really quit press y to confirm.

Really kill this window [y/n]

to see more commands and what they do type the command below:

[shell]
# press and keep control then shift and a and then press ?
ctrl+shift+a ?
[/shell]

That’s all folks, this tutorial is not with the intent to be very long and exhaustive. Sometimes, one just needs to really some really simple tutorial about basic usage to get going. That wasn’t necessarily the posts I stumble upon during my quest for a solution. Feel Free to read other Tutorial to have more complete view on how to use screen. I hope this has been for you a really simple “how to” to get you going.

3 thoughts on “how to use screen to protect your ssh session from abrupt internet deconnection

Leave a Reply

Your email address will not be published. Required fields are marked *

captcha * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top