Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I ssh to a remote machine and then lose internet connectivity, the session freezes. I can't control-c or otherwise abort and go back to my local xterm or terminal prompt but if I wait several minutes it will do so. There must be some way to force it to abort the remote ssh session when connectivity is lost. I'm on a Mac but I believe this happens on cygwin or linux as well.

share|improve this question
    
Awesome! Glad I could help. –  James T Jun 3 '10 at 3:47

2 Answers 2

up vote 9 down vote accepted

Tilda period should do what you want (~.). It must be the first thing typed after pressing enter, so press enter and then type it. Here is the manual entry for ssh:

 -e escape_char
         Sets the escape character for sessions with a pty (default: ‘~’).
         The escape character is only recognized at the beginning of a
         line.  The escape character followed by a dot (‘.’) closes the
         connection; followed by control-Z suspends the connection; and
         followed by itself sends the escape character once.  Setting the
         character to “none” disables any escapes and makes the session
         fully transparent.

ctrl-d might also work.

Edit: SSH has a lot of escape character commands built in. With an active SSH session open you can press [enter]~? to see a list of options. This is all in the man entry for ssh. If you don't already know about man entries... you will discover a world of amazingness! In the linux terminal type "man command" in this case "man ssh" without the quotes. It has lots of information. Press "q" to quit. You can also type "info ssh" if you like the info interface better.

share|improve this answer
    
[enter]~. works like a charm! Thank you! I can't believe I only now learned this trick. This has been an annoyance for, literally, years. Interestingly, it only works when the connection has died and the terminal is frozen. If it can tell the difference why can't it just do this automatically when the connection dies? –  dreeves Jun 2 '10 at 23:26
    
Thats strange that it only works when your connection died. What gets me all the time is forgetting to press enter first. If you start to type something and backspace to the beginning again... then ~. won't work. I expect there are ways to change the connection timeout. I expect the long timeout was written in so that connection delays between old modems would not drop the session all the time. I added some more stuff to the solution that you might find interesting. –  James T Jun 3 '10 at 0:05
    
I was wrong! It does work regardless of whether the connection has died. Thanks again, James. I owe you big time. –  dreeves Jun 3 '10 at 3:29

Something is holding the connection open, probably from some sort of forwarding. You can try to use Ctrl\ to force ssh to die, but in general you should try to figure out what is wedging it open.

share|improve this answer
    
Ctrl-\ isn't mapped to SIGQUIT on the Mac. –  Spiff Jun 2 '10 at 4:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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