简体   繁体   English

更改 github 用于克隆和访问 GitHub 上的存储库的 ssh 密钥

[英]Change which ssh key github uses to clone and access repos on GitHub

I need to change which ssh key GitHub uses to clone and access repos on my local machine.我需要更改 GitHub 用于在我的本地计算机上克隆和访问存储库的 ssh 密钥。

For example, it is defaulting to use /c/Users/xxx.xxx/.ssh/id_ed25519.例如,默认使用 /c/Users/xxx.xxx/.ssh/id_ed25519。 But I need it to use /c/Users/xxx.xxx/.ssh/id_rsa_github但我需要它来使用 /c/Users/xxx.xxx/.ssh/id_rsa_github

Is there some way to change that configuration?有什么方法可以更改该配置吗?

There is—but first, we should straighten out your question.有——但首先,我们应该理清你的问题。 It's not GitHub that uses an ssh key.使用 ssh 密钥的不是GitHub It's ssh that uses an ssh key.它是使用 ssh 密钥的ssh So the place to look is ssh's configuration .所以要看的地方是ssh的配置

Some ssh configuration details are OS-dependent, but virtually all ssh implementations will use a hidden .ssh folder (directory) in your home directory (folder).一些 ssh 配置细节取决于操作系统,但几乎所有 ssh 实现都将使用您的主目录(文件夹)中的隐藏.ssh文件夹(目录)。 In this .ssh , you will find several ordinary files:在这个.ssh中,你会发现几个普通的文件:

  • known_hosts contains a list of fingerprints for hosts you've connected to before. known_hosts包含您之前连接到的主机的指纹列表。
  • authorized_keys , if it exists, contains a list of keys that someone can use to log in to your machine (if your machine allows logins at all). authorized_keys (如果存在)包含某人可以用来登录您的机器的密钥列表(如果您的机器完全允许登录)。
  • id_rsa and id_rsa.pub contain the default private and public keys for RSA encryption. id_rsaid_rsa.pub包含用于 RSA 加密的默认私钥和公钥。
  • id_ed25519 and id_ed25519.pub contain the default private and public keys for ED25519 encryption. id_ed25519id_ed25519.pub包含 ED25519 加密的默认私钥和公钥。
  • config contains your personal ssh configuration. config包含您的个人 ssh 配置。 This is the file you want to adjust.这是您要调整的文件。

The config file consists mainly of sets of lines, with a line-set starting with a Host line. config文件主要由行集组成,行集以Host行开头。 The Host line defines which name(s) the subsequent lines apply to. Host行定义后续行适用的名称。 So to make things apply to the command:所以要使事情适用于命令:

ssh github.com

for instance, you might write Host github.com as a line.例如,您可以将Host github.com写成一行。

Underneath the Host line (and normally indented further as a visual clue) you will add more configuration lines, such as:Host行下方(通常作为视觉线索进一步缩进)您将添加更多配置行,例如:

    User git
    IdentityFile ~/.ssh/id_rsa_github
    IdentitiesOnly yes

The User git line, if present, sets the default user name . User git行(如果存在)设置默认用户名 This is optional but saves you from having to type in git@ in front of github.com .这是可选的,但您不必在github.com前面输入git@

