ssh 服务器如何知道哪个 public 密钥匹配 ssh 私钥?
How does ssh server know which public key to match the ssh private key?
服务器的authorized_keys包含几万个密钥,服务器如何知道哪个public密钥匹配当前用户的私钥?
例如,通常用户名总是git
,服务器如何知道当前用户的身份?
详细问题:
当我使用ssh通过git pull
拉取代码时,服务器如何知道git pull
来自哪个用户?
然后服务器如何获取与用户关联的public密钥?
因为public键注册到GitHub user account settings
通常,这种存储库托管服务会使用 SSH forced command:
填充其 ~git/.ssh/authorized_keys
command="/path/to/script userID",\
no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty \
ssh-rsa <yourPublicKey>
而不是:
ssh-rsa <yourPublicKey>
它使用与您的 public 密钥关联的用户 ID 调用脚本。
这就是 GitHub 将 git@github.com
与您的帐户相关联的方式。
在您的帐户中注册 public 密钥会使用命令和用户 ID 修改 ~git/.ssh/authorized_keys
,而不仅仅是 public 密钥。
- When I use ssh to pull the code by
git pull
, how does the server know that git pull
is from which user?
- And then how does the server get the public key associated with the user?
实际上 GitHub 获取您的 public 密钥作为您与 GitHub 之间 SSH 事务的一部分:然后它从其 ~git/.ssh/authorized_keys
[=22= 中获取您的用户 ID ]
最后,它检查该用户是否被授权从远程存储库 clone/fetch/pull(例如,它可以是私有的,在这种情况下,用户最好是该存储库的所有者或声明的合作者) .
简而言之,事实并非如此。不过,这个答案不是很有帮助。
假设您是一个 ssh 服务器,您刚刚获得了一个用户名(此时始终为 git
)和一个 public 密钥。
您现在看 ~git/.ssh/authorized_keys
,这是一个充满行的大文件,由四个 space 分隔的字段组成:
- 选项
- 键类型
- base64 编码密钥
- 评论
(虽然选项部分可以省略)。这里的第一个重要部分是 base64 编码密钥:它由 public 密钥和一些其他内容组成。
现在让我们再看看你的问题:
The server's authorized_keys contains tens of thousands keys, how does the server know which public key to match the current user's private key?
没有。 不需要。它现在手头有两个public键;它只需要匹配两个 public 键 ,这很容易,因为它们要么逐位匹配,要么不匹配。所以此时它所要做的就是逐行扫描整个 authorized_keys
文件,检查:这一行是否具有相同的 public 键?
当然,它必须进一步验证:仅拥有 public 密钥并不意味着您就是您所声称的那个人。但这对于第一次通过就足够了。在 authorized_keys
文件中找到(或“a”)右行后,sshd 现在可以使用 选项 来决定你是谁,如果你通过了其余的身份验证过程。
逻辑上:
当您使用 GitHub 注册 public 密钥时,GitHub 将关联
public 密钥与您的帐户。
当您尝试登录 GitHub 时,您告诉 GitHub 哪个 public 键
使用 in some way.
GitHub 然后使用 public 密钥对您进行身份验证,您需要
使用私钥完成认证过程。
如果您通过认证,GitHub根据
步骤 1.
SSH 密钥身份验证仅在第一次显式设置密钥时使整个过程隐式化。
Username/Password 身份验证每次都是显式的。
相关:How does SSH public key authentication work (picking up right keys)
服务器的authorized_keys包含几万个密钥,服务器如何知道哪个public密钥匹配当前用户的私钥?
例如,通常用户名总是git
,服务器如何知道当前用户的身份?
详细问题:
当我使用ssh通过
git pull
拉取代码时,服务器如何知道git pull
来自哪个用户?然后服务器如何获取与用户关联的public密钥?
因为public键注册到GitHub user account settings
通常,这种存储库托管服务会使用 SSH forced command:
填充其~git/.ssh/authorized_keys
command="/path/to/script userID",\
no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty \
ssh-rsa <yourPublicKey>
而不是:
ssh-rsa <yourPublicKey>
它使用与您的 public 密钥关联的用户 ID 调用脚本。
这就是 GitHub 将 git@github.com
与您的帐户相关联的方式。
在您的帐户中注册 public 密钥会使用命令和用户 ID 修改 ~git/.ssh/authorized_keys
,而不仅仅是 public 密钥。
- When I use ssh to pull the code by
git pull
, how does the server know thatgit pull
is from which user?- And then how does the server get the public key associated with the user?
实际上 GitHub 获取您的 public 密钥作为您与 GitHub 之间 SSH 事务的一部分:然后它从其 ~git/.ssh/authorized_keys
[=22= 中获取您的用户 ID ]
最后,它检查该用户是否被授权从远程存储库 clone/fetch/pull(例如,它可以是私有的,在这种情况下,用户最好是该存储库的所有者或声明的合作者) .
简而言之,事实并非如此。不过,这个答案不是很有帮助。
假设您是一个 ssh 服务器,您刚刚获得了一个用户名(此时始终为 git
)和一个 public 密钥。
您现在看 ~git/.ssh/authorized_keys
,这是一个充满行的大文件,由四个 space 分隔的字段组成:
- 选项
- 键类型
- base64 编码密钥
- 评论
(虽然选项部分可以省略)。这里的第一个重要部分是 base64 编码密钥:它由 public 密钥和一些其他内容组成。
现在让我们再看看你的问题:
The server's authorized_keys contains tens of thousands keys, how does the server know which public key to match the current user's private key?
没有。 不需要。它现在手头有两个public键;它只需要匹配两个 public 键 ,这很容易,因为它们要么逐位匹配,要么不匹配。所以此时它所要做的就是逐行扫描整个 authorized_keys
文件,检查:这一行是否具有相同的 public 键?
当然,它必须进一步验证:仅拥有 public 密钥并不意味着您就是您所声称的那个人。但这对于第一次通过就足够了。在 authorized_keys
文件中找到(或“a”)右行后,sshd 现在可以使用 选项 来决定你是谁,如果你通过了其余的身份验证过程。
逻辑上:
当您使用 GitHub 注册 public 密钥时,GitHub 将关联 public 密钥与您的帐户。
当您尝试登录 GitHub 时,您告诉 GitHub 哪个 public 键 使用 in some way.
GitHub 然后使用 public 密钥对您进行身份验证,您需要 使用私钥完成认证过程。
如果您通过认证,GitHub根据 步骤 1.
SSH 密钥身份验证仅在第一次显式设置密钥时使整个过程隐式化。
Username/Password 身份验证每次都是显式的。
相关:How does SSH public key authentication work (picking up right keys)