Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Many times I have an SSH session that doesn't respond anymore (for example, when I lose internet connection and then reconnect). Ctrl+C, Ctrl+D, Ctrl+Z and a zillion of key presses don't have any effect.

Most of the time I already have tmux or byobu running already, so I can just start another terminal and reconnect. However it does feel cumbersome. How can I disconnect SSH from the current terminal?

share|improve this question

3 Answers 3

up vote 43 down vote accepted

Use the "escape character" (normally, the tilde ~) to control an SSH session:

  • ~ followed by . closes the SSH connection;
  • ~ followed by Ctrl+Z suspends the SSH process;
  • ~ followed by another ~ sends a literal ~.

You can set the escape character using the -e option to ssh.

share|improve this answer
    
Great! How could I have missed! –  phunehehe May 29 '11 at 14:42
5  
You should also remember to press Enter before ~. The escape character works when it is first character in the line. And also you can use ~ and later ? to get help from ssh client. –  Lukasz Stelmach May 29 '11 at 18:44
3  
~ followed by ? gives you a complete list of the supported escape sequences –  Matijs Sep 14 '13 at 10:47

Try Ctrl+Break

share|improve this answer

Openssh can be configured to send client or server alive messages to the other side. If no response is obtained, the connection will be terminated. From the server side, see the ClientAliveInterval and ClientAliveCountMax config options. From the client side, see the ServerAliveInterval and ServerAliveCountMax options. TCPKeepAlive is also an option, but is spoofable while the former options aren't, so they are probably better.

If you set these to reasonable values, you'll probably never have a connection hang again. The only downside is that your ssh connections might stay open if you would rather they would terminate, for security reasons.

See the OpenSSH sshd config man page and the OpenSSH ssh config man page for further infrormation.

share|improve this answer

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.