2017-07-02 57 views
0

我无法推送到我的Amazon EC2实例上存储的远程git仓库。SSH可以进入服务器,但不是git push

我的本地sshconfig文件中有一个主机条目,看起来像这样:

Host ec2 
    HostName ec2-54-89-205-210.compute-1.amazonaws.com 
    User ec2-user 
    IdentityFile ~/.ssh/ec2TestB 

而且这样我就可以运行该命令ssh ec2,我会成功连接到ec2

接下来,我在名为testGitRootroot文件夹中的ec2上创建了git repo。在我的本地机器上,我创建了一个git repo,名为testGitLocal。然后我添加ec2repogit remote add origin [email protected]:/root/testGitRoot(我也试过没有[email protected]部分..不知道是否需要)。

位当我运行git push origin master我得到:

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

Please make sure you have the correct access rights 
and the repository exists. 

所以我的问题是什么概念,我错过了?如果我能通过ssh与ssh ec2进行连接,那么为什么我不能推回我的回购?有一件事情我不清楚的是,我甚至使用了ssh来完成git设置步骤。通常情况下,我总是推送到github,在这种情况下,我采取ssh网址并将其设置为我的远程。在这种情况下,我只是将我的ec2repo设置为远程,其URL只是ec2Domain:path/to/git/repo。所以即使git push origin master使用ssh,如果是的话,为什么它似乎是在ssh ec2命令不失败时公钥失效?

回答

1

您设置仓库使用用户ec2-user,如在ssh_config文件:

Host ec2 
    User ec2-user 

但你设定的遥控器来使用不同的用户名,ec2user

git remote add origin [email protected] ... 

你说ssh的作品,但混帐不。因此,git可能是用户名不正确的那个。

要解决它,只需更改远程URL,以便它有正确的用户名:

git remote set-url origin \ 
    [email protected]:/root/testGitRoot 
1

此错误消息表示您未通过身份验证。

这些是可能会导致常见的原因:

  • 试图用错键连接。你确定这个实例使用这个密钥对吗?
  • 试图用错误的用户名连接。 ubuntu是基于Ubuntu的AWS发行版的用户名,但是在某些其他版本中,它是ec2-user(或在某些Debian上为admin
  • 试图连接错误的主机。这是您尝试登录的正确主机吗?

请注意,如果您在EC2实例上弄糟了/home/<username>/.ssh/authorized_keys文件,也会发生1.。

关于2.关于您应该使用哪个用户名的信息通常缺乏AMI图片说明。但是您可以在AWS EC2文档中找到一些内容。

项目符号点4。:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

+0

我使用的(我认为)CentOS的AMI,所以我敢肯定的是,用户是'ec2user'。我不确定在'git remote add origin '中是否需要'user @'语法。基本上是''?或' @'?我也不知道哪个密钥对是正确的。我已经安装了两个git代表,并简单地将远程作为源名添加到域名中。正如我在我的问题中所说,我甚至不确定如果它使用ssh。 –

+0

'git remote add origin $ URL'。如果你使用ssh类型的URL,git将使用ssh。请参阅[git URL](https://git-scm.com/docs/git-fetch#_git_urls_a_id_urls_a)。 – phd

相关问题