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/

參考文獻

相關文章
相關標籤/搜索