Ask Different is a question and answer site for power users of Apple hardware and software. 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

Sometimes my ssh session in the terminal hangs. How can I exit the ssh session and reconnect?

share|improve this question
up vote 68 down vote accepted

Type ~. (i.e. tilde, period) at the beginning of a line. In other words, press Enter, then ~, then .. (In some languages, you may need to press Shift or Alt to enter the ~ character.)

Generally speaking, the ~ character is an escape character in an SSH session when you type it at the beginning of a line. Type Enter then ~? to see the list of escape commands. The most common ones are

  • ~. to terminate the connection
  • ~^Z (press ~ then Ctrl+Z) to suspend the connection and type a command locally (run the command fg to return to the SSH session)

If you want a tilde at the beginning of a line, press ~ twice.

share|improve this answer
Wow. Super top! To be clear, this solution seems to apply over the domain of all ssh connections, rather than the domain of OSX terminals. – bukzor Jul 8 '14 at 18:16
You're a genius dude! – laike9m Oct 1 '15 at 8:56
I wish I could upvote more than once! Great! – Mike Aski Apr 6 at 12:41

All you need to do to exit an ssh session is type exit at the prompt.

Try entering Shift+`+.

share|improve this answer
It's not accepting user input anymore, it's literally stalled. On Ubuntu there's a keyboard combination that terminates the session that you can use in cases like these. After a while the session will terminate stating broken-pipe but I rather not wait. – Kit Sunde Jan 4 '12 at 19:00
@Kit I fixed my answer. Sorry about that. I didn't realize you meant a completely stalled SSH session. – daviesgeek Jan 4 '12 at 19:04
I usually just wait for it to timeout. Despite the fact that I know about these commands, I always forget that I can use them to expedite the process of getting my shell back. Note @Gilles answer as well, you may have to press Enter/Return once before ~. You can learn more in the SSH manpage (a bit over halfway in), and by typing: ~? into an active/connected SSH session, again, you may have to hit enter once before. – Jason Salaz Jan 4 '12 at 22:24

If your session is hung and the prompt is no longer responsive you can just kill the Terminal instance and all child processes associated with that instance, of which your ssh session is one, should get axed along with it.

If you're really wanting to be thorough though you can open a new Terminal instance, look for ssh sessions in the process list, and kill them via the command line:

> ps -ef  |grep ssh
501  1332   142   0 20Dec11 ??         0:01.33 /usr/bin/ssh-agent -l
501 57172 57150   0  1:58pm ttys000    0:00.01 grep ssh
501 57139 57133   0  1:57pm ttys002    0:00.03 ssh -i/Users/ian/code/ec2-keys/id_rsa-gsg-keypair

> kill 57139

If that doesn't work try:

> kill -9 57139

Don't kill the ssh-agent process.

Or you can open Activity Monitor and search there for sessions and hit the kill button for them:

Activity Monitor filtering for ssh sessions

share|improve this answer
+1, this is what I do. Often where I've left a session running, and then either the connection breaks accidentally or I sleep the laptop, it causes ssh to freeze. I open a new console tab and kill the PID of the ssh process (or close the ssh tab and open a new one) – halfer Jan 6 '12 at 0:07
Does using the keyboard shortcut work on your Mac without tediously killing the process? – Rafael Jan 18 '13 at 10:57

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.