【问题标题】:Deploying to ec2 instance failing when access GitHub private repo访问 GitHub 私有仓库时部署到 ec2 实例失败
【发布时间】:2014-08-16 17:59:57
【问题描述】:

我大约在一年前设置了我的 GitHub 帐户,并在当时生成了一个新的 SSH 密钥(以便从命令行轻松地从 GitHub 推送和拉取)。几天前,我启动了一个新的 ec2 实例,并下载了它给我的 .pem 文件,以便通过 SSH 连接到其中。我可以使用.pem 文件通过命令行轻松地通过 SSH 连接到服务器。

我正在使用 Capistrano 部署我的 Rails 应用程序,并运行 cap staging deploy:check。在deploy.rb 文件中,我有 private repo git url,在staging.rb 文件中,我将set ssh_options 设置为我从 ec2 实例下载的.pem 文件的位置.

当命令开始运行时,它可以正常连接到服务器,但是当它尝试访问 git repo GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/<APP_NAME>/git-ssh.sh /usr/bin/env git ls-remote -h git@github.com:<USERNAME>/<APP_NAME>.git 时,它会失败并显示以下消息:

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

一直在在线研究这个问题,从我收集的信息来看,似乎我需要为 GitHub 和 ec2 服务器使用相同的 SSH。可能是我的信息不正确……到目前为止一直没有成功。

希望对这个问题提出一些建议。感谢您的宝贵时间。

【问题讨论】:

  • 您需要将公钥注册到您的 GitHub 用户帐户。
  • 如果当前是 .pem 文件,我该怎么做?

标签: ruby-on-rails git github ssh amazon-ec2


【解决方案1】:

如果问题是注册public key to your GitHub account,您可以轻松convert a pem to an ssh key

openssl rsa -in MYFILE.pem -pubout > MYFILE.pub
ssh-keygen -f MYFILE.pub -i -m PKCS8

第一个命令在“openSSL: how to extract public key?”中。
Convert pem key to ssh-rsa format”中的第二个。

正如OP Justin 确认的那样:

我刚刚使用PuttyGen.pem 转换为GitHub 想要的格式...它以ssh-rsa 为前缀。

【讨论】:

  • 感谢您的快速回复。我使用第一个命令将.pem 文件转换为.pub 文件。即使查看文本编辑器中的每个键,我也可以看到第一个以 -----BEGIN RSA PRIVATE KEY----- 开头,在我转换为 pub 之后:-----BEGIN PUBLIC KEY-----。但是,当我运行第二个命令时,我得到了这个响应:do_convert_from_pkcs8: MY_KEY_NAME.pub is not a recognised public key format
  • @Justin 让我们在第一个命令处停下来,然后:在您的 GitHub 帐户上注册该公钥是否有助于您访问您的私人仓库?
  • 我将不得不进一步看看我做错了什么,因为使用GitHub guidelines,我在将发布密钥保存到 GitHub 时收到错误Key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key
  • 然后试试openssl rsa -noout -text -inform PEM -in key.pub -pubin,如stackoverflow.com/a/20352821/6309
  • 确实以这种格式输出它00:59:2c:36:d7.........但再次尝试将其粘贴到 GitHub SSH Keys 文本字段中会引发该错误。只想说我真的很感谢你在这方面的时间。
猜你喜欢
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 2021-11-08
  • 2020-04-27
  • 2022-06-28
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
相关资源
最近更新 更多