Linux-将端口上的 SSH 重定向到本地网络上的另一台机器

Linux-将端口上的 SSH 重定向到本地网络上的另一台机器

我在虚拟网络上有一台 KVM 客户机。它的 IP 地址为192.168.122.65。主机连接到公共互联网。如果我在主机上打开终端,我可以通过输入 SSH 进入客户机,所以我知道它工作正常。ssh [email protected]

现在我想通过互联网从任何计算机 ssh 进入客户机。我该如何配置主机才能实现这一点?我尝试了很多规则,iptables但到目前为止都没有用。

更新:到目前为止,这个解决方案似乎有效,但我还没有尝试重新启动服务器:https://serverfault.com/a/170641/264678

答案1

如果主机也运行 SSH 服务器软件,并且可以从 Internet 访问,则可以使用该命令。该选项的使用方式如下:ssh -L 22065:127.0.0.1:22 [email protected]-L

 -L [bind_address:]port:host:hostport
             Specifies that the given port on the local (client) host is to be
             forwarded to the given host and port on the remote side.  This
             works by allocating a socket to listen to port on the local side,
             optionally bound to the specified bind_address.  Whenever a con‐
             nection is made to this port, the connection is forwarded over
             the secure channel, and a connection is made to host port
             hostport from the remote machine.  Port forwardings can also be
             specified in the configuration file.  IPv6 addresses can be spec‐
             ified by enclosing the address in square brackets.  Only the
             superuser can forward privileged ports.  By default, the local
             port is bound in accordance with the GatewayPorts setting.  How‐
             ever, an explicit bind_address may be used to bind the connection
             to a specific address.  The bind_address of “localhost” indicates
             that the listening port be bound for local use only, while an
             empty address or ‘*’ indicates that the port should be available
             from all interfaces.

假设主机的 IP 地址为 10.0.0.5。从 Internet 上的系统,您可以使用命令 建立与主机上端口 22065 的连接。注意:即使您使用的是主机的公共 IP 地址(我使用私有 IP 地址范围 10.xxx 中的地址,仅用于示例),您仍需要为客户系统提供有效的用户 ID 和密码,因为连接实际上将通过隧道传输到客户机。ssh -p 22065 [email protected]

当连接到主​​机上的 22065 端口时,它将通过SSH 隧道由您从主机到位于 192.168.122.65 的客户系统的第一个 SSH 连接建立。就 Internet 上的 SSH 客户端而言,它将直接通过端口 22065 连接到客户系统,尽管它实际上是通过来自 10.0.0.5 主机的 SSH 隧道连接到该系统上的端口 22。注意:要使其正常工作,从主机到客户系统的 SSH 连接必须当时处于开启状态。

我放置的位置127.0.0.1你也可以放置192.168.122.65,但由于连接是本地主机地址,即客户机本身,而不是外部的某个系统,我会使用127.0.0.1

对于所需的防火墙规则,您需要在主机上以及主机与 Internet 之间的任何防火墙上制定一条规则,允许传入 TCP 连接到端口 22065,或您选择的任何端口。您不需要在客户系统上制定任何其他防火墙规则。

答案2

首先,也许不要通过 ssh 进入虚拟机?
而是进入tmuxKVM 主机并连接到该主机,在那里您可以根据需要打开任意数量的 ssh 会话。

告诉我这是否听起来更接近您想要的,我会详细说明。

快速而肮脏:

  • 使用 设置反向隧道ssh -R,在 KVM 主机上运行它,配置您的网关(面向 Internet 的路由器)以:22在您在反向隧道中设置的端口转发或 DMZ 到 KVM 主机。

永久且自给自足:

  • 使用类似的虚拟网络设备桥接两个网络tun,并以类似方式设置网关。

简单的:

  • 有 SaaS 服务可以为您实现这一点。许多服务都有免费套餐,或许可以满足您的使用情况。

相关内容