The IdentityFile line gives the path name of a key (key-pair, really: one file is normally named whatever.pub and the other whatever ) to be used with this host. IdentityFile行给出了与该主机一起使用的密钥的路径名(实际上是密钥对:一个文件通常命名为whatever.pub ,另一个命名为whatever )。 Since you say you want id_rsa_github used, that's the name we'll give here;既然你说你想使用id_rsa_github ,那就是我们在这里给出的名字; the ~/.ssh/ tells ssh to find this in the .ssh directory. ~/.ssh/告诉 ssh.ssh目录中找到它。 (You can use backwards slashes on Windows, but forwards ones are easier to type, and work everywhere , so that's what I would do and what I show here.) (您可以在 Windows 上使用反斜杠,但正斜杠更容易键入,并且可以在任何地方使用,所以这就是我要做的以及我在这里展示的内容。)

The IdentitiesOnly yes line is important, though often not crucially so: it tells ssh that, despite however many keys it may find on a "key ring" (eg, provided via an SSH Agent, using ssh-add ), you want any attempt to open a lock on github.com to use only this one particular key . IdentitiesOnly yes行很重要,但通常不是很重要:它告诉 ssh,尽管它可能在“密钥环”上找到许多密钥(例如,通过 SSH 代理提供,使用ssh-add ),但您需要任何尝试打开github.com上的锁以仅使用这一个特定的密钥 The reason this is important has to do with the way ssh works on GitHub.这很重要的原因与 ssh 在 GitHub 上的工作方式有关。

How ssh public key authentication works with GitHub ssh 公钥身份验证如何与 GitHub 配合使用

Whenever you ask to log in on GitHub—whether with https or ssh, in a browser or because Git is literally running ssh so as to access a repository on GitHub—you must provide two (or sometimes more) pieces of information to GitHub:每当你要求登录 GitHub 时——无论是使用 https 还是 ssh,在浏览器中,还是因为 Git 确实在运行 ssh 以访问 GitHub 上的存储库——你必须向 GitHub 提供两条(或有时更多)信息:

  • Who do you claim to be?你声称自己是谁?
  • What proof do you have that you are this person?你有什么证据证明你就是这个人?

When using https, this is pretty straightforward, because you send over a user name to say who you claim to be, and then an authorization token (basically a fancied-up password) to prove that you are that person.使用 https 时,这非常简单,因为您发送一个用户名来说明您声称自己是谁,然后发送一个授权令牌(基本上是一个虚构的密码)来证明您就是那个人。 When using ssh, however, you are required to claim that you are a guy named git at the ssh level.但是,在使用 ssh 时,您需要在 ssh 级别声明您是一个名为git的人 You don't send your actual user name here!您不会在此处发送您的实际用户名!

Instead, to provide the user name , GitHub keeps a giant table of user-name-and-public-keys.相反,为了提供用户名,GitHub 保留了一个巨大的用户名和公钥表。 Having provided the name git , GitHub now demand, via their ssh server, a public key.提供名称git后,GitHub 现在通过他们的 ssh 服务器要求一个公钥。 Your ssh code hunts around for all the public keys that it could possible use on this "door lock".您的 ssh 代码四处寻找可能在此“门锁”上使用的所有公钥。 Maybe there's only one key, in the file you've named.在您命名的文件中,也许只有一个键。 Maybe there are hundreds of keys, on an OSX Keychain, or in an SSH Agent, or in a Windows Keychain, or whatever: there are a lot of possibilities.也许有数百个密钥,在 OSX 钥匙串上,或在 SSH 代理中,或在 Windows 钥匙串中,或其他任何东西:有很多可能性。

Your ssh will begin offering public keys, one at a time, from the allowed list, until GitHub recognize one of these.您的 ssh 将开始从允许的列表中一次提供一个公钥,直到 GitHub 识别其中一个。 That public key has a user name attached to it, inside a table over on GitHub.该公钥附加了一个用户名,位于 GitHub 上的一个表中。 They then say: Okay, I see you're claiming to be _____ (fill in the blank), now prove it.然后他们说:好的,我看到你自称是_____(填空),现在证明一下。 Here's a secret challenge that you can reply to if and only if you have the private key too.这是一个秘密挑战,当且仅当您也拥有私钥时,您才能回复。 Your ssh then uses your private key to decrypt the challenge, and respond correctly, and now they know you hold both keys and therefore are the person you claimed to be.然后,您的 ssh 使用您的私钥解密挑战,并正确响应,现在他们知道您持有这两个密钥,因此就是您声称的那个人。

But who did you claim to be?但你声称自己是谁? You claimed to be the person whose public key got used .你声称自己的公钥被使用了 If you have more than one public key that could work , you've just claimed to be one of those several identities.如果您有多个可用的公钥,那么您只是声称自己是这几个身份之一。 Is that the identity you wanted?这就是你想要的身份吗? If so, great!如果是这样,太好了! If not, bad!如果没有,那就糟糕了!

The IdentitiesOnly line tells ssh that it should only try the public key(s) listed on these lines . IdentitiesOnly行告诉 ssh 它应该只尝试这些行中列出的公钥 That way you can have two or more public keys on GitHub, one for a work identity for instance and one for personal projects, and you control which of these public keys get offered.这样,您可以在 GitHub 上拥有两个或多个公钥,一个用于工作身份,一个用于个人项目,并且您可以控制提供哪些公钥。

(To make the multiple identities work, you simply define more than one Host : you might define a Host gh-work and a Host gh-home for instance. But we'll leave that for other questions.) (要使多个身份工作,您只需定义多个Host :例如,您可以定义一个Host gh-work和一个Host gh-home 。但我们将把它留给其他问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM