SSH隧道通过Ubuntu堡垒到私有子网中的实例

问题描述 投票:0回答:2

我存在问题,我无法完全得到如何解决。问题很常见,但我仍然无法得到我做错的事。

根据这个AWS文档:Scenario 2: VPC with Public and Private Subnets (NAT)我有自己的VPC有两个子网:私有和公共。在公共子网中,我已经部署了一个带有指定EIP的Ubuntu 16.04实例。它还具有下一个安全组入站规则:

Type   Protocol Port Range Source            Description
SSH    TCP      22         xx.xx.xx.xx/32    Home IP

和相应的出境:

Type   Protocol Port Range Source            Description
SSH    TCP      22         sg-xxprivatexx    Security group ID for instance in private subnet

看起来不错,我可以从我家外面ssh。没问题。

在私有子网中,我已经部署了另一台具有下一个安全组(入站规则)的Ubuntu 16.04计算机:

Type   Protocol Port Range Source            Description
HTTP   TCP      80         sg-xxpublicxxx    Security Group ID for bastion instance in public subnet
SSH    TCP      22         sg-xxpublicxxx    -

并且没有出站规则(实际上它已经打开了80,443个出站端口,但我认为它不是一个有趣的部分)。我仍然可以使用我的堡垒中的ssh来访问这个虚拟机。

现在我只想制作一个简单的东西 - 运行ssh端口转发,这样我就可以在家用PC浏览器上运行localhost:8080并查看我在私有实例上发布的网页。如果我从herehere(以及here)正确理解它,我必须运行如下:

 ssh -N -v -L 8080:10.0.1.112:80 [email protected]

我猜这基本上意味着:只需通过我的堡垒虚拟机将IP 10.0.1.112:80的私有子网实例的流量转发到我的localhost:8080,用户名ubuntu托管在EIP 3.121.46.99上。

调试以行结束:

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:ZyVHgnF8z5vE5gfNr1S2JDfjhdydZVTNevPRgJZ+sRA /home/matterai/.ssh/key.pem
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/matterai/.ssh/id_rsa
debug1: Trying private key: /home/matterai/.ssh/id_dsa
debug1: Trying private key: /home/matterai/.ssh/id_ecdsa
debug1: Trying private key: /home/matterai/.ssh/id_ed25519
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).

我玩了几天,我仍然无法得到我做错了什么。它如此奇怪:我可以ssh -A(允许转发)到我的堡垒,我可以ssh到我的私人实例从堡垒。但是我无法建立SSH隧道以查看我的网页(将来它将是mongodb)而没有错误。需要一些建议或指向正确的方向,拜托!谢谢。

UPD#1

那好吧。如果我使用本地机器和我的堡垒进行手动转发,我会得到预期的结果。基本上它意味着在堡垒上运行此命令:

ubuntu@bastion: ssh -v -N -L 5000:localhost:8000 [email protected]

之后在本地/家庭机器上运行命令:

matterai@homepc: ssh -v -N -L 5000:localhost:5000 [email protected]

当我在本地机器上向localhost:5000发出请求时,我可以看到结果页面。我可以以及如何将这两个命令结合起来? (剧透:是的,有可能:看到答案!)

amazon-web-services amazon-ec2 ssh ssh-tunnel
2个回答
1
投票

您似乎已正确配置了某些内容,但错误是说它无法找到用于连接的私钥。

要测试端口转发,请使用登录到公共实例的ssh命令开始。

然后,取出那个确切的命令,然后简单地添加:-L 8080:10.0.1.112:80

如果它适用于'普通'ssh,那么它也适用于端口转发。

顺便说一下,通常您永远不需要修改安全组的出站规则。默认设置允许所有出站流量。这“信任”在实例上运行的应用程序,并允许它们向外通信到任何地方。您只需要在希望强制实施高安全性环境的地方限制此类规则。


0
投票

好的,这很简单。希望我的回答能帮到别人。

  1. 您需要使用ssh -J选项来连接您的堡垒虚拟机:
 -J [user@]host[:port]
         Connect to the target host by first making a ssh connection to
         the jump host and then establishing a TCP forwarding to the ulti‐
         mate destination from there.  Multiple jump hops may be specified
         separated by comma characters.  This is a shortcut to specify a
         ProxyJump configuration directive.
  1. 然后,您需要使用:8000 :5001将应用程序(或数据库)启动的目标虚拟机端口(ssh)的流量转发到您的localhost端口(-L):
 -L [bind_address:]port:host:hostport
 -L [bind_address:]port:remote_socket
 -L local_socket:host:hostport
 -L local_socket:remote_socket
         Specifies that connections to the given TCP port or Unix socket
         on the local (client) host are to be forwarded to the given host
         and port, or Unix socket, on the remote side.  This works by
         allocating a socket to listen to either a TCP port on the local
         side, optionally bound to the specified bind_address, or to a
         Unix socket.  Whenever a connection is made to the local port or
         socket, the connection is forwarded over the secure channel, and
         a connection is made to either host port hostport, or the Unix
         socket remote_socket, from the remote machine.

        Port forwardings can also be specified in the configuration file.
         Only the superuser can forward privileged ports.  IPv6 addresses
         can be specified by enclosing the address in square brackets.

        By default, the local port is bound in accordance with the
         GatewayPorts setting.  However, 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.
  1. 完整的ssh命令如下所示:
matterai@homepc: ssh -v -N -A -J [email protected] -L 5001:localhost:8000 [email protected]
© www.soinside.com 2019 - 2024. All rights reserved.