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).

  • setup AWSDevTools-RepositorySetup.sh in git repository on ubuntu
  • Startup script on EC2
  • Problems with git submodules when submodules are private Github repos
  • Cannot clone using deploy key (bitbucket) EC2
  • Unable to load AWS credentials from any provider in the chain - Jenkins & AWS codepipeline
  • how to deploy my ruby on rails app with git in my ec2 instance?
  • 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://ubuntu@X.X.X.X/home/ubuntu/repo (fetch)
    deploy  ssh://ubuntu@X.X.X.X/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 ubuntu@X.X.X.X (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 ubuntu@X.X.X.X and will tell how it goes.

  • notepad++ not working in Git Bash
  • Set SSH Identities In Bashrc
  • git rebase resolve conflicts again and again?
  • Deploy to a machine in the network using git
  • how can I avoid weird git diffs like this?
  • How to fix case sensitivity in Windows Git repository
  • One Solution collect form web for “Permission denied (publickey) when pushing commit to amazon ec2”

    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 ubuntu@X.X.X.X, then your keys are fine, as well as the instance name.

    If you can not, you can:

    • try a ssh -Tvv ubuntu@X.X.X.X 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 ubuntu@X.X.X.X 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://ubuntu@X.X.X.X/home/ubuntu/repo (fetch)
    deploy  ssh://ubuntu@X.X.X.X/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.

    Git Baby is a git and github fan, let's start git clone.