Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

git - Permission denied (publickey) when pushing commit to amazon ec2

I know that a lot of similar questions exist here (and I read a lot of them before posting this one), but this one is different, so please stay with me for a while.

Few days ago I configured my remote git repository on amazon ec2. After hours of struggling I made it working and was able to push files there. I was working with it for a day or two pushing resources to it without any problems (I saw every update properly).

Today, for some reason I can not push anything to it from my local machine.

git push deploy ends up with the following error message (the same one I see when I try to get info about deploy: git remote show deploy):

warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

The first part of it (before permission denied) I was able to see previously, but it was still pushing everything correctly back then.

Deploy exist because I can clearly see it with git remote -v:

deploy  ssh://[email protected]/home/ubuntu/repo (fetch)
deploy  ssh://[email protected]/home/ubuntu/repo (push)

I can correctly ssh to my server, also my private key has 0400 permissions. On both machines I have ubuntu 12.04 LTS.

I thought that may be there is some problem with my public key, so I went ahead and recreate it from private key: ssh-keygen -y -f key.pem > key.pub. Based on its sha it is identical to my previous public key: sha1sum key_prev.pub = sha1sum key.pub

I still went to the server and added a new key to authorized keys as well.

Still no changes. I can not push anything. Can anyone tell me what is the problem here and why it happened? Do I need to do anything with my local .ssh/known_hosts?

Also in one of the answers I found the following:

Please note that after restarting the instance, the dns name changed. I fell for this several times. The keyfile was still valid, but the "servername" changed.

I actually restarted my machine, so I think this is highly relevant. The problem is that I can not understand what should I change now.

After reading the answer of VonC.

I can not ssh to the server by doing ssh [email protected] (I got Permission denied (publickey).), but I can do this with ssh myAlias, where myAlias is defined in ~/.ssh/config

Host            myAlias
Hostname        X.X.X.X
User            ubuntu
IdentityFile    path/to/mypem/file.pem

When I have done git config --global push.default simple I have another problem when doing git push deploy:

fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream deploy master

I will try to do ssh -Tvv [email protected] and will tell how it goes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, do yourself a favor, and type git config --global push.default simple in order to not see the first part of the error.

Second, if you can login successfully with ssh [email protected], then your keys are fine, as well as the instance name.

If you can not, you can:

  • try a ssh -Tvv [email protected] to see what is going one, and
  • verify that by going to the server, stopping sshd and restarting it in debug mode (/usr/sbin/sshd -d): it will receive only one ssh connection: try a ssh [email protected] from your client, and you should see the sshd response.
    Then restart sshd normally.

In your case, you have to use your alias, defined in ~/.ssh/config, since your key isn't a standard one.

For the push to go smoothly, do a git push -u deploy master.

Since git remote -v returns:

deploy  ssh://[email protected]/home/ubuntu/repo (fetch)
deploy  ssh://[email protected]/home/ubuntu/repo (push)

That means you are not using the ssh alias.

Type:

git remote set-url deploy myAlias:/home/ubuntu/repo

(The syntax is important: myAlias: followed by the repo path)

And then try again the git push.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...