461

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
  • 162,382
Andreea
  • 4,721

7 Answers7

487

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
  • 5,038
344

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,821
34

Do you want to exit the SSH shell?

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

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

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

PHP Bugs
  • 103
octa
  • 139
12

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
  • 229
10

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
  • 89,072
  • 65
  • 269
  • 311
Etienne
  • 169