Git 克隆 EC2 实例权限错误

cph*_*ill 4 git ssh github amazon-ec2

我试图将我的私有 github 存储库克隆到我的 EC2 实例,但看来我可能采取了错误的步骤来允许 Github 连接到 EC2 实例。在git clone git@github.com:username/repo.git我在路径中创建的文件夹中运行/var/www/app时,我被告知要验证 Githbuc.com 主机并提供​​两个 RSA 密钥指纹(我假设是我的存储库中的指纹),然后向我发送一个错误:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

在运行命令之前,git clone我将在我的 SSH 密钥中找到的 SSH 密钥复制.pem到我的 Github SSH 密钥。我使用这些 EC2 凭证通过 SSH 连接到实例。这是使用错误的密钥吗?我知道 EC2 实例也有公钥.ssh/authorized_keys,但我觉得这些与用于通过 SSH 连接到实例的公钥相同。

我是不是少了一步?我需要在/var/www/app目录中初始化 git 或在git clone命令之前配置任何内容吗?

任何帮助都会很棒。谢谢你!

Yaf*_*ang 5

确保您在本地计算机 ~/.ssh/ 上生成 ssh 密钥对,并且您的公钥已添加到您的 GitHub 帐户中。

\n\n

要将 ssh 密钥对从本地目录克隆到 EC2 实例,请执行以下操作:

\n\n

将您的公钥克隆到您的 EC2 实例:

\n\n
$ cat ~/.ssh/id_rsa.pub | ssh -i ~/.ssh/your_pem.pem ubuntu@your_dns "cat >> .ssh/authorized_keys"\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果 \xe2\x80\x9c.ssh/authorized_keys" 不存在\xe2\x80\x99,请登录到您的 EC2 实例并创建目录:

\n\n
$ ssh -i ~/.ssh/***.pem ubuntu@[Your Public DNS]\n$ mkdir ~/.ssh\n$ chmod 700 ~/.ssh\n$ touch ~/.ssh/authorized_keys\n$ chmod 600 ~/.ssh/authorized_keys\n$ exit  # exit EC2 terminal, this will not stop your instance\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后继续将我们的 GitHub 私钥安全复制到 EC2 实例。\xe2\x80\x9cUserKnownHostsFile\xe2\x80\x9d 和 \xe2\x80\x9cStrictHostKeyChecking\xe2\x80\x9d 选项删除主机验证提示(计算机要求您键入 yes 或 no 才能继续)。如果我们想在脚本中运行这些操作,则必须删除它。

\n\n
$ scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ~/.ssh/your_pem.pem ~/.ssh/id_rsa ubuntu@your_dns:~/.ssh/\n
Run Code Online (Sandbox Code Playgroud)\n\n

您应该能够使用 ssh 密钥对连接到 git。

\n\n

如果无法修复,请参阅“使用 Amazon EC2 在 GitHub 存储库中运行脚本”的更完整教程:

\n\n

https://github.com/yafangy/Tutorial-using-Amazon-AWS-EC2-run-scripts-GitHub/tree/master#method-2-clone-git-to-ec2-instance-recommended

\n