配置 Git 以使用来自特定位置的 .pem 密钥

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

每当我尝试做一个 'git pull origin master' 我得到(它不是 Github):

Permission denied (publickey).

我可以通过 SSH 连接到我的 AWS EC2 Linux 服务器,它有裸存储库,当我收到上述权限错误时,我正试图从中提取。

我确实已经将 public 密钥复制到该服务器,因为我可以通过 ssh 成功登录,但只能执行以下操作:

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

我需要配置 Git 以使用我的“.pem”密钥。 如何完成设置 Git 以使用我的“.pem”密钥?

来自 git(1) 手册页:

   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.

根据我的个人经验,在 .ssh/config 中添加主机设置的一次性成本产生了很大的不同,即使对于只有用户名不同的主机也是如此。

我从 here 那里得到这个,但这不是主要答案。 此处列出的说明对我更有用。

调整您的 ~/.ssh/config 并添加:

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

现在使用 ssh 主机别名作为您的存储库:

$ git remote add origin example:repository.git

$ git pull origin master

它应该使用 other_id_rsa 键!

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

我们可以使用 git config core.sshCommand 来自 git 版本 2.10.0。 -c 选项可以临时更改配置。

克隆命令示例。

git -c core.sshCommand="ssh -i some.pem -F /dev/null" clone ...

参考:https://qiita.com/sonots/items/826b90b085f294f93acf