432

I’m connecting to a server via SSH to send a message to a socket server using a command like:

ssh 181.169.1.2 -p 5566

After the connection is established and I write the message and send it I can’t exit the text mode. I’m only allowed to enter more text and that’s it.

Is there a command or a key combination that allows me to return to command mode?

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
Andreea
  • 4,431
  • 3
  • 15
  • 9

7 Answers7

436

How do I exit an SSH connection?

Two ways:

  • closing the shell session will usually exit, for example:
    • with the shell builtin command, exit, followed by Enter, or
    • Ctrl-d, (end-of-file)
  • in the case where you have a bad connection and the shell is unresponsive, hit the Enter key, then type ~. and ssh should immediately close and return you to your command prompt.

The first option should be intuitive, but how do we know the latter option?

We could learn this information from a careful reading of the man page.

$ man ssh

gives us the SSH documentation, which has the following section on escape characters:


ESCAPE CHARACTERS
     When a pseudo-terminal has been requested, ssh supports a number of
     functions through the use of an escape character.

     A single tilde character can be sent as ~~ or by following the tilde by
     a character other than those described below.  The escape character
     must always follow a newline to be interpreted as special.  The escape
     character can be changed in configuration files using the EscapeChar
     configuration directive or on the command line by the -e option.

     The supported escapes (assuming the default ‘~’) are:

     ~.      Disconnect.

     ~^Z     Background ssh.

     ~#      List forwarded connections.

     ~&      Background ssh at logout when waiting for forwarded connection
             / X11 sessions to terminate.

     ~?      Display a list of escape characters.

     ~B      Send a BREAK to the remote system (only useful if the peer sup‐
             ports it).

     ~C      Open command line.  Currently this allows the addition of port
             forwardings using the -L, -R and -D options (see above).  It
             also allows the cancellation of existing port-forwardings with
             -KL[bind_address:]port for local, -KR[bind_address:]port for
             remote and -KD[bind_address:]port for dynamic port-forwardings.
             !command allows the user to execute a local command if the
             PermitLocalCommand option is enabled in ssh_config(5).  Basic
             help is available, using the -h option.

     ~R      Request rekeying of the connection (only useful if the peer
             supports it).

     ~V      Decrease the verbosity (LogLevel) when errors are being written
             to stderr.

     ~v      Increase the verbosity (LogLevel) when errors are being written
             to stderr.

There is nothing special about exit to ssh, it's just a way to exit the shell, which results in closing the ssh session:

$ type exit
exit is a shell builtin
$ help exit
exit: exit [n]
    Exit the shell.
    
    Exits the shell with a status of N.  If N is omitted, the exit status
    is that of the last command executed.

Citing and quoting reference sources is to provide further evidence for what would otherwise be a perhaps demonstrable assertion of fact, as well as inform the user where more relevant information may be stored.

You want to know that you're doing semantically the correct thing, as well as knowing that it works.

You don't want to learn to invoke as a feature something that is documented as a bug and then later "fixed." Doing the semantically correct thing will continue to be supported.

