AWS SSM交互式会话的代理命令

4
目标:使用SSM登录到EC2实例并通过代理命令连接SSH终端。
我可以使用'aws ssm start-session --target instance-id'成功登录到我的实例。然而,为了让生活更简单,我想在我的.ssh/config中使用代理命令,以便能够使用更短的命令'ssh i-awsguid'登录到实例,而代理命令将填补其余部分。
然而,当我这样做时,会出现一个会话挂起的情况,从未看到远程机器的提示符,当按下control-c时,我会看到AWS操作的JSON输出。
$ ssh i-076efab920fd7a5e2 -v
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/user/.ssh/config
debug1: /Users/user/.ssh/config line 49: Applying options for *
debug1: /Users/user/.ssh/config line 70: Applying options for *
debug1: /Users/user/.ssh/config line 86: Applying options for i-*
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Executing proxy command: exec aws ssm start-session --target i-076efab920fd7a5e2
debug1: identity file /Users/user/.ssh/id_rsa type 0
debug1: identity file /Users/user/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: kex_exchange_identification: banner line 0: 
debug1: kex_exchange_identification: banner line 1: Starting session with SessionId: user@email.com-0a7ca620b0b70ba7c
debug1: kex_exchange_identification: banner line 2: This session is encrypted using AWS KMS.
debug1: kex_exchange_identification: banner line 3: echo Connected to $(hostname)
debug1: kex_exchange_identification: banner line 4: $ Connected to ip-10-147-34-181
^C

Command '['session-manager-plugin', '{"SessionId": "user@email.com-0a7ca620b0b70ba7c", "TokenValue": "scrubbed", "StreamUrl": "wss://ssmmessages.us-east-1.amazonaws.com/v1/data-channel/user@email.com-0a7ca620b0b70ba7c?role=publish_subscribe", "ResponseMetadata": {"RequestId": "cd75db36-3cc0-415f-b849-b5995e1701ec", "HTTPStatusCode": 200, "HTTPHeaders": {"server": "Server", "date": "Wed, 07 Apr 2021 17:49:22 GMT", "content-type": "application/x-amz-json-1.1", "content-length": "678", "connection": "keep-alive", "x-amzn-requestid": "cd75db36-3cc0-415f-b849-b5995e1701ec"}, "RetryAttempts": 0}}', 'us-east-1', 'StartSession', '', '{"Target": "i-076efab920fd7a5e2"}', 'https://ssm.us-east-1.amazonaws.com']' died with <Signals.SIGPIPE: 13>.

例如,在shell中直接调用同一实例。

$ aws ssm start-session --target i-076efab920fd7a5e2

Starting session with SessionId: user@email.com-0bcfc5bf3d8325cb1
This session is encrypted using AWS KMS.
echo Connected to $(hostname)
$ Connected to ip-10-147-34-181
$ whoami
ssm-user
$ exit


Exiting session with sessionId: user@email.com-0bcfc5bf3d8325cb1.

.ssh/config的相关部分

# SSH over Session Manager
host i-*
    ProxyCommand aws ssm start-session --target %h

我认为我遇到了与@gregs类似的问题,你是否找到了解决这个问题的方法? - PicoutputCls
1个回答

0

我认为问题在于您的代理命令是aws cli。JSON输出确实表明命令成功执行。但是您没有请求一个shell,您只是要求执行一个aws命令。

根据AWS关于通过SSM进行SSH会话的文档,您需要将aws命令作为参数传递给一个shell:

  • 对于MacOS和Linux,使用sh(我假设MacOS中存在sh
  • 对于Windows,使用Windows PowerShell(C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe),尽管我猜也可以在命令行中运行。

此外,我只有通过以下方式才能使其工作:

  • 在我的个人电脑上拥有 SSH 私钥
  • 在我的虚拟机的 ~/.ssh/config/authorized_keys 中拥有我的 SSH 公钥
  • host 配置中指定我的虚拟机的用户名
  • 在代理命令 AWS-StartSSHSession 中指定 AWS 文档
  • 传递端口号变量 %p

需要使用我的用户名和 SSH 密钥可能是因为我的 IAM 帐户没有正确的权限,所以这不是一个完美的解决方案。(但我无法控制工作场所的权限。)

根据调试信息判断,您是在 Mac 上,对吗?因此您的 .ssh/config 主机应该是:

# SSH over Session Manager
# you may need the region as a parameter to aws, e.g. --region us-east-1
host i-*
    User YourUsername
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

在我的情况下,在PowerShell中,由于我必须使用许多AWS帐户,因此我在代理命令中指定正确的AWS配置文件:
# SSH over Session Manager on a specific hostname
# using PowerShell and a specific AWS profile
Host myVM
  HostName i-xxxxxxxxxxx
  User myusername
  ProxyCommand C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "aws ssm start-session --profile myAwsProfile --target %h --document-name AWS-StartSSHSession --parameters portNumber=%p"

这对我没有用。通过AWS SSM连接的重点不是使用SSH,因此不使用SSH端口(因为它可能被阻止,并且仅通过堡垒机可用),也不使用公钥/私钥。然而,您在您的答案中提到了这一点-不合理,并且您的解决方案无法正常工作。 - user2965205
既定目标是“通过SSM登录到EC2实例,使用proxycommand连接到ssh终端”。我的理解是ProxyCommand的目的是将连接委托给其他进程而不是SSH。这正是原帖作者试图做的事情,使用“ssh”命令。这也正是我在工作中被迫做的事情,因为需要AWS SSO并且SSH端口被阻止。我使用上面的ssh配置来登录不公开其VPC之外IP的EC2实例。就像OP所要求的那样。然而,仍需要使用ssh进行EC2实例的身份验证。 - SylvainC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接