我無法讓 git ssh 身份驗證與多個用戶一起使用。最初,它總是會嘗試使用我的 home-key 進行身份驗證
~/.ssh/home-key,
直到我找到這個答案
https://gist.github.com/Jonalogy/54091c98946cfe4f8cdab2bea79430f9?permalink_comment_id=3700405#gistcomment-3700405
我運行了那個命令并成功了。它起作用了,但是現在我遇到了相反的問題,并且它總是嘗試進行身份驗證,
~/.ssh/work-01
盡管有
~/.ssh/home-01
我知道 git 組態檔是正確的,因為在我的作業倉庫中,當我運行
gitconfig user.email
我明白了
rob@Robs-MBP test % git config user.email
[email protected]
同樣在我的家庭倉庫中
我明白了
rob@Robs-MacBook-Pro ~ % git config user.email
[email protected]
據我所知,我的 ~/.ssh/config 檔案是正確的
#Default GitHub
Host github.com-home
HostName github.com
User git
IdentityFile /Users/rob/.ssh/home-01
IdentitiesOnly yes
#wearenv-git
Host github.com-work
HostName github.com
User git
IdentityFile /Users/rob/.ssh/work-01
IdentitiesOnly yes
我的全域 git 組態檔是這個
[user]
name = home-username
email = [email protected]
[includeIf "gitdir:~/work-repos"]
path = /work-repos/.gitconfig
[init]
defaultBranch = main
[color]
ui = auto
[core]
editor = code --wait
這includeIf一點
[user]
name = work-username
email = [email protected]
[init]
defaultBranch = main
[color]
ui = auto
[core]
editor = code --wait
甚至設定本地組態檔
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = [email protected]:wearenv-digital/test.git
fetch = refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
[user]
name = work-username
email = [email protected]
我已經檢查,仔細檢查和三重檢查我已將正確的密鑰檔案上傳到 git。
Git 可以使用 work.key 進行身份驗證,如下所示:
rob@Robs-MacBook-Pro ~ % ssh -i~/.ssh/work-01 [email protected]
PTY allocation request failed on channel 0
Hi work-username! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
rob@Robs-MacBook-Pro ~ %
似乎當我只有work01in 時ssh-agent,它總是嘗試使用 [email protected] 進行身份驗證
反之亦然home-01
然后,當兩個鍵都在 中時ssh-agent,它總是嘗試使用添加的第一個鍵。
編輯我可以提供的詳細輸出
ssh -i ~/.ssh/work-01 [email protected]
例如
ssh -i ~/.ssh/work-01 -v [email protected]
uj5u.com熱心網友回復:
這部分:
#Default GitHub Host github.com-home HostName github.com User git IdentityFile /Users/rob/.ssh/home-01 IdentitiesOnly yes #wearenv-git Host github.com-work HostName github.com User git IdentityFile /Users/rob/.ssh/work-01 IdentitiesOnly yes
.ssh/home-01是正確的,假設身份在命名檔案中,即.ssh/work-01在您的主目錄中,即/Users/rob. 您可以通過撰寫使這更簡單:
IdentityFile ~/.ssh/home-01
例如 as~表示“我的主目錄”,因此這會自動成為正確的~/.ssh/目錄,而您只需要正確的檔案名即可。
但這部分是錯誤的:
ssh -i~/.ssh/work-01 [email protected]
好吧,錯誤太強了:它作業得很好,只是矯枉過正。你想要的要簡單得多:
ssh github.com-home
“登錄”以[email protected]使用您的家庭身份,以及
ssh github.com-work
“登錄”以[email protected]使用您的作業身份。也就是說,我們不 github.com使用偽造的“主機名”。我會拼寫這個gh-work,gh-home簡稱自己:
Host gh-work
User git
[etc] 以便“主機名”是不合格的字串gh-workor gh-home,它更短且更易于鍵入。
在您的 Mac 上,您可能首先需要運行:
ssh-add ~/.ssh/work-01
和:
ssh-add ~/.ssh/home-01
(雖然只是在你重新啟動后一次)。1 用于ssh-add -l列出您的代理知道的 ID。
1這利用了 macOS在您登錄時為您啟動 ssh 代理的事實,為每個終端視窗中打開的每個 shell 提供正確的環境變數。這樣,從您登錄時打開的每個終端視窗都共享一個代理行程。沒有必要開始一個。.bashrcLinux 系統通常要求您在登錄時啟動一個,這反過來又需要在一個或.profile其他啟動檔案中進行一些花哨的操作。macOS 環境使這變得容易得多。
讓 Git 使用正確的 URL
一旦上述方法奏效——你可以輕松地測驗它ssh -Tv github.com-home,因為你已經寫了東西——你只需要說服Git從存盤庫克隆和推送到存盤庫ssh://github.com-home/personal/repo,ssh://github.com-work/work/repo例如。
這就是這種技巧的用武之地:
[includeIf "gitdir:~/work-repos"] path = ~/work-repos/.gitconfig
(請注意,我添加了一個~我認為你放棄的角色)。
我們可以做的includeIf是:
- 設定
user.name和user.email作業與個人回購 - 讓 Git 使用URL
insteadOf規則ssh://github.com/
并且(這是該includeIf部分的原因)根據它在我們的主目錄中的位置,使實際替換取決于這是一個“作業”或“主”存盤庫。
如果您將您的作業存盤庫~/work-repos/*(如此處所示)和您的個人存盤庫放在其他地方(可能是 ~/home-repos/ ? use whatever you like; as you've set this up only the ~/work-repos/ones get the work treatment), *and* add these lines to ~/work-repos/.gitconfig`:
[url "ssh://github.com-work/"]
insteadOf = ssh://github.com/
現在如果你運行:
git clone ssh://github.com/company/repo.git
Git 會接受它并將其更改為:
git clone ssh://github.com-work/company/repo.git
這反過來會導致 Git 運行:
ssh github.com-work git-upload-...
也就是說,Git 會要求 ssh 連接到一個名為的主機github.com-work并運行智能協議以訪問company/repo.git.
我們已經知道(并且已經測驗過)ssh github.com-work實際連接到 github.com, not github.com-work,并使用 work-user 公鑰以作業用戶身份登錄,以便您的 ssh 命令被正確識別為“you-at-$company”。因此,該用戶可以訪問公司存盤庫并可以克隆它。
如果您不介意一開始就輸入“錯誤”的 URL(包括家庭/作業的區別),那么您不需要這個insteadOf技巧。您使用假主機名的事實將觸發 ssh在真實( ) 主機上使用所需的身份。但是添加技巧使這更加方便。github.cominsteadOf
由于很多人可能會使用[email protected]:user/repo.git而不是ssh://github.com/user/repo.git,或git@在前面添加github.com獲取ssh://[email protected]/user/repo.git,因此您可能需要多個insteadOf規則,例如:
[url "ssh://github.com-work/"]
insteadOf = ssh://github.com/
insteadOf = ssh://[email protected]/
insteadOf = [email protected]:
當然,有很多方法可以做到這一點。選擇一個最適合你的。 首先以ssh你想要的方式開始作業,完全不添加 Git。然后添加 Git,知道 ssh 正在按照您想要的方式作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488190.html
