了解用于通过 SSH 连接到服务器与访问的 ssh-keygen GitHub

Understanding ssh-keygen for SSH'ing into a server vs accessing GitHub

所以我的理解是 ssh-keygen 是 OpenSSH 提供的众多命令之一。

当使用 ssh-keygen 生成 private/public 密钥对时,我知道您可以将 public 密钥放到远程服务器上(在 ~/.ssh/authorised_keys 内)并且它'我将允许您通过 ssh 进入该服务器。

但是似乎有两种不同的方法来生成密钥对,从某种意义上说,您可以单独使用 ssh-keygen,而当您 cat public关键你会看到这样的东西:

ssh-rsa XXXX User@ComputerName

对我来说 User@ComputerName 看起来像 M@Marks-MBP.default

另一种方法是使用 -C 注释标志(GitHub 建议这样做)。它似乎不会使用 User@ComputerName,而是使用您的电子邮件地址代替它(或您碰巧使用的任何 'comment')。

我认为对于通过 SSH 连接到服务器,您用于登录的用户名是 public 密钥中 -C 注释设置的任何内容,我的想法是否正确?

因此,如果我根本不使用 -C,那么它将生成一个 public 密钥,最后带有 M@Marks-MBP.default。然后我假设用户名是 M.

但是如果我使用 -C "bingbop",那么我假设 SSH 的用户名是 bingbop

对吗?

此外,是否有任何技术原因 GitHub 建议使用 -C 指定您的电子邮件地址而不是关闭该标志?

我猜当你尝试克隆或推送到你的 repo(使用他们的 git@github.com:<user>/<repo>.git 协议)时,它无法单独通过 public 密钥识别你,它需要评论设置为您的 GitHub 电子邮件地址

评论仅供参考;它与 Git 已知的用户名无关。当您通过 ssh 进入 github 时,它会将 public 密钥与您存储库中的私钥相匹配,因此仅通过它就知道要与哪个用户关联。

Am I right in thinking that for SSH'ing into a server, the username you use to login is whatever the -C comment is set to in your public key?

没有。密钥的评论字段实际上只是:一个(可选的)评论,按照惯例通常设置为您的电子邮件地址,但如果未指定,则碰巧默认为 user@host

您在通过 SSH 登录时使用的用户名与该评论字段无关或源自该评论字段。

Also, is there any technical reason GitHub suggest using -C to specify your email address rather than leaving that flag off?

是的,原因是默认的 user@host 很少向您将 public 密钥交给的人提供有用的信息。另一方面,您的电子邮件地址是更有价值的信息。

I'm guessing that when you try to clone or push to your repo (using their git@github.com:/.git protocol) that it can't identify you by the public key alone, it needs the comment to be set to your GitHub email address.

GitHub 可以 仅通过提供的密钥来识别您,因为它在所有 GitHub 用户中都是唯一的。在您提供的 URI 语法中,@ 之前的 git 是提供给 SSH 的用户名。因此 GitHub 为其所有用户使用通用用户名 git,并且各个用户通过他们提供的密钥进行识别。