SSH -L 隧道不再工作

Rec*_*cct 1 ssh ssh-tunneling

运行我的脚本的本地机器需要访问远程服务器的数据库。上周我可以在本地机器 ssh -f -N -T -L1081:localhost:3306 ueser@remoteserver -p2222 上的本地机器上做:

# netstat -an | egrep "tcp.*:1081.*"
tcp        0      0 127.0.0.1:1081          0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:1081                :::*                    LISTEN
Run Code Online (Sandbox Code Playgroud)

然后告诉我的脚本使用端口 1081 进行 mysql 连接工作。这个 netstat 命令显示我在本地接受连接正常,但在远程机器上的相同检查与实际的 mysql 服务器根本没有在 1081 上侦听,以前不是这种情况

我检查了 sshd_config 似乎允许隧道,这不是配置更改。我还尝试打开到我网络上另一台服务器的隧道,但这也不起作用,是我的命令废话还是什么?尝试了各种组合 -f -T

eri*_*rik 19

我画了一些草图

键入 ssh 隧道命令的机器称为“您的主机”

从本地开始的 ssh 隧道


从远程开始的 ssh 隧道

介绍

  1. 当地的: -L Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.

    ssh -L sourcePort:forwardToHost:onPort connectToHost意思是:用 ssh 连接到connectToHost,并将所有连接尝试转发到机器上称为的本地 sourcePort端口onPort,该端口forwardToHost可以从connectToHost机器访问。

  2. 偏僻的: -R Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.

    ssh -R sourcePort:forwardToHost:onPort connectToHost表示:使用 ssh 连接到connectToHost,并将所有连接尝试转发到名为 的机器上的远程 sourcePort端口onPort,该端口forwardToHost可以从您的本地机器访问。

其他选项

  • -f 告诉 ssh 在它进行身份验证后将其自身作为后台,因此您不必坐在远程服务器上运行某些东西以使隧道保持活动状态。
  • -N表示您需要 SSH 连接,但实际上并不想运行任何远程命令。如果您创建的只是一个隧道,那么包含此选项可以节省资源。
  • -T 禁用伪 tty 分配,这是合适的,因为您没有尝试创建交互式 shell。

你的榜样

第一张图片代表您的情况。如果你这样做

ssh -L 1081:localhost:3306 remotehost
Run Code Online (Sandbox Code Playgroud)

所有到绿色端口的连接尝试1081都通过 ssh 隧道转发到3306远程主机本地主机上的粉红色端口,即远程主机本身。

现在您的 php 脚本可以通过localhost:1081. 但是netstat你的命令remotehost在 1081 上找不到任何监听(至少不是因为隧道)。因为粉色端口不是ssh创建的监听端口,而是转发的目标。它不是转发到端口1081而是转发到3306.

你的数据库服务器真的在监听端口3306还是可能监听端口1081?如果后者为真,则您的命令应更改为如下所示:

ssh -L 1081:localhost:1081 remotehost
Run Code Online (Sandbox Code Playgroud)

如果您的数据库侦听端口,1081您应该使用 netstat(独立于 ssh)找到它。