Search code examples
gitamazon-ec2permissionspermission-denied

EC2 | SSH works but GIT does not


I just set up an EC2 server and I am trying to push local files from my machine to the server with git.

On the server, I initialized a git repo inside of /home/ec2-user/:

mkdir project.git
cd project.git
git init --bare

On the client I use the following command:

git fetch origin master

Which results in the error:

Permission denied (publickey, gssapi-keyex, gssapi-with-mic).
fatal: Could not read from remote repository
Please make sure you have the correct access rights and the repository exists.

This is what my local repo's config file looks like: (url IP changed for question)

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = ec2-user@ec2-22-222-22-222.us-east-2.compute.amazonaws.com:project.git
    fetch = +refs/heads/*:refs/remotes/origin/*

This command WORKS: (I am able to ssh in)

ssh ec2-user@ec2-22-222-22-222.us-east-2.compute.amazonaws.com

Because the command above works, I would think that my PEM key is not the issue, seeing how it allows me to ssh into the server.


Solution

  • Just in case, I would start testing with the full repository path:

    git remote set-url origin ec2-user@ec2-22-222-22-222.us-east-2.compute.amazonaws.com:/home/ec2-user/project.git
    

    That would ensure that Git is looking at the folder you are trying to reach.

    The OP mentions in the chat using

    ssh -i "ec2-ohio.pem" ec2-user@ec2-22-222-22-222.us-east-2.compute.amazonaws.com
    

    That means the private key used is not the default %USERPROFILE%\.ssh\id_rsa

    For Git to use it, it needs to refer to said key through the URL, and %USERPROFILE%\.ssh\config

    First, in the config file, declare an ec2 entry (you can call it any name you want, I use one which reminds me of the remote server target)

    Host ec2
        Hostname ec2-22-222-22-222.us-east-2.compute.amazonaws.com
        User ec2-user
        IdentityFile ~/.ssh/ec2-ohio.pem
    

    Then change the remote URL to use that entry (and its associated private key)

    git remote set-url origin ec2:/home/ec2-user/project.git