如何退出 SSH 连接?

And*_*eea 350 linux ssh virtualbox

我正在通过 SSH 连接到服务器,以使用如下命令向套接字服务器发送消息:

SSH 181.169.1.2 -p 5566

建立连接并编写消息并发送后,我无法退出文本模式。我只允许输入更多文本,仅此而已。

是否有命令或组合键可以让我返回命令模式?

Aar*_*all 312

如何退出 SSH 连接?

两种方式:

  • 关闭 shell 会话通常会退出,例如:
    • 使用 shell 内置命令, exit, 后跟Enter, 或
    • Ctrl- d,(文件结束
  • 如果您的连接不良并且外壳没有响应,请Enter按键,然后键入~.ssh 应立即关闭并返回到您的命令提示符。

第一个选项应该很直观,但是我们怎么知道后一个选项呢?

我们可以通过仔细阅读手册页来了解这些信息。

$ man ssh
Run Code Online (Sandbox Code Playgroud)

为我们提供了 SSH 文档,其中包含以下有关转义字符的部分:


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.
Run Code Online (Sandbox Code Playgroud)

exitssh没有什么特别之处,它只是一种退出 shell 的方式,这会导致关闭 ssh 会话:

$ 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.
Run Code Online (Sandbox Code Playgroud)

引用和引用参考来源是为了提供进一步的证据,否则可能是可证明的事实断言,并告知用户可以存储更多相关信息的位置。

你想知道你在做语义上正确的事情,以及知道它是有效的。

您不想学习将记录为错误然后“修复”的内容作为功能调用。将继续支持做语义正确的事情。

  • “如果它们确实像某些看起来那样有用,我就不会使用 Stack Exchange。” 我同意,这就是为什么我尝试写出好的答案。引用和引用参考来源并不是为了让任何人因为没有阅读过该来源而感到愚蠢或不好,这样做是为了提供进一步的证据来证明可能是可证明的事实断言,并告知用户哪里更重要可以存储相关信息。你想知道你在做语义上正确的事情,以及知道它是有效的。 (13认同)
  • _如果连接不良且 shell 无响应,请按 Enter 键,然后输入 ~。并且 ssh 应该立即关闭并返回到命令提示符。_ 该评论对我最终“解决” mac os 上的问题非常有帮助,而不是打开和关闭新的终端选项卡。难以置信的是我多年来一直以缓慢的方式这样做:-) (4认同)

Dub*_*ubu 311

简答:类型 exit

但是,如果这不起作用...

SSH 转义字符和断开连接序列

大多数 SSH 实现都为交互式会话实现了转义字符,类似于 telnet 的Ctrl-]组合。默认的 SSH 转义字符是~,在行首输入。

如果您想终止一个卡住exitCtrlD的交互式 OpenSSH 会话,并且无法通过进入进入远程端的 shell退出,您可以输入~后跟一个点.。要确保在输入行的开头输入转义字符,您应该先按 Enter。因此,在大多数情况下,以下序列将终止 SSH 会话:

Enter~.

其他转义序列

例如,OpenSSH 提供了除~.. ~?在会话期间输入应该会给你一个列表。一些例子:

  • ~随后Ctrl-Z暂停会议,
  • ~& 将其直接放入背景中,
  • ~# 给出此会话中转发的连接列表。
  • 如果您只想在一行的开头输入波浪号,则必须将其加倍:~~

可以使用命令行选项更改转义字符-e。如果您设置了特殊值-e none,转义将被禁用并且会话是完全透明的。

另请参阅命令行选项下的ssh 上的 OpenBSD 手册页(从www.openssh.org引用)-e

  • @MariusMatutiae OP 要求没有远程 shell 来输入`exit` 或`Ctrl-D`,而只是一个监听过程。我明确表示我的解决方案适用于会话_卡住且无法退出_。我试图进一步澄清这一点,希望现在更容易看到。 (9认同)
  • 我很怀疑,但是没有空格的`~.` 正是我所需要的,谢谢!:) (7认同)
  • @Dobu 找到了之前不起作用的原因。如果您已经在同一行中输入了 ~(如果您正在测试,这很容易发生),以下 ~. 序列被忽略。必须先按回车... (4认同)
  • 当你可以让它变得毫无意义的复杂时,为什么要让它变得简单? (3认同)
  • 在瑞士德语键盘布局中,波浪号是通过按 AltGr+^ 生成的(加上它是一个阻塞键)。因此,转义序列似乎不起作用。有谁知道如何在瑞士德语中输入默认转义符? (2认同)

Jer*_*ost 32

是否要退出 SSH shell?

您可以输入exit并点击Enter,或使用Ctrl+D


小智 13

只需输入exitlogout(当然然后按 Enter)都可以。


小智 8

这些是受支持的字符,它们提供了您可以使用 ssh 的各种选项。

支持的转义序列:

 ~.  - 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
Run Code Online (Sandbox Code Playgroud)

(请注意,仅在换行符之后立即识别转义符。)您可以通过点击 关闭转义序列列表Enter


Kam*_*ski 5

MacOS:当 ssh 挂起时,请使用以下顺序:

ENTER 
SHIFT+`
.
Run Code Online (Sandbox Code Playgroud)

其中: shift+` 产生~(波浪号字符)


归档时间:

查看次数:

957720 次

最近记录:

5 年,4 月 前