176

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

Kit Sunde
  • 3,089

8 Answers8

321

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 ⌥ Option/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.

  • 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! – Johan Alexis Duque Cadena Dec 18 '20 at 11:42
  • 2
    @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 https://apple.stackexchange.com/questions/219603/how-do-i-type-a-tilde-in-spanish-keyboard – Gilles 'SO- stop being evil' Dec 19 '20 at 20:00
  • Works for me with German keyboard layout and Alt Gr + +. – Paul P Mar 12 '21 at 19:47
  • This worked for me in iTerm2 on MacOS. On my US keyboard, I had to type Shift + "~", then ".". I regret taking so many years to look for this answer. – nicbou Feb 07 '23 at 09:54
11

If your session is hung and the prompt is no longer responsive you can just kill the Terminal instance. All child processes associated with that instance, including your ssh session, exit.

A more thorough approach, open a new shell (new tab or window), list ssh sessions and send kill signals:

> 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 [email protected]

> kill 57139

If that doesn't work try:

> kill -9 57139

Don't kill the ssh-agent or sshd Processes.

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

Activity Monitor filtering for ssh sessions

bmike
  • 235,889
Ian C.
  • 45,969
  • +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 06 '12 at 00: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 04 '19 at 08:16
9

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

Try entering Shift+`+.

daviesgeek
  • 38,901
  • 52
  • 159
  • 203
  • 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 04 '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 04 '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 04 '12 at 22:24
4

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.)
1

~. 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 .

Hannu
  • 11
0

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

0

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:

Return

~

Control-z

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

John D
  • 101
0

For mac users (new macs).

it's option + N then shift + ; .