How to Recover from an Accidental SSH Disconnection on Linux

SSH remains the most common way by which we access our Linux servers. Most people don’t manually log into the server console itself. Through SSH, you can do almost everything except for certain low-level operations. However, this leaves us open to the possibility of disconnection. It might be unexpected but is a very real possibility. A sudden fluctuation on the Internet, a power surge, and your SSH session are immediately terminated. Along with that, any processes that might have been running are typically terminated as well. Not just the ones active, but all the background process as well. If you were doing something important that shouldn’t be interrupted in the middle – like a complex installation, this can completely mess up your system.

 I just close my SSH session. I don’t exit gracefully or anything. I just press the “X” at the top of the Window. When I log in again, my process has vanished. It no longer exists and was killed as soon as I closed SSH:

Now let’s try the same thing with screen activated. In the screenshot below, I run the same command, but this time inside a “screen”

This is a new screen session, within screen “0”. Now when I close SSH and come back, I can get a list of all previous screens using the command:

screen -ls

To reconnect to it, just type:

screen -r

And this will take you back to where you were before the SSH connection was terminated! It’s an amazing tool that you need to use for all important operations as insurance against accidental terminations.

Manually Detaching Screens

When you break an SSH session, what actually happens is that the screen is automatically detachedfrom it and exists independently. While this is great, you can also detach screens manually and have multiple screens existing at the same time.

For example, to detach a screen just type:

screen -d

And the current screen will be detached and preserved. However, all the processes inside it are still running, and all the states are preserved:

You can re-attach to a screen at any time using the “screen -r” command. To connect to a specific screen instead of the most recent, use:

screen -r [screenname]

Changing the Screen Names to Make Them More Relevant

By default, the screen names don’t mean much. And when you have a bunch of them present, you won’t know which screens contain which processes. Fortunately, renaming a screen is easy when inside one. Just type:

ctrl+a :

We saw in the previous article that “ctrl+a” is the trigger condition for screen commands. The colon (:) will take you to the bottom of the screen where you can type commands. To rename, use:

sessionname [newscreenname]

Now you can have as many screens as you want without getting confused about which one is which!

About Rose G

I know who I am. I am not perfect. I’m not the most beautiful woman in the world. But I’m one of them.

Check Also

How to Fix 503 Service Unavailable Error in WordPress

In this tutorial we will show you how to fix 503 service unavailable error in …