Configure Git to use a .pem key from a specific location

情到浓时终转凉″ 提交于 2020-02-23 14:47:17

问题


Whenever I try to do a 'git pull origin master' I get (It is NOT Github):

Permission denied (publickey).

I am able to SSH into my AWS EC2 Linux sever, which has the bare repository, which I'm trying to pull from when I get the aforementioned permission error.

I have indeed copied the public key to that server, because I can login successfully via ssh, but only by doing a:

ssh -i /location/of/pemkey/mykey.pem ec2-user@ec2-12-34-56-78.us-east-compute.amazonaws.com

I need to configure Git to use my '.pem' key. How do I accomplish setting up Git to utilize my '.pem' key?


回答1:


From the git(1) man page:

   GIT_SSH
       If this environment variable is set then git fetch and git push
       will use this command instead of ssh when they need to connect to a
       remote system. The $GIT_SSH command will be given exactly two or
       four arguments: the username@host (or just host) from the URL and
       the shell command to execute on that remote system, optionally
       preceded by -p (literally) and the port from the URL when it
       specifies something other than the default SSH port.

       To pass options to the program that you want to list in GIT_SSH you
       will need to wrap the program and options into a shell script, then
       set GIT_SSH to refer to the shell script.

       Usually it is easier to configure any desired options through your
       personal .ssh/config file. Please consult your ssh documentation
       for further details.

In my personal experience, the one-time cost of adding host settings in .ssh/config has made a big difference, even for hosts where only the username is different.




回答2:


I got this from here, but it is not the main answer. The instructions listed here were more useful to me.

Adjust your ~/.ssh/config and add:

Host example
Hostname example.com
User myuser
IdentityFile ~/.ssh/other_id_rsa

Now use the ssh host alias as your repository:

$ git remote add origin example:repository.git

$ git pull origin master

And it should use the other_id_rsa key!




回答3:


1. touch ~/.ssh/config
2. chmod 644 ~/.ssh/config
3. vim ~/.ssh/config
    #write next codeline
    host ec2-ip.eu-west-1.compute.amazonaws.com
    IdentityFile ~/Documents/ec2-user.pem

git clone git@ec2-ip.eu-west-1.compute.amazonaws.com:/home/git/my-repo.git


来源:https://stackoverflow.com/questions/30972776/configure-git-to-use-a-pem-key-from-a-specific-location

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