Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I frequently ssh into my box at home from school, but usually when I change classes and my computer suspends, the pipe will be broken. However, ssh simply locks up - Ctrl+c, Ctrl+z and Ctrl+d have no effect.

It's annoying to have to restart my terminal, and even more annoying to have to close and re-create a new screen window.

So my question, is there an easy way to make ssh die properly (i.e. when the pipe fails "normally" it will exit with a message about a broken pipe)? Or do I have to figure out what the PID is and manually kill it?

share|improve this question
    
If I got disconnected with an active SSH session, it freezes. I just kill it and start a new session. No information is lost because I use GNU screen. –  Lekensteyn Mar 11 '11 at 16:33
    
Me too - screen is the best. But it's still annoying to have to screen -x :P –  Wayne Werner Mar 12 '11 at 6:43

3 Answers 3

up vote 106 down vote accepted

Normal keys are forwarded over the ssh session, so none of those will work. Instead, use the escape sequences. To kill the current session hit subsequently Enter ↵, ~, ..

More of these escape sequences can be listed with Enter ↵, ~, ?:

Supported escape sequences:
  ~.  - terminate session
  ~B  - send a BREAK to the remote system
  ~R  - Request rekey (SSH protocol 2 only)
  ~#  - list forwarded connections
  ~?  - this message
  ~~  - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)
share|improve this answer
9  
For keyboard layouts where ~ is a dead key, the key sequence is Enter ~ Space .. –  Søren Løvborg Nov 20 '12 at 12:55
    
I've added more of the escape sequences which might be useful. :) –  gertvdijk Jan 19 '13 at 17:08
1  
Note that you need to uncomment the line EscapeChar ~ in /etc/ssh/ssh_config (or ~/.ssh/ssh_config if you prefer). –  aditya menon Aug 3 '13 at 6:19

As noted in geekosaur's answer, the escape sequence ~. will terminate the connection.

The full list of escape sequences and what they do can be displayed by typing ~?:

Supported escape sequences:
  ~.  - terminate connection (and any multiplexed sessions)
  ~B  - send a BREAK to the remote system
  ~C  - open a command line
  ~R  - Request rekey (SSH protocol 2 only)
  ~^Z - suspend ssh
  ~#  - list forwarded connections
  ~&  - background ssh (when waiting for connections to terminate)
  ~?  - this message
  ~~  - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)
share|improve this answer
    
How do I close the list of escape sequences? –  kristianp Oct 18 '13 at 22:19

You may also want to setup application-level keep-alives for ssh to prevent SSH from freezing on connection issues. My ~/.ssh/config contains this:

Host *
ServerAliveInterval 15

This makes ssh client send application-level keep-alives every 15 seconds. Whenever three of them fail consecutively (configurable using ServerAliveCountMax), the client considers the connection as hung and closes it.

Opposed to the other option TCPKeepAlive, this is checked within the encrypted channel and is not spoofable.


It is being noted that those keep-alives also help to, uhm, keep long-idling connections alive, i.e. prevent you from having half-closed tcp sessions hanging for hours untouched.

I highly recommend turning this feature on if you run into this regularly, but you should also know about the slight security risk it may impose. A known-plaintext attack might become easier if the attacker knows the interval and contents of an idle connection. This might be the reasons for why it isn't enabled by default.

share|improve this answer
2  
Security reasons. You could go have drink and leave ssh sessio opened and your lab partner who you have worked with in the past 20 years might use your session to hijack the server and destroy it... while you were drinking in your 10 minute break. –  Luis Alvarado Mar 11 '11 at 19:16
1  
@CYREX, eh? And how can turned off by default option prevent you from having unfair lab partners? %) –  ulidtko Mar 11 '11 at 21:31
1  
@ulidtko: Is there any reason for not setting ServerAliveInterval to 1 so that a lost connection is detected immediately? –  krlmlr Jan 19 '13 at 8:01
    
I've improved the post with my knowledge about it together with other 'fixes'. I think it is not the answer to the question as it is more about preventing rather than fixing an already broken one. –  gertvdijk Jan 19 '13 at 17:01
1  
@gertvdijk: Thank you. For me, it works even without the Host line. Also, "Modern ciphers such as Advanced Encryption Standard are not currently susceptible to known-plaintext attacks." (from the link you added)... –  krlmlr Jan 19 '13 at 22:32

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.