{"id":28962,"date":"2018-10-26T06:52:16","date_gmt":"2018-10-26T11:52:16","guid":{"rendered":"https:\/\/www.rosehosting.com\/blog\/?p=28962"},"modified":"2022-12-14T06:52:04","modified_gmt":"2022-12-14T12:52:04","slug":"how-to-recover-from-an-accidental-ssh-disconnection-on-linux","status":"publish","type":"post","link":"https:\/\/www.rosehosting.com\/blog\/how-to-recover-from-an-accidental-ssh-disconnection-on-linux\/","title":{"rendered":"How to Recover from an Accidental SSH Disconnection on Linux"},"content":{"rendered":"
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.<\/span> Along with that, any processes that might have been running are typically terminated as well. Not just the ones active, but all the\u00a0<\/span>background processes as well<\/span><\/a>. 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.<\/span><\/p>\n In this article, we’ll show you how to recover from an accidental disconnection on SSH.<\/span><\/p>\n In a previous article, we’d shown you how to install and use the “screen” package for Linux<\/a><\/span>. 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.<\/span><\/p>\n In the screenshot below, I’ve initiated a process to sleep for 30 minutes and pushed it into the background:<\/span><\/p>\n 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:<\/span><\/p>\n Now let’s try the same thing with screen activated. In the screenshot below, I run the <\/span>same<\/span><\/i> command, but this time inside a “screen” as shown here:<\/span><\/p>\n 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:<\/span><\/p>\n And this gives me the output as shown here:<\/span><\/p>\n As you can see, there is a screen session here with the name:<\/span><\/p>\n pts-0.test-centos-server<\/span><\/p>\n To reconnect to it, just type:<\/span><\/p>\n And this will take you back to where you were before the SSH connection was terminated! It’s an amazing tool that you <\/span>need<\/span><\/i> to use for all important operations as insurance against accidental terminations.<\/span><\/p>\n When you break an SSH session, what actually happens is that the screen is automatically <\/span>detached<\/span><\/i> from it and exists independently. While this is great, you can also detach screens <\/span>manually<\/span><\/i> and have multiple screens existing at the same time.<\/span><\/p>\n For example, to detach a screen just type:<\/span><\/p>\n And the current screen will be detached and preserved. However, all the processes inside it are still running, and all the states are preserved:<\/span><\/p>\n 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:<\/span><\/p>\n 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:<\/span><\/p>\n ctrl+a :<\/span><\/p>\n 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:<\/span><\/p>\n As shown here:<\/span><\/p>\n And now when you detach the screen, it will show with the new name like this:<\/span><\/p>\n Now you can have as many screens as you want without getting confused about which one is which!<\/span><\/p>\n<\/p>\n
<\/p>\n
<\/p>\n
screen -ls<\/span><\/pre>\n
<\/p>\n
screen -r<\/span><\/pre>\n
Manually Detaching Screens<\/span><\/h2>\n
screen -d<\/span><\/pre>\n
<\/p>\n
screen -r <\/span>[screenname]<\/span><\/i><\/pre>\n
Changing the Screen Names to Make Them More Relevant<\/span><\/h2>\n
sessionname <\/span>[newscreenname]<\/span><\/i><\/pre>\n
<\/p>\n
<\/p>\n
\n