通过堡垒机器的SSH隧道和端口转发会导致连接被拒绝错误

问题描述

我在服务器计算机上运行着jupyter笔记本,可以通过堡垒计算机将其切入。我想访问本地计算机上的笔记本。

enter image description here

这是在服务器计算机上运行jupyter之后尝试的方法

ssh -f -N -L local_machine_port:server_machine_IP:server_machine_port_hosting_jupyter username@bastion_machine_ip

每当我尝试访问http://localhost:local_machine_porthttp://127.0.0.1:local_machine_port时,我都会得到

debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: Connection to port 7001 forwarding to <server ip> port <server port> requested.
debug1: channel 2: new [direct-tcpip]
debug1: Connection to port 7001 forwarding to <server ip> port <server port> requested.
debug1: channel 3: new [direct-tcpip]
channel 2: open Failed: connect Failed: Connection refused
debug1: channel 2: free: direct-tcpip: listening port 7001 for <server ip> port <server port>,connect from 127.0.0.1 port 43276 to 127.0.0.1 port 7001,nchannels 4
channel 3: open Failed: connect Failed: Connection refused
debug1: channel 3: free: direct-tcpip: listening port 7001 for <server ip> port <server port>,connect from 127.0.0.1 port 43278 to 127.0.0.1 port 7001,nchannels 3
debug1: Connection to port 7001 forwarding to 192.168.2.38 port 6006 requested.
debug1: channel 2: new [direct-tcpip]
channel 2: open Failed: connect Failed: Connection refused
debug1: channel 2: free: direct-tcpip: listening port 7001 for <server ip> port <server port>,connect from 127.0.0.1 port 43280 to 127.0.0.1 port 7001,nchannels 3

我该怎么办?

解决方法

尝试将-ip 0.0.0.0 选项添加到 jupyter笔记本命令

您可以按照以下步骤操作:

 1. Run ssh on local machine:
     ssh -L local_machine_port:server_machine_IP:server_machine_port_hosting_jupyter username@bastion_machine_ip
 2. Run jupyter notebook on remote server:
     jupyter notebook --ip 0.0.0.0 --port server_machine_port_hosting_jupyter  
 3. Open browser on localmachine:
     http://localhost:local_machine_port
,

修改

对不起,我只是意识到您正在与ssh连接到堡垒机器,而不是连接到运行jupyter的远程服务器。

在这种情况下,您可能首先需要使用堡垒机器与远程服务器建立ssh连接,请参见this answer on how to do that

原始答案

您是否用计算机的外部IP地址替换命令中的server_machine_IP?即类似

ssh -f -N -L 7001:10.45.12.34:7001 username@bastion_machine_ip

如果是这样,这会将您的本地端口转发到位于其外部IP地址上的远程服务器上的指定端口。但是,如果该主机上的jupyter不在该IP上侦听-仅在本地主机上-将会拒绝连接。

如果jupyter仅在侦听本地连接,则可以尝试@Mohammed's answer让jupyter侦听外部IP。或尝试以下

ssh -f -N -L 7001:localhost:7001 username@bastion_machine_ip

并在本地计算机上打开http://localhost:local_machine_port:这将从本地计算机上的端口转发到远程计算机上localhost 上的端口,即用于jupyter on远程主机,它看起来就像是来自本地计算机(即远程服务器)的连接。