使用 SSH 隧道通过堡垒访问 EC2 上的 Jupyter 服务器 运行

Accessing Jupyter Server Running on EC2 Through Bastion with SSH Tunnel

所以我有一个只能通过堡垒访问的 ec2 实例。

ec2 实例在 127.0.0.1:8888/?token=$token

上为我的 jupyter 服务器提供服务

我的目标是 运行 一个 ssh 隧道命令,它将侦听 127.0.0.1:8888 上的连接,并通过堡垒将它们转发到我的 ec2 实例到 127.0.0.1:8888

我已经尝试了以下但没有成功。

来自本地:

(我可以毫无问题地通过 bastion ssh 进入 bastion 和 ec2 机器)

ssh -f -N -L 127.0.0.1:8888:127.0.0.1:8888 -i ~/.ssh/id_rsa $user@$bastion_dns

ssh -f -N -L 8888:127.0.0.1:8888 -i ~/.ssh/id_rsa $user@$bastion_dns

ssh -f -N -L 8888:$ec2_private_ip:8888 -i ~/.ssh/id_rsa $user@$bastion_dns

来自堡垒:

(我在 bastion 安全组上打开了 8888 ingress 并将 bastion ssh 密钥添加到 ec2-machine 以便我可以定期从 bastion ssh 到 ec2)

ssh -f -N -L 8888:127.0.0.1:8888 $user@$ec2_private_ip

使用-L时,您可以指定接收机器将流量发送到哪里。

假设您有:

  • 本地计算机
  • 堡垒
  • Jupyter 服务器

因此,您可以运行这样的命令:

ssh -i key.pem -L 8888:jupyter-server:8888 ec2-user@bastion-IP

这会将本地计算机上的 localhost:8888 转发到 Bastion 服务器。

然后堡垒服务器会将请求转发到 VPC 内的 jupyter-server:8888

在这个

的帮助下解决了这个问题

命令是:

ssh -v -N -A -J $user@$bastion_dns -L 8888:localhost:8888 $user@$ec2_private_ip