簡體   English   中英

Git 和 SSH,使用哪個密鑰?

[英]Git and SSH, which key is used?

假設您的.ssh目錄包含 30 個密鑰(15 個私有密鑰和 15 個公共密鑰)。

Git 在哪里可以檢查哪一個用於連接到給定的遠程存儲庫?

.ssh/config文件中的以下條目解決了該問題

  host git.assembla.com
  user git
  identityfile ~/.ssh/whatever

~/.ssh/whatever是你的私鑰的路徑

此外,用戶和主機可以從

git push git@git.assembla.com:repo_name.git
         ^__ ^_______________
         user host

在詳細模式下執行 ssh,也就是ssh -v user@host ,將打印大量調試信息,其中還包含有關它正在嘗試登錄的密鑰文件的詳細信息。

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/user/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 332
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).

現在如果你把它和 Git 自己的SSH 幫助頁面中的第 4 步結合起來, ssh -vT git@github.com可以給你答案。

注意:您還可以使用-i開關在命令執行期間告訴 ssh,使用哪個密鑰文件。

我想說對我的口味最實用的是:

GIT_SSH_COMMAND='ssh -v' git …

當然,根據具體情況,將其導出到當前 SHELL 的環境可能會有所幫助,這樣您就不必每次都手動添加它。 那么它會是這樣的:

export GIT_SSH_COMMAND='ssh -v'
git …

— 正如man git所暗示的,有一些環境變量會影響 Git 在使用 SSH 時的操作。 根據man ssh ,您可以在部署-v選項時獲得一些調試信息(不僅如此,如果您想了解更多信息,請查看手冊)。

使用哪個鍵?

在輸出中你會看到...

debug1: Offering public key: …

……這就是你問題的答案。

除非在.ssh/config中指定,否則它將使用默認的私鑰文件。

默認文件是~/.ssh/id_rsa~/.ssh/id_dsa~/.ssh/identity ,具體取決於協議版本。

這可能是超級優勢,但是在運行ssh -vT git@github.com之后它向我顯示它正在檢查/root/.ssh的密鑰,我期待它檢查我的主目錄,然后我意識到我登錄為根!

由於git僅使用ssh進行連接,因此它將使用ssh用於連接到遠程主機的任何密鑰。 有關詳細信息,請參閱~/.ssh/config文件; host塊使用IdentityFile指令來指定要使用的私鑰。 ssh_config(5)聯機幫助頁包含完整的詳細信息。

在遠程服務器上,編輯 sshd_config 文件並將 LogLevel 從 INFO 更改為 VERBOSE 並重新啟動 ssh。

現在,您的日志文件將保存用於驗證每個用戶的密鑰的指紋。

在 Ubuntu 上,這些文件是:

/etc/ssh/sshd_config
/var/log/auth.log

但它們在另一個發行版上可能會有所不同。 只需用谷歌搜索他們的位置(例如,有些人使用 /var/log/secure)。

您可以通過嘗試連接到 git@github.com 來檢查正在使用哪個密鑰:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type -1
> debug1: identity file /Users/you/.ssh/id_rsa-cert type -1
> debug1: identity file /Users/you/.ssh/id_dsa type -1
> debug1: identity file /Users/you/.ssh/id_dsa-cert type -1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Trying private key: /Users/you/.ssh/id_rsa
> debug1: Trying private key: /Users/you/.ssh/id_dsa
> debug1: No more authentication methods to try.
> Permission denied (publickey).

在那個例子中,我們沒有任何可供 SSH 使用的密鑰。 “身份文件”行末尾的“-1”表示 SSH 找不到要使用的文件。 稍后,“嘗試私鑰”行也表明未找到文件。 如果文件存在,這些行將分別為“1”和“提供公鑰”:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type 1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Offering RSA public key: /Users/you/.ssh/id_rsa

驗證公鑰已附加到您的帳戶

您必須向 GitHub 提供您的公鑰才能建立安全連接。

  1. 打開終端。

  2. 在后台啟動 SSH 代理。

    $ eval "$(ssh-agent -s)"

    代理pid 59566

  3. 查找並記下您的公鑰指紋。

    $ ssh-add -l -E sha256

    2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)

  4. 在任何 github 頁面的右上角,單擊您的個人資料照片,然后單擊設置。

  5. 在用戶設置側欄中,單擊 SSH 和 GPG 密鑰。

  6. 將 SSH 密鑰列表與 ssh-add 命令的輸出進行比較。

  7. 如果您在 GitHub 中沒有看到您的公鑰,則需要將您的 SSH 密鑰添加到 GitHub 以將其與您的計算機相關聯。

點擊此鏈接了解詳情

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM