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.

  • 2
    have no idea why it's asked not on Unix StackExcnage but it helped me with Linix ssh, thanks! Aug 13 '20 at 17:35
  • 1
    @BogdanMart: Maybe that's because macOS is buggier? :)
    – Seamus
    Oct 24 '20 at 19:52
  • This works only if you have an ANSI keyboard, I have spanish keyboard and doens't work, in spanish the "tilde" key ~ is made with AltGr + 4. But if I change the keyboard layout to english, and I use Shift + º, then it works! Dec 18 '20 at 11:42
  • 1
    @JohanAlexisDuqueCadena I don't know what an “ANSI keyboard” is but I don't think it's relevant. If your tilde key is a dead key (i.e. typing ~ doesn't insert ~ immediately, but typing e.g. ~ then a inserts ã), you may need to type a space after ~ to actually insert a tilde, then type the second character. See also apple.stackexchange.com/questions/219603/… Dec 19 '20 at 20:00
  • Works for me with German keyboard layout and Alt Gr + +.
    – Paul P
    Mar 12 at 19:47

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

  • +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? 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+`+.

  • 3
    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. 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.)
  • It's from command ~?, just copy-paste Apr 25 '20 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 .


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


From Ian's answer on apple.stackexhange, this is the answer for Mac OS X:

The default escape key for the ssh that ships with OS X the ~ character. You have to enter it immediately after a new line for ssh to respect it. And then the key sequence Control-z is used to suspend and background a task in bash.

So try this key sequence:




If it works you'll see something like:

myhost.local:~ |ruby-2.2.0|
> ssh someremotehost
Last login: Fri Mar  6 14:15:28 2015 from myhost

someremotehost:~ |ruby-2.2.0|
> ~^Z [suspend ssh]

[1]  + 48895 suspended  ssh myremotehost

You must log in to answer this question.

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