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 processes 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.
In this article, we’ll show you how to recover from an accidental disconnection on SSH.
In a previous article, we’d shown you how to install and use the “screen” package for Linux. While that was for demonstrating how to multi-task on Linux, you can also use the screen to preserve your SSH sessions. It’s a fantastic benefit and one that propels screen to the top of any “must-have” utilities to use on your Linux server.
In the screenshot below, I’ve initiated a process to sleep for 30 minutes and pushed it into the background:
Now 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” as shown here:
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:
And this gives me the output as shown here:
As you can see, there is a screen session here with the name:
To reconnect to it, just type:
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 detached from 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:
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:
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:
As shown here:
And now when you detach the screen, it will show with the new name like this:
Now you can have as many screens as you want without getting confused about which one is which!
If you are one of our Managed VPS hosting clients, we can do all of this for you. Simply contact our system administrators and they will respond to your request as soon as possible.
If you liked this blog post on how to recover from an accidental SSH disconnection on Linux, please share it with your friends on social media networks, or if you have any question regarding this blog post, simply leave a comment below and we will answer it. Thanks!