git 指定要提交的ssh key

问题描述

 ssh具备-i选项,用于告知在验证时使用哪一个私钥文件:javascript

-i identity_filephp

  • Selects a file from which the identity (private key) for RSA or DSA authentication is read.  The default is 
~/.ssh/identity for protocol version 1, and  ~/.ssh/id_rsa and  ~/.ssh/id_dsa for protocol version 2.  Identity files may also be specified on a per-host basis in the configuration file.  It is possible to have multiple  -i options (and multiple identities specified in configuration files).

 

有没有相似的方法告诉git哪一个私钥文件在~/.ssh目录中有多个私钥的系统上使用?java

 

最佳解决方案

~/.ssh/config中,添加:git

  1.  
    host github.com
  2.  
    HostName github.com
  3.  
    IdentityFile ~ /.ssh/id_rsa_github
  4.  
    User git

如今你能够作git clone git@github.com:username/repo.gitgithub

注意:验证IdentityFile的权限是否为400.SSH将以不清楚的方式拒绝太可读的SSH密钥。它只会看起来像一个凭证拒绝。在这种状况下,解决方案是:shell

chmod 400 ~/.ssh/id_rsa_github 

 

次佳解决方案

环境变量GIT_SSH_COMMAND

从Git版本2.3.0可使用环境变量GIT_SSH_COMMAND,以下所示:安全

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example 

请注意,-i有时能够被您的配置文件覆盖,在这种状况下,您应该给SSH一个空配置文件,以下所示:ruby

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone example 

配置core.sshCommand

从Git版本2.10.0,您能够配置每一个repo或全局,因此您没必要再设置环境变量!dom

  1.  
    git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
  2.  
    git pull
  3.  
    git push

 

第三种解决方案

没有直接的方法告诉git哪一个私钥要使用,由于它依赖于ssh进行存储库认证。可是,仍有几种方法能够实现您的目标:ssh

选项1:ssh-agent

您可使用ssh-agent临时受权您的私钥。

例如:

$ ssh-agent sh -c 'ssh-add ~/.ssh/id_rsa; git fetch user@host' 

选项2:GIT_SSH_COMMAND

使用GIT_SSH_COMMAND环境变量(Git 2.3.0+)传递ssh参数。

例如:

  1.  
    $ GIT_SSH_COMMAND= 'ssh -i ~/.ssh/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
  2.  
    git clone user@host

您能够在一行中输入全部内容,省略

选项3:GIT_SSH

使用GIT_SSH环境变量传递ssh参数。

例如:

  1.  
    $ echo 'ssh -i ~/.ssh/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $*' > ssh
  2.  
    $ chmod +x ssh
  3.  
    $ GIT_TRACE= 1 GIT_SSH='./ssh' git clone user@host

注意:上面的行是你应该粘贴到你的终端的shell(终端)命令行。他们将建立一个名为ssh的文件,使其可执行,并(间接)执行它。

选项4:~/.ssh/config

使用其余答案中建议的~/.ssh/config文件,以指定您的私钥的位置。

 

第四种方案

编写一个使用所需参数调用ssh的脚本,并将脚本的文件名放在$GIT_SSH中。或者将您的配置放在~/.ssh/config中。

 

第五种方案

在与$GIT_SSH斗争以后,我想分享一下对我有用的东西。

经过个人例子,我会假设你的私钥位于/home/user/.ssh/jenkins

错误避免:GIT_SSH值包括选项

$ export GIT_SSH="ssh -i /home/user/.ssh/jenkins" 

或者任何相似的将失败,由于git将尝试将该值做为文件执行。所以,您必须建立一个脚本。

$ GIT_SSH脚本/home/user/gssh.sh的工做示例

脚本将被调用以下:

$ $GIT_SSH [username@]host [-p <port>] <command> 

工做示例脚本可能以下所示:

  1.  
    #!/bin/sh
  2.  
    ssh -i /home/user/.ssh/jenkins $*

注意$*究竟是它的重要组成部分。

甚至更安全的选择,这将防止任何与您的默认配置文件中的任何可能的冲突(加上明确说起要使用的端口)将是:

  1.  
    #!/bin/sh
  2.  
    ssh -i /home/user/.ssh/jenkins -F /dev/null -p 22 $*

假设脚本在/home/user/gssh.sh中,那么你将:

$ export GIT_SSH=/home/user/gssh.sh 

全部人都应该工做。

 

第六种方案

若是您不想在每次运行git时指定环境变量,则不要再使用另外一个包装器脚本,不要/不能运行ssh-agent(1),也不想为此下载另外一个包,请使用git-remote-ext(1 )外部运输:

  1.  
    $ git clone 'ext::ssh -i $HOME/.ssh/alternate_id git.example.com %S /path/to/repository.git'
  2.  
    Cloning into 'repository'
  3.  
    (...)
  4.  
    $ cd repository
  5.  
    $ git remote -v
  6.  
    origin ext::ssh -i $HOME/.ssh/alternate_id git.example.com %S /path/to/repository.git (fetch)
  7.  
    origin ext::ssh -i $HOME/.ssh/alternate_id git.example.com %S /path/to/repository.git (push)

我认为这个解决方案是优越的,由于:

  • 它是存储库/远程特定的

  • 避免包装脚本膨胀

  • 不须要SSH代理 – 若是您想要无人值守的克隆/推/拉(例如在cron)

  • 固然,没有外部工具须要

 

第七种方案

您可使用ssh-ident而不是建立本身的包装器。

您能够阅读更多:https://github.com/ccontavalli/ssh-ident

它首次须要加载ssh密钥,一次,即便是多个登陆会话,xterms或NFS共享的家庭。

使用一个微小的配置文件,它能够自动加载不一样的密钥,并根据您须要作的事情将它们分隔在不一样的代理(代理转发)中。

 

第八种方案

~/.ssh/config中使用自定义主机配置,以下所示:

  1.  
    Host gitlab- as-thuc
  2.  
    HostName git.thuc.com
  3.  
    User git
  4.  
    IdentityFile ~ /.ssh/id_rsa.thuc
  5.  
    IdentitiesOnly yes

而后使用您的自定义主机名:

git remote add thuc git@gitlab-as-thuc:your-repo.git 
 

欲了解更多详情,请阅读:http://itblog.study.land/how-to-specify-different-ssh-keys-for-git-push-for-a-given-domain/

参考文献

相关文章
相关标签/搜索