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:
screen -ls
And this gives me the output as shown here:
As you can see, there is a screen session here with the name:
pts-0.test-centos-server
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 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:
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]
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!
what about “&” ?
The “&” at the end of the command will make that command to run in the background.
Many routers will drop your telnet or ssh connection after a couple of minutes of inactivity because they are designed for “intermittent” connections like a browser uses as it requests data in chunks at-a-time from websites.
I use a script that does one thing: it spins in a while-do loop and wakes up every 30 seconds to do a “space, backspace”. Harmless in almost any app at the command line, and keeps activity on the connection. I call the script from the command line with the “&” to put it into the background, and almost never drop connections.
“Screen” is safer and a great choice for critical usage tho….”