ssh隧道解释

waw*_*235 8 ssh xforwarding ssh-tunneling x11

我有一个关于 ssh 隧道的问题。我读过这篇文章

我想让 X 转发工作并在家里运行一些 X 应用程序并将它们显示在远程系统上:

 ssh -X -R 5555:localhost:22 user@remoteserver.com -N
Run Code Online (Sandbox Code Playgroud)

在远程:

 ssh -X -p 5555 user@192.168.1.2
Run Code Online (Sandbox Code Playgroud)

然后在家里:

 //configure sshd to listen on 5555
 ssh user@remoteserver.com
 //here run some app
Run Code Online (Sandbox Code Playgroud)

它应该工作吗?

eri*_*rik 9

我画了一些草图

键入 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可以从您的本地机器访问。

你的榜样

好吧,如果您只想使 X 转发工作,即在家中的计算机上运行一些 X 应用程序并将它们显示在远程系统上(我们称之为工作计算机,因为它可能在您的工作场所),那么您可能根本不需要 ssh 隧道。

没有隧道的情况下启动 X 应用程序

您可以简单地从工作计算机 ssh 到您的家用计算机吗?如果是这样,当您坐在工作计算机上并想要启动在您的家用计算机运行在您的工作计算机上显示的 X 应用程序时,您必须(在工作计算机上)键入:

ssh -X homeuser@homecomputer firefox

这将在您的家用计算机上启动 firefox 并将其显示在您键入此命令的计算机上,例如您的工作计算机。

隐藏的计算机需要隧道

这是我草图的第 3图像。很多时候,家庭计算机无法直接从 Internet 访问,因为它位于防火墙后面或通过NAT(从路由器)隐藏。然后就可以使用隧道了。

在您的蓝色家用电脑 ( yourhost) 上键入:

ssh -R 5555:localhost:22 remoteuser@remotehost
Run Code Online (Sandbox Code Playgroud)

图中5555绿色端口在哪里22,粉红色端口在哪里。

如果您现在正在工作,在remotehost,并连接到绿色端口5555,您的连接将通过隧道/转发到您家用计算机的粉红色端口localhost(即您的蓝色家用计算机本身)。现在你必须在你的工作电脑上输入:

ssh -X -p 5555 homeuser@localhost firefox
Run Code Online (Sandbox Code Playgroud)

这将在您的家用计算机 ( yourhost)上启动 Firefox并将其显示在您键入此命令的计算机上,例如您的工作计算机 ( remotehost)。


Sté*_*las 1

您需要指定要转发的远程显示。在远程:

DISPLAY=:0 ssh -X -p 5555 user@localhost # not sure why you used 192.168.1.2
Run Code Online (Sandbox Code Playgroud)

转发:0显示。

然后在该 shell 中(现在在您的家用计算机上运行)运行:

echo "$DISPLAY"
Run Code Online (Sandbox Code Playgroud)

找出转发的显示内容。这将类似于localhost:10,这意味着您需要在端口 6010 上进行 TCP 连接以连接到:0远程计算机上的显示器(:0意味着连接到某些 Unix 域套接字,例如/tmp/.X11-unix) 。

然后,要使计算机上的应用程序显示在远程服务器的显示屏上,只需告诉他们使用localhost:10

DISPLAY=localhost:10 xlogo
Run Code Online (Sandbox Code Playgroud)

例如。

请注意,这是隧道之上的隧道。