Aaron Hall
  • 4,518
  • 1
  • 15
  • 14
  • I don't find the "how do we know this" section particularly helpful, because to me it implies that there is some sort of obvious-to-find solution, when I don't find it particularly intuitive that I would "man ssh" to find how to disconnect. How would I have found "exit", for example? I do agree it's good to reaffirm checking the man pages when you can. – Alexander Pritchard Oct 11 '17 at 15:49
  • @AlexanderPritchard I have updated my answer to more carefully explain what we're doing here. If you find the man page difficult to understand, file an issue with the maintainer, or even better, submit a pull request with an (unqualifyingly good) improvement. – Aaron Hall Oct 11 '17 at 17:01
  • 1
    That sounds good in theory, but in practice, many people struggle to figure out how to navigate these commands because even knowing how to look for the information you want assumes context people do not have. Filing an issue with maintainers and submitting pull requests with improvements goes even further beyond this. I do not argue that man pages are useful, but if they were as definitively useful as some make them seem, I wouldn't be using Stack Exchange. I want to be clear I think your answer is fine, and that people should check the man pages, but they are not all-encompassingly useful. – Alexander Pritchard Oct 12 '17 at 17:54
  • 23
    "if they were as definitively useful as some make them seem, I wouldn't be using Stack Exchange." I agree, and that's why I try to write good answers. Citing and quoting reference sources is not to make anyone feel dumb or bad for not having already read from that source, it is done to provide further evidence for what would otherwise be a perhaps demonstrable assertion of fact, as well as inform the user where more relevant information may be stored. You want to know that you're doing semantically the correct thing, as well as knowing that it works. – Aaron Hall Oct 12 '17 at 18:20
  • Sometimes I have to press `Ctrl-c` after entering `exit` , is it a normal behavior? – alper Jul 15 '20 at 13:21
  • @alper I never do that. I presume you mean you type exit and then ssh hangs. Does Enter, `~.` close ssh after you type exit? That would imply your `ssh` client is working fine, but there's something wrong with the server connection. This post might help: https://superuser.com/questions/617252/why-does-ssh-hang-at-the-end-of-these-commands-and-how-can-i-make-it-exit – Aaron Hall Jul 15 '20 at 13:31
  • Yes `~.` closes. Once a while I believe due to `X11 window forwarding` after exit cursor remains on the new line and I have to press `Ctrl-c` to complete the exit operation. Maybe I can make a alias for exit to run `exit && ctrl-c` if possible – alper Jul 15 '20 at 14:25
  • `Enter ~.` works, if the session has lost connection, but it doesn't if the session is still active. How do you enter some of these `~`-Commands, if the session is still living? – rubo77 Mar 20 '21 at 08:31
  • @rubo77 - try it - it works regardless. – Aaron Hall Mar 20 '21 at 17:35
  • ~. is good for sshd on windows :) – user1133275 May 31 '21 at 20:59
  • 4
    _in the case where you have a bad connection and the shell is unresponsive, hit the Enter key, then type ~. and ssh should immediately close and return you to your command prompt._ That comment extremely helpful for me finally "solving" the issue on mac os, rather than opening and closing a new terminal tab. Unreal that I've been doing this the slow way for many years :-) – Jason R Stevens CFA Jul 06 '21 at 13:22
  • I love that the escape code `Enter` `~` `.` or `Return` `~` `.` can be read "enter home here" or "return home now" given the typical usage of `~` and `.` on Unix-like systems. Easy to remember! – Cosmo Apr 12 '23 at 22:02
333

Short answer: Type exit

If that doesn't work, however...

SSH Escape Character and Disconnect Sequence

Most SSH implementations implement an escape character for interactive sessions, similar to telnet's Ctrl-] combination. The default SSH escape character is ~, entered at the beginning of a line.

If you want to terminate an interactive OpenSSH session which is stuck and cannot be exited by entering exit or CtrlD into a shell on the remote side, you can enter ~ followed by a dot .. To be sure to enter the escape character at the beginning of an input line, you should press Enter first. So the following sequence will in most cases terminate an SSH session:

Enter~.

Other Escape Sequences

OpenSSH, for example, offers other escape sequences besides ~.. Entering ~? during a session should give you a list. Some examples:

  • ~ followed Ctrl-Z suspends the session,
  • ~& puts it directly into background,
  • ~# gives a list of forwarded connections in this session.
  • If you want to simply enter a tilde at the beginning of a line, you have to double it: ~~.

The escape character can be changed using the command line option -e. If you set the special value -e none, escaping is disabled and the session is fully transparent.

See also the OpenBSD man page on ssh (which is referenced from www.openssh.org) under the -e command line option

