我試圖設定 ssh 密鑰,以免每次推送更新時都輸入我的用戶名/令牌。我使用本手冊設定 ssh 密鑰并將 ssh 密鑰添加到我的 GitHub 帳戶:
https://docs.github.com/en/authentication/connecting-to-github-with-ssh
但它仍然詢問我的用戶名/令牌。我發現我的 URL 地址是 HTTPS 而不是 [email protected],所以我試圖從這里遵循解決方案:
SSH 密鑰 - 仍在要求輸入密碼和密碼
使用命令替換 URL:
git remote set-url origin [email protected]:USERNAME/REPOSITORY.git
但是,因為專案存盤庫不在我的帳戶中(它位于我授權提交更改的公司帳戶中),所以這不起作用(看不到存盤庫)。我不確定應該如何重寫此命令以使其作業。
uj5u.com熱心網友回復:
我對你的困惑有點困惑。看看我能不能理直氣壯。?? 當您向 GitHub 表明自己的身份時,您就聲稱自己是某個人。您聲稱的某人確實有權訪問某個存盤庫,或者無權訪問該存盤庫。
(到目前為止和我在一起?)
當您使用 HTTPS“登錄”到 GitHub 時,您提供:
- 用戶名:這只是一個用戶名,即它不是秘密,它可以識別您的身份;和
- 密碼(無論如何在 Ye Olden Dayse 中):這只是您的秘密密碼,它所做的只是證明您就是您所說的那個人。
他們現在想出了一些辦法,因此您提供了一個“PAT”而不是“密碼”,這仍然是一個秘密,但現在有一些內部結構可以讓您做一些花哨的事情,例如說某些 PAT 的身份驗證值低于其他。這也意味著,如果您的“密碼”,即 PAT,曾經被泄露,您仍然擁有真正秘密的密碼 — 實際密碼 — 您可以使用它來撤銷舊 PAT 并生成新 PAT。
如果你喜歡這個系統,你可以繼續使用它,但如果你喜歡 ssh 系統——這是我自己的偏好——那么我們有一個有點棘手的東西。當您使用 ssh “登錄”到 GitHub 時,您提供:
- 固定用戶名
git:它不是秘密,但它也不能識別你;和 - 使用或某些類似程式生成的ssh 密鑰
ssh-keygen。
ssh 密鑰不是,也從來不是,只是一個密碼。它充滿了結構:例如,它說明了使用什么樣的加密來制作它,以便我們知道它是一個舊的非常短的 RSA 密鑰(不再安全),一個更漂亮的更長的 RSA 密鑰(仍然安全), ECDSA 密鑰、ed25519 密鑰等。它們也是成對出現的:每對中有一個公鑰和一個私鑰。該公開鍵的設計,讓您可以把它交給別人,包括GitHub上,甚至明文。有人,否則,你給此鍵來可以給你一個“挑戰”,他們已經加密的; 您的計算機使用您對應的私鑰解密“挑戰”并做出回應,證明您確實是您所聲稱的人。
但請稍等。 你聲稱是誰? 我們只是說我們要聲稱自己是固定名稱git。好吧,現在我們有一個小技巧:這些公鑰和私鑰對是唯一的。當您生成對時,您交給 GitHub 的公鑰是唯一的。他們——GitHub——所要做的就是在你給他們公鑰時記錄你的身份,你可以通過使用你的實際密碼登錄到 GitHub 來做到這一點。
所以,你在這里,在你的電腦上,用你的 ssh 做你自己。例如,您可能已經生成了三個身份密鑰。如果只有一個這三個作品來訪問你想要的資訊庫,您只需出示給GitHub上的一個標識密鑰做的作業,而不是其他兩個是不作業的。
你這樣做的方式是......根本不使用Git。Git 不執行 ssh。Git 運行ssh,它完成所有 ssh 作業。所以你必須檢查你的 ssh 檔案。1 幸運的是,幾乎所有的 ssh 實作都有很多共性,包括$HOME/.ssh/config檔案的概念。這些組態檔讓您告訴 ssh,如果您要求連接到名為 的主機hub-id1,您希望使用一對密鑰連接到實際名為 的主機github.com。如果您要求連接到名為 的主機hub-id2,您希望使用不同的單個密鑰對連接到實際名為 的主機github.com。您可以重復使用盡可能多的密鑰對,使用盡可能多的身份。
為此,在您的.ssh/config檔案中,您需要這些神奇的線條:
Host hub-id1
Hostname github.com
IdentityFile ~/.ssh/id_github_1
User git
IdentitiesOnly yes
Host hub-id2
Hostname github.com
IdentityFile ~/.ssh/id_github_2
User git
IdentitiesOnly yes
等等。
您輸入的主機名由Host您決定,但它應該與您在 ssh URL 中設定的名稱相匹配:
git remote set-url origin ssh://hub-id2/path/to/repo.git
或者:
git remote set-url origin hub-id2:path/to/repo.git
該User行是可選的,但如果您將其包含在此處,則可以git@在放置位置的前面省略[email protected],但現在正在放置git@hub-id1等等。
該HostName行是必需的:這是 ssh 將查找以獲取 IP 地址以連接到 GitHub 的內容。
The IdentityFile line is required: it provides the path name to the file(s) in which you've stored the public and/or private key(s). There is a bit of trickiness here: if you use the ssh agent, you can provide just the pathname of the .pub file, and you need only the .pub file on this host if you're logging in to some corporate machine from your laptop and keep your private keys only on your laptop, for instance. In some cases, however, you must point ssh to the private key file, and have the public key have the same name but with .pub added.
The IdentitiesOnly line is optional, but advisable: it says use only the key I listed here. If you omit this line, ssh will try other keys as well, if the IdentityFile keys don't seem to be working. (You can observe this in action with ssh -Tv output: each key that ssh tries, results in a debug line.)
If you only have one identity on GitHub, you don't need this complication. Your one identity on GitHub—you as yourself—will give you access to the repository, or won't, based on what whoever owns the repository set up. But if you have more than one identity on GitHub, and are using ssh, this is how you tell your ssh commands which identity to send to GitHub.
(Note that if you have encrypted access to your private ssh keys with a passphrase, you may have to type your passphrase into ssh, or into your ssh agent. This is quite independent of all of the above.)
1On Windows systems, Git comes with a replacement ssh,2 in case your Windows ssh is too ancient to be usable. Modern Windows comes with a modern ssh, which is usable. These two different ssh-es may use different file storage. Be careful about which ssh you're using. Make sure your Git uses the ssh you want it to use, whichever one that is. Most other systems are easier to deal with, as they have just one ssh.
2This is similar to the way Git-for-Windows usually comes with a usable version of bash, because the old CLIs in old Windows systems are unusable (at least by Git). Neither the included bash, called git-bash for short, nor the included ssh are actually part of Git. They're just packaged together with Git for Windows, to make it usable.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/355859.html
上一篇:發送拉取請求后從遠程主分支拉取
