January 1, 2008

Close your frozen ssh session

I am working with Linux quite a lot and sometimes I face this situation: I have a terminal with ssh connected to a remote server and after a while the ssh connection gets frozen on a remote side for some reasons. Now what? I used to find the PID of the ssh process in another terminal and then used kill -9 PID. This method looked a bit too "brute force" just for closing the broken ssh session and I was looking for a more "elegant" solution. Like when I am telnet_ed somewhere, I can use the CRTL+] key sequence as a escape character to break the connection. I found a similar solution for ssh: [enter]~. First press the Enter key, then tilde, and then add a dot and your ssh session will be closed and you can continue your work in back in freed terminal.

14 comments:

  1. THANK YOU THANK YOU THANK YOU!! I have been wondering about this for years! I am a bit fanatical about archiving my bash history, so I always want to kill frozen ssh connections (usually the result of closing my laptop) from a separate [Gnu] screen window and exit bash properly. When I have 10+ windows in screen with different ssh connects in N-1 of them, it is a pain.

    Oh, and if that sounds careless, consider that I run screen both locally and remotely. That way anytime I reconnect to a server I see the terminal exactly the way I left it. (Just be careful not to leave yourself "sudo su -"ed, that's poor security.) If you aren't familiar with Gnu Screen, get informed.

    ReplyDelete
  2. Many thanks Jozef, this is the best tip I got since a long time

    ReplyDelete
  3. Worked a charm. Thank you!

    ReplyDelete
  4. Ah, well... didn't work for me, but my problem is worse.

    I have a screened ssh session stuck within an ssh session. I tried to log out of it, and it got stuck. No escapes work. The only way I know of to kill it is to kill screen session itself, which I don't want to do.

    Any elegant way of fixing this?

    ReplyDelete
  5. @Anonymous: well, maybe screen is doing some characters translation (like the CRTL+A has a special use with screen), and therefore you might want to use a slightly different keys combination.
    Or ... if you can SSH to that server once again (open a new session), then just use the pretty good old way: find your fronzen SSH's PID and kill it with "kill -9 $PID".

    ReplyDelete
  6. Thank you a lot. This was bothering me for months.

    ReplyDelete
  7. Thanks! I've been killing them for years. I should have googled ages ago.

    @RichardBronosky
    Regarding the keeping of your bash history - try adding PROMPT_COMMAND='history -a' to your .bashrc. You'll never lose another command again! ;-)

    ReplyDelete
  8. I love your blog, you should add an RSS feed feature so I can get automatic notifications of new blogs. If you set one up please email me! i will bookmark you for now. Again Excellent Blog!
    _________________
    get vpn

    ReplyDelete
  9. This has been bothering me for years. I'm very happy that I googled it today :)
    Thank you very much!

    ReplyDelete
  10. The most difficult is finding a reliable vpn service that can maintain total anonymity in all Internet connections and avoids the interception of all Internet communication all of this using the internet connection at the maximum speed with no visible delay..

    ReplyDelete
  11. Perfect trick!
    Thank you :)

    ReplyDelete
  12. WOW...Many thanks! Worked great and something I always wanted to know...and needed.

    ReplyDelete
  13. ThankS!
    "Everything I always wanted to know about ssh but was afraid to ask...!"

    ReplyDelete