Dubu
  • 3,701
  • 2
  • 15
  • 11
  • 4
    Why make it simple when you can have it uselessly complicated? – MariusMatutiae Jun 06 '14 at 12:14
  • 10
    @MariusMatutiae The OP asked for a case where there was no remote shell to enter `exit` or `Ctrl-D`, but just a listening process. I clearly stated that my solution is suited for a session _which is stuck and cannot be exited_. I tried to clarify this more, hope it is easier to see now. – Dubu Jun 10 '14 at 09:47
  • 7
    I was sceptical, but `~.` without spaces was exactly what I needed, thanks! :) – Jamey Jun 25 '14 at 14:38
  • This worked perfectly. I was thinking it might have been `Ctrl`+`^`, as that's the escape character for telnet and a couple of other things. –  Sep 19 '14 at 18:11
  • 2
    In swiss german keyboard layout, tilde is generated by pressing AltGr+^ (plus it's a blocking key). Because of this, it seems that the escape sequence doesn't work. Does anyone know how to type the default escape in swiss german? – Daniel Alder Dec 01 '14 at 12:14
  • It doesn't work on an Italian keyboard, which has no `~` key. – Wizard79 May 20 '15 at 08:59
  • @Lorenzo Does [this superuser article](http://superuser.com/questions/667622/italian-keyboard-entering-the-tilde-and-backtick-characters-without-cha) maybe help with typing a tilde? – Dubu May 20 '15 at 09:07
  • @DanielAlder Did you try entering the `AltGr`+`^`, followed by a space (and then the `.`)? Or maybe enter the combination twice in a row? – Dubu May 20 '15 at 09:09
  • @Dobu AltGr+^ is the combination for "~". The character is directly printed to the console (non-blocking keyboard). AltGr+^, then . prints "~." to the console – Daniel Alder May 20 '15 at 11:38
  • @DanielAlder Ah, you first said it was a blocking key, so I expected that pressing the combination would do nothing unless you add another key, like dead keys for accents (`\``, `´`, `^`) on a German keyboard. With those you have to enter the key twice or add a space to get the symbol itself. – Dubu May 20 '15 at 11:42
  • 1
    @Dubu I'm on another computer now. But you're right. i tested again and it works now with AltGr+^, then "." or Ctrl+Z. Not sure what I did wrong before. And I'm not sure if it would also work on a machine with blocking key – Daniel Alder May 20 '15 at 13:07
  • 4
    @Dobu Found the reason why it didn't work before. If you already typed ~ in the same line (which easily happens if you are testing), the following ~. sequences are ignored. Had to press return first... – Daniel Alder May 20 '15 at 13:16
  • 1
    Notice that if ~ is a dead key in your keyboard layout you have to type a space between ~ and . – kasperd Oct 25 '18 at 20:11
  • `exit` works but once in a while it requires me to press additional `ctrl-c`, can I prevent this and for exit? – alper Jul 15 '20 at 13:23
  • This is the first answer of this question, that mentioned the escape character. It should have been the accepted answer. Thank you! – Franklin Yu Aug 17 '22 at 18:15
34

Do you want to exit the SSH shell?

You can type exit and hit Enter, or use Ctrl+D

JoshP
  • 2,272
  • 3
  • 22
  • 28
13

Just type exit or logout (then hit Enter of course) both will work.

PHP Bugs
  • 103
  • 5
octa
  • 139
  • 1
  • 2
10

These are the supported characters which provide various options with which you can play around with ssh.

Supported escape sequences:

 ~.  - terminate session

 ~B  - send a BREAK to the remote system

 ~R  - Request rekey (SSH protocol 2 only)

 ~#  - list forwarded connections

 ~?  - this message

 ~~  - send the escape character by typing it twice

(Note that escapes are recognized only immediately after a newline.) You can close the list of Escape sequences by hitting Enter.

0_o
  • 209
  • 2
  • 3
9

MacOS: when ssh hangs use following sequence:

ENTER 
SHIFT+`
.

where: shift+` produces ~ (tilde character)

4

You can write logout in the console line (and hit Enter of course).

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
Etienne
  • 169
  • 1