I keep a screen session running on a dev box at work, and I ssh into my dev box and resume my screen session while I'm at work. From there, I hope to other machines.

Occasionally, I find myself ssh'ing to a remote host from my already-remote-screen-session-over-ssh when that remote host I'm ssh'ed to doesn't allow me to disconnect cleanly. So I end up needing to force my remote (second) ssh session to die without affecting my original ssh session connected to screen. I know that killing ssh is done with a enter ~., but like I said, my local ssh client intercepts that key-combo accomplishing nothing more than my needing to reconnect ssh to my dev box.

Anyone ever run into this? How do I terminate the remote ssh session in screen without killing my original ssh session? I'm unable to find anything apropos in screen to do it. This also goes for additional hops IE ssh -> screen -> ssh -> ssh (unable to logout cleanly) <- want to force kill this ssh session.

I tried running enter ~~., which kills the second ssh session, and that kind of works. It's not good enough for say a third, fourth, or more hop, though.

It would be nice to have a solution that works however deeply nested I am ssh'ed into other machines.

  • Consider instead screen -x to attach to an already running session without booting off your other connection? – thrig Sep 2 '15 at 21:46
  • Wow! what's up buddy?? No, that's the point. I'm in an already running screen session. Did I understand what you were saying? – Jim Sep 2 '15 at 21:48
  • (I actually never kill any of the sessions, and just leave them all running.) – thrig Sep 3 '15 at 19:42
  • 2
    This is unrelated to the question, but related to the used insecure approach (of interactively ssh'ing to remote systems from remote systems) -- you may consider to properly tunnel your traffic without exposing your credentials on the intermediate hosts, see: dmitry.khlebnikov.net/2015/08/… – galaxy Sep 4 '15 at 12:59

To kill the nth SSH session, type <enter>, then 2^(n-1) ~, then .. (~~ sends the escape character; thus, two tildes will cause the first SSH to send it to the second SSH, which will then pick up the dot and die. Extrapolate as necessary for deeper nesting.)

I have a similar situation; my solution is to have Mosh on the dev box, which doesn't pick up SSH's escape sequences (but has its own). This strips one layer from the whole thing, which makes things easier.

  • That was it. Thank you Tom. I guess I was on the right track but didn't take it far enough. – Jim Sep 2 '15 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.