git puttykeyfile 兩三事

在windows下使用git,通常是安裝tortoiseGit和msysGit。
前者提供了GUI介面透過自帶的putty以plink介面帶出pageant提供ssh授權。
後者提供了command line介面讓你能以下指令方式操作git。

產生ssh需要的private/public key有兩種方式。

  1. 使用puttygen產生private/public ppk檔案,使用時需要pageant工具作為authentication agent。
  2. 在command line下使用ssh-keygen產生~/ .ssh/id_rsa 和 ~/.ssh/id_rsa.pub。
    ssh-keygen產生的格式跟ppk不一樣,但可以透過puttygen載入再另存成ppk格式。

兩種方式設定git需要的ssl

一般而言使用puttygen產生的ppk檔配合pageant就能正常使用了。

  1. tortoiseGit: 只要使用GUI介面裡的 setting > Git > Remote - choose origin > Putty Key: 選擇要使用的 private ppk 檔。
    這時在.git/config裡[remote “origin”]區會增加一筆puttykeyfile = c:\git.privatekey.ppk記錄
    當然也能透過 git config —local remote.origin.puttykeyfile c:\git.privatekey.ppk 來加入。
    這筆記錄能在使用tortoiseGit做remote push/pull時,若”Autoload Putty Key”有被勾選,則透過plink自動帶出載入指定ppk的pageant。

    然而若你使用tortoiseGit時不勾選”Autoload Putty Key”,也不手動啟動pageant並載入適當的ppk。在對remote進行操作時就會發生error。

    類似的情況在command line裡下git指令對remote做push操作也一樣;必需先手動啟動pageant並載入適當的ppk。

  2. 使用 ssh-keygen會產生~/.ssh目錄,並在目錄內放ssh相關的資訊和產生的key。
    使用ssh-keygen產生private/public key pair供免密碼ssh安全登入很常見。
    如前面所提,~/.ssh/id_rsa是可以透過puttygen轉成ppk格式供putty/tortoiseGit使用的。

    特別的是若使用ssh-keygen的方式產生了~/.ssh裡的private/public key pair,接下來就可以不需要pageant這類的authentication agent了。
    此時直接使用tortoiseGit或command line下使用git指令,都會直接使用~/.ssh/id_rsa。
    就像在unix下使用ssh的方式,方便很多。

    值得一提的是,這時啟動pageant載入ppk還是有效果的。
    remote.origin.puttykeyfile c:\git.privatekey.ppk在使用tortoiseGit時勾選”Autoload Putty Key”自動帶出載入指定ppk的pageant時一樣有用。手動啟動pageant並載入適合的ppk也一樣有效。

用bat檔寫好script來啟動pageant帶上ppk檔,是比操作視窗加ppk檔方便得多的方式。

1
start /d "c:\Program Files\TortoiseGit\bin" pageant.exe c:\git.privatekey.ppk c:\git.privatekey.repo2.ppk