Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 63 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
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
~ followed by ? gives you a complete list of the supported escape sequences – Matijs Sep 14 '13 at 10:47

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

Try Ctrl+Break

share|improve this answer

Your Answer


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.