无法使用SSH访问AWS CodeCommit

25

在使用标准SSH身份验证时,我非常难以弄清楚如何让AWS CodeCommit正常工作。看到过另一个类似的主题,但没有答案,而且我现在还不能评论。这是在Windows上使用Git Bash。

复制步骤

  1. 创建具有完整权限的IAM用户(AwsAdministrator)
  2. 在Git Bash中的~/.ssh
  3. "cat id_rsa.pub"并将输出复制到剪贴板中
  4. 在IAM控制台中,单击添加SSH密钥按钮,并粘贴到输入字段中。然后,单击保存。
  5. 尝试访问CodeCommit repo(在这种情况下是尝试推送),然后收到“权限被拒绝”的错误消息。

Git + SSH 输出

这是使用DEBUG3日志记录的SSH输出:

debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/Dan/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug2: input_userauth_pk_ok: fp SHA256:<omitted>
debug3: sign_and_send_pubkey: RSA SHA256:<same-as-above>
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).
fatal: Could not read from remote repository.

相比之下,我在使用完全相同的SSH密钥登录GitHub时所得到的结果如下:

debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/Dan/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug2: input_userauth_pk_ok: fp SHA256:<same-as-above>
debug3: sign_and_send_pubkey: RSA SHA256:<same-as-above>
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.252.130]:22).

以上输出是通过在.ssh/config中启用ssh调试日志记录,运行诸如git push origin master之类的常规git命令得出的:
Host git-codecommit.us-east-1.amazonaws.com
  LogLevel DEBUG3

Host github.com
  LogLevel DEBUG3

你正在使用哪个命令获取这个日志?在服务器错误日志中,你能看到什么? - Jakuje
@Jakuje 更新了描述,说明了我运行命令的方式来获取日志。不知道如何查看AWS和GitHub的服务器日志? - Dan
9个回答

29

看起来您在设置SSH时错过了一步。 您需要将此信息添加到您的 .ssh/config 文件中:

Host git-codecommit.us-east-1.amazonaws.com
   User Your-SSH-Key-ID, such as APKAEIBAERJR2EXAMPLE
   IdentityFile Your-Private-Key-File, such as ~/.ssh/codecommit_rsa or ~/.ssh/id_rsa

您可以从IAM控制台获取您的SSH密钥ID。

2
确实是这个问题,谢谢!如果我看了Linux/Mac步骤,而不是试图翻译Windows步骤,那么这个问题会更加明显。 - Dan
我之前依赖于由Sourcetree创建的配置文件,但它并没有起作用。当我将我的配置文件更改为这个后,它就可以工作了。 - Brian McCall

7
尝试在执行git clone时使用sshkey-ID。 enter image description here 因此,git clone应该像这样:
git clone ssh://<SSHKEY-ID>@git-codecommit.<REGION>.amazonaws.com/v1/repos/<REPO-NAME>

你的解决方案对我起作用了!!!!!!! - Kunal Mahajan

6

我的情况有点不同。

“安全凭证”选项卡中既有“访问密钥”也有“AWS CodeCommit的SSH密钥”。请确保您正在使用CodeCommit密钥ID。 enter image description here


我正确上传了密钥,但当我去复制ID时,我抓错了一个,谢谢你添加这个,否则我会疯掉的。 - Richard Dunn
1
我不小心拿了 Access Key ID(附上图片以便说明),浪费了很多时间试图弄清楚为什么 git clone ssh://Your-SSH-Key-ID@... 也无法工作。 - dejanualex

5

输入图像描述这行代码可用

Host git-codecommit.us-west-2.amazonaws.com
    User SSHKEYID
    IdentityFile ~/.ssh/id_rsa

你是个英雄兄弟 :) - Muhammadsiddiq

3
请按照AWS提供的以下文档进行操作。 Linux
https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html

Windows

https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-windows.html

我已经测试了仅限于 Windows 的 SSH 连接设置,请通过上述链接按照步骤测试 Linux 上的 SSH 连接;

以下是在 Windows 10 上设置 CodeCommit SSH 连接的步骤。

第一步

CodeCommit 初始配置(请参见文档链接)

第二步

安装 Git(请参见文档链接)

第三步

运行以下命令生成 SSH 密钥:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

运行该命令后,应在 C:\Users{用户名}.ssh 目录下生成公钥(.pub)和私钥。
第4步:
登录 AWS 管理控制台并转到 IAM-->用户-->选择目标用户-->安全凭据选项卡-->AWS CodeCommit 的 SSH 密钥,然后单击上传 SSH 密钥。复制粘贴公钥的内容,例如 C:\Users{用户名}.ssh\id_rsa.pub,并保存更改。但是,您可以在使用命令 ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 时为您的公钥(id_rsa.pub)选择不同的名称。
第5步:
上传公钥后,复制 SSH 密钥 ID。
第6步:
创建 C:\Users{用户名}.ssh\config 文件(无文件扩展名,在 Windows 中将其保存为 config 并选择所有文件类型选项。Config.txt 可能不起作用)。将以下内容粘贴到 C:\Users{用户名}.ssh\config 文件中:
Host git-codecommit.*.amazonaws.com
  User {copy SSH key ID here}
  IdentityFile ~/.ssh/{copy name of the public key here }
  
Host git-codecommit.*.amazonaws.com
  User THISISEXMAPLE123
  IdentityFile ~/.ssh/id_rsa
  

第七步

  git clone ssh://Your-SSH-Key-ID@git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo my-demo-repo

1

请确保配置文件不要保存为txt文件,并使用SSH密钥ID,而不是访问密钥。


0

AWS核心配置的更改是多种多样的。即使您可能正确地获取了所有配置,仍然可能出现连接问题。要直接明确。

enter image description here

尝试将默认的git url origin从旧的更改为<KEY_ID>@git-codecomitxxxxxxxx/v1/repos/xxxxx,确保该密钥作为存储库的用户所有者

然后再次推送。


0
你遇到的问题是,在 AWS 文档的第 8 至 9 步期间,Windows 自动将配置文件保存为 config.txt。

https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-windows.html?icmpid=docs_acc_console_connect_np#setting-up-ssh-windows-keys-windows

您可以按照以下说明来解决这个问题

  1. 打开cmd命令行
  2. cd Users\Mr.S>cd C:\Users\Mr.S.ssh (在这里,您正在导航到.config.txt文件所在的.ssh目录)
  3. 输入copy config.txt config (通过使用复制功能,您正在将config.txt文件转换为config文件)

完成上述步骤后,请继续进行下一步。


0
在我的情况下,我将~/.ssh/config配置如下。
Host remote
  HostName  git-codecommit.us-east-1.amazonaws.com
  User APLAY5AJ2VA0XEXAMPLE
  IdentityFile ~/.ssh/id_rsa

我尝试使用命令进行克隆

git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/repo-name

导致权限被拒绝。

但是通过运行以下命令完成了任务。

git clone ssh://remote/v1/repos/repo-name

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