简体   繁体   English

git push origin master:权限被拒绝(公钥)错误

[英]git push origin master: permission denied (public key) error

I have set up a git repo on my amazon ec2 ubuntu server instance.我已经在我的 amazon ec2 ubuntu 服务器实例上设置了一个 git repo。 I have been trying to push the code onto the server's repo from my local machine.我一直在尝试将代码从本地机器推送到服务器的存储库中。 The steps that I followed are:我遵循的步骤是:

ssh-add /path/to/myEC2publickey

On my EC2 Instance在我的 EC2 实例上

mkdir /path/my_project.git
cd /path/my_project.git
git init --bare

Later on my localhost,后来在我的本地主机上,

cd the_project 
git init git add . 
git commit -m "Initial git commit message" 
git remote add origin username@hostname.com:the_project.git 
git config --global remote.origin.receivepack "git receive-pack" 
git push origin master

Since I was getting a Permission Deined (public key) error while executing the last command (ie git push origin master), I set the public key using the steps given on a forum that included -由于我在执行最后一个命令(即 git push origin master)时遇到权限定义(公钥)错误,我使用论坛上给出的步骤设置了公钥,其中包括 -

ssh-keygen -t rsa -C "myemail@somedomain.com"
eval 'ssh-agent -s'
ssh-add

I was able to add the public key but I am still facing the Permission Denied (public key) : Error.我能够添加公钥,但我仍然面临权限被拒绝(公钥) :错误。

I'm new to git and have been looking forward to shift all my code into a git repo.我是 git 的新手,一直期待将我的所有代码转移到 git 存储库中。

Any help would be greatly appreciated.任何帮助将不胜感激。

One step you seem to have missed (or didn't include in your description) is the publication of the public key on the server side.您似乎错过(或未包含在您的描述中)的一个步骤是在服务器端发布公钥。
Upload your public ssh key and add it to the ~username/.ssh/authorized_keys file.上传您的公共 ssh 密钥并将其添加到~username/.ssh/authorized_keys文件中。

Also, try it (for testing) first with a private key without passphrase (no need to ssh-add your key to an ssh agent)此外,首先使用没有密码短语的私钥尝试(用于测试)(无需将您的密钥 ssh 添加到 ssh 代理)

Finally, make sure your ssh keys are with standards names ( id_rsa and id_rsa.pub ), with the right protection:最后,确保您的 ssh 密钥具有标准名称( id_rsaid_rsa.pub ),并具有正确的保护:

Finally, an ssh -Tvvv username@hostname.com should tell you more, if the previous steps didn't solve the issue.最后,如果前面的步骤没有解决问题, ssh -Tvvv username@hostname.com应该会告诉您更多信息。

The complete procedure being:完整的程序是:

  1. Add the EC2 public key to your ssh list with the following command使用以下命令将 EC2 公钥添加到您的 ssh 列表

    ssh-add /path/to/myEC2publickey
  2. Create a git repository on the EC2 instance with the following commands使用以下命令在 EC2 实例上创建一个 git 存储库

    mkdir /path/my_project.git cd /path/my_project.git git init --bare
  3. Connect the local files on your system to your repository with the commands使用命令将系统上的本地文件连接到存储库

    cd the_project git init git add . git commit -m "Initial git commit message" git remote add origin username@hostname.com:the_project.git git config --global remote.origin.receivepack "git receive-pack" git push origin master
  4. Create a public key as the user and add it to the server's authorized keys以用户身份创建公钥并将其添加到服务器的授权密钥中

    You can do this step by just copying the file id_rsa.pub from the localhost to the servers ~/.ssh/authorized_keys file, as suggested in the previous answer.您可以通过将文件 id_rsa.pub 从本地主机复制到服务器的 ~/.ssh/authorized_keys 文件来完成此步骤,如上一个答案中所述。

After following these steps if you try the git push, you should not get a "permission denied" error.如果您尝试使用 git push 执行这些步骤后,您应该不会收到“权限被拒绝”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM