我在我的 macos 上使用兩個 Github SSH 密鑰時遇到問題,它卡在重新啟動后使用的第一個帳戶上。
Background:
我的 Mac 設定了兩個 Github 帳戶 - 一個用于personal存盤庫,一個用于存盤庫business。商業回購是私人的。個人 repo 是公開的,但只有我的個人帳戶可以推送。
System setup:
我的系統和全域配置是空的
~/.ssh/配置:
# business
Host business
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
UseKeychain yes
AddKeysToAgent yes
#personal
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
UseKeychain yes
AddKeysToAgent yes
個人reporoot/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = personalgithubaccount
email = [email protected]
[remote "origin"]
url = git@personal:mygithubuser/personalrepo
fetch = refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
businessreporoot/.git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = businessgithubaccount
email = [email protected]
[remote "origin"]
url = git@business:businessname/businessrepo
fetch = refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
一切都按預期進行,直到我重新啟動計算機。當我嘗試推送到我的個人存盤庫時,我注意到了這個問題。它錯誤地說企業帳戶無權推送到個人存盤庫。
I spent a number of hours trying through Stack Overflow and internet articles, most of which recommended deleting my macos Keychain git entries and removing osxkeychain from the config files, but no no avail. Finally, resorting to troubleshooting 101, I rebooted and viola, I could push to the personal account and it used my personal credential now instead of my business credential.
BUT -- then I went to git pull on the business repo and it said it was unable to find the repository. Figuring I had the reciprocal problem and it was now stuck on my personal account, I rebooted, pulled the business repo and it worked, but now I couldn't push to my personal repository because git was trying to use my business repo credential again.
我在鏈接到這些說明的這篇 SO 文章中找到了一種解決方法。似乎在重新啟動后運行將使 Git 尊重每個 repo 的 SSH 密鑰。但是隨后的 macos 重啟又把問題帶回來了。$ ssh-add -D
所以......問題是,為什么 Git 會被固定到重啟后使用的第一個 SSH 憑據,為什么清除 SSH 身份并$ ssh-add -D解決問題,以及如何改進我的設定,以便我不必做解決方法重啟后?
uj5u.com熱心網友回復:
TL; 博士
使用IdentitiesOnly yes和IdentityFile小心地聲稱自己只是一個人。也就是說,稍微更新您的部分:
Host business
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
然后確保您將 URLssh://business/path/to/work-repo.git用于作業存盤庫(或者business:path/to/repo.git如果您喜歡簡短版本)和ssh://personal/path/to/personal-repo.git個人存盤庫。
長
所以......問題是,為什么 Git 會被固定到重啟后使用的第一個 SSH 憑證......
它不是,真的 - 這不是一個真正的 macOS 問題,它在任何 ssh 設定中都是通用的(盡管細節會因您是否使用 ssh-agent 以及如何使用而有所不同;macOS 設定您以便所有新的終端視窗默認情況下共享一個代理,這通常是您想要的)。理解這一點的技巧是了解 ssh 密鑰的作業原理:ssh 如何向您希望驗證的站點提供密鑰,以及 GitHub 如何使用這些 ssh 密鑰。
首先,讓我們定義一個 ssh 密鑰。它是:
- 用于某些加密演算法的一串位元組;
- 由公鑰和??私鑰構建。
通常,您將私鑰對自己保密并提供公鑰,或者用私鑰加密的東西,或者用兩個密鑰加密的東西,或者其他任何東西:細節并不是非常重要。我們知道(和關心)什么是公共密鑰被給出了,和私鑰不,我們可以假裝說的“鑰匙”出示給其他一些網站(如github.com)是公鑰.
現在,僅提供密鑰本身并不能證明您就是您。有人可能在較早的交易中偷偷復制了您的公鑰。因此,GitHub(或您連接的任何人)會向您發送一些加密的隨機資料,您只有擁有私鑰才能解密這些資料。你解密它,從而向 GitHub 證明你不僅擁有你發送給他們的公鑰,而且你是相應私鑰的持有者。
那么,GitHub 考慮到這兩件事,現在知道你就是你聲稱的那個人。但你聲稱是誰? 為什么,誰擁有那個公鑰,就是誰。
因此,假設您有兩個鍵,它們都可以使用。今天,你目前的公鑰一個第一。他們向您發送一個挑戰,以測驗您是否真的是A的私有一半的持有者,并且您做出了正確的回應。好吧,這就解決了:你是A。
明天,你叫他們起來和現在的公鑰乙第一。他們向您發送挑戰,您正確回應,然后就解決了。你是B。
你就是你第一次提供的人。所以你的人是,據GitHub上來講,是你聲稱自己是第一次人。
但是假設在星期六(如果明天是星期五)您提供公鑰A但未能發送正確的回應,然后提供公鑰B并發送正確的回應。GitHub 現在會認為你是B。
所以 GitHub真的相信你就是你成功宣稱的那個人。這意味著,即使你總是提供一個第一,撤銷你的私鑰對一個就足以讓你成為乙。這或多或少是你ssh-add -D所做的。(你的 ssh 可能很聰明,如果它沒有正確的私鑰,甚至不提供公鑰,因為如果你計劃在挑戰中失敗,那么聲稱自己是A毫無意義。當然,無論哪種方式都有效,但總的來說,最好不要用一百萬次按鍵嘗試過多地敲門:人們或正確編程的計算機會產生懷疑。)
最后,這意味著您必須首先確保 Git不會提供錯誤的密鑰。使用代理時,您可以添加任意數量的密鑰,ssh 將能夠“查看”所有密鑰。這將提供所有的作業重點對的,以某種順序,除非你使用IdentitiesOnly yes。如果您確實使用此行,它將僅提供有IdentityFile行和/或-i選項的鍵。(請注意,您可以列出多個檔案,而 ssh 仍會嘗試多個密鑰,無論順序如何。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/400650.html
標籤:混帐 苹果系统 github ssh ssh-keys
