将端口转发添加到正在运行的 SSH 会话

use*_*408 25 openssh port-forwarding

OpenSSH 客户端有一个用于端口转发的命令行选项,使用方式如下:

\n
ssh -L localport:server:serverport user@host\n
Run Code Online (Sandbox Code Playgroud)\n

它将连接到hostas user,同时localport在客户端上重定向到serverporton server(可以是hosthost通过网络可访问的任何内容)。

\n

现在假设我已经通过 SSH 进行了host操作

\n
ssh user@host\n
Run Code Online (Sandbox Code Playgroud)\n

在会话进行到一半时,我意识到我忘记转发端口。唉,我正在做某事,所以我不想 \xe2\x80\x99t 只是想注销并通过端口转发重新建立 SSH 连接。

\n

有没有办法将端口转发添加到正在运行的 SSH 会话?

\n

Kam*_*ski 49

From man 1 ssh:

\n
\n

ESCAPE CHARACTERS

\n

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

\n

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.

\n

The supported escapes (assuming the default ~) are:

\n

[\xe2\x80\xa6]

\n

~C
\nOpen command line. Currently this allows the addition of port forwardings using the -L, -R and -D options (see above). [\xe2\x80\xa6] Basic help is available, using the -h option.

\n
\n

So type Enter~C (i.e. capital c), then

\n
-L localport:server:serverport\n
Run Code Online (Sandbox Code Playgroud)\n

with desired localport, server and serverport, finally Enter.

\n

Notes:

\n
    \n
  • The initial Enter will be immediately sent to the remote side and may cause some action there, so pick a good moment (e.g. when you\'re in a shell with an empty command line). Or if you are sure the last thing you have typed is Enter anyway (e.g. you have just invoked a command that is now running), you can start directly with ~ because Enter has already been noticed by your local ssh.

    \n
  • \n
  • On internationalized keyboards the tilde could be a dead key for generating special \'tilded\' characters (like pressing ~n to generate \xc3\xb1). In that case, it could be necessary to press SPACE after ~ to generate a single tilde, i.e: ENTER~SPACEC. In the case of the Spanish/LA keyboard layouts, as there is no combined character using tilde and C, the space can be omitted and the ~C generates the desired sequence.

    \n
  • \n
  • 对于多个重定向,ssh转义的命令行仅接受单个命令。您应该再次按键盘序列以输入另一个命令或重定向。

    \n
  • \n
\n