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


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.

| improve this answer | |
  • have no idea why it's asked not on Unix StackExcnage but it helped me with Linix ssh, thanks! – Bogdan Mart Aug 13 at 17:35
  • @BogdanMart: Maybe that's because macOS is buggier? :) – Seamus Oct 24 at 19:52

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 someuser@someinstance.amazonaws.com

> 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

| 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 Bugajewski Jan 18 '13 at 10:57
  • 2
    Use ~.. See above. – Snowcrash Apr 4 '19 at 8:16

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

Try entering Shift+`+.

| improve this answer | |
  • 2
    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
  • 1
    @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

According to the documentation:

 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
 ~V/v - decrease/increase verbosity (LogLevel)
 ~^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.)
| improve this answer | |
  • It's from command ~?, just copy-paste – Adam Krawczyk Apr 25 at 13:03

~. works, although it looks like being totally stuck, stops the ssh connection and you can start it again, but in my keyboard I have to press Alt+~ then space and then .

| improve this answer | |

For me, working with Terminal in macOS Mojave 10.14.6 the only solution was to enter ~ followed by Ctrl-Z as described in this answer: https://apple.stackexchange.com/a/175670/136875

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .