EC2 | SSH works but GIT does not

China☆狼群 提交于 2021-02-07 10:33:20

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/62719545/ec2-ssh-works-but-git-does-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!