这只是关于 git 如何运作的一般问题。我在 git 手册中找不到与此相关的任何内容,也许我没有找对地方。
当您使用来自 git 服务器的 ssh 链接进行克隆时,它使用的用户名是 git,而不是您尝试使用的密钥的用户名。为什么 git 这样做,以及 git 如何知道它应该使用哪个密钥对来验证连接。
当您使用来自 git 服务器的 ssh 链接进行克隆时,它使用的用户名是 git,而不是您尝试使用的密钥的用户名。
正确的。这样做的原因是该服务与用户帐户绑定,您需要以该用户身份访问服务器才能调用该服务。这确实是 SSH 的一个特性,而不是 Git 的特性——Git 只是使用 SSH 作为传输。此外,SSH——因此 Git——对与 SSH 密钥关联的用户一无所知——只是它已被批准访问该帐户。这通常是通过authorized_keysGit 用户的文件完成的,当您添加和删除用户时,Gerrit 或 Gitolite 等工具会管理该文件。Authorized_keys 文件允许指定在经过身份验证时运行的特定命令,这些工具使用该功能与用户进行通信——然后应用程序从那里确定权限。
为什么 git 这样做,以及 git 如何知道它应该使用哪个密钥对来验证连接。
这里有一点误解。其中一些与其说是 Git,不如说是 SSH。像 Git 这样的工具使用 SSH 作为传输,因为它完成了身份验证、保护网络活动的艰巨工作,并且拥有像 ssh-agent 这样的工具来简化身份验证。为什么要重新发明轮子?
密钥对实际上是由两种方式之一确定的:您在您的~/.ssh/config(这就是我所做的)中指定它,或者您ssh遍历可用的密钥并找出它。如果管理员设置了严格的规则,后者可能会导致问题,因为任何不起作用的密钥都将被视为身份验证尝试。大多数人只有一把钥匙,所以这通常不是问题。
您可以通过使用ssh -v git@github.com或您要反对的任何服务器来看到一些这种协商的发生:
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /Users/jszakmeister/.ssh/config
debug1: /Users/jszakmeister/.ssh/config line 286: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Control socket "/tmp/git@github.com:22" does not exist
debug1: Connecting to github.com [192.30.255.113] port 22.
debug1: Connection established.
debug1: identity file /Users/jszakmeister/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jszakmeister/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version libssh_0.7.0
debug1: no match: libssh_0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/jszakmeister/.ssh/known_hosts:25
Warning: Permanently added the RSA host key for IP address '192.30.255.113' to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/jszakmeister/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.255.113]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
PTY allocation request failed on channel 0
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi jszakmeister! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Connection to github.com closed.
Transferred: sent 2988, received 1868 bytes, in 0.2 seconds
Bytes per second: sent 19242.2, received 12029.6
debug1: Exit status 1
Run Code Online (Sandbox Code Playgroud)
我不确定这是否完全回答了您的问题,但我希望它有所帮助。
更新
SSH 是一个非常强大的工具,它也有很多配置选项。您可以做的一件事让生活更轻松,那就是设置您的配置以应用您要使用的用户名和密钥文件:
~/.ssh/config
Host gh
Username git
Hostname github.com
IdentityFile ~/.ssh/id_github
Run Code Online (Sandbox Code Playgroud)
您可以使用类似上面的内容来允许git clone gh:foo/bar.git本质上意味着git clone git@github.com:foo/bar.git并使用不同的 SSH 密钥进行身份验证。我一直在使用这个功能,而不仅仅是与 Git 相关的东西。
| 归档时间: |
|
| 查看次数: |
2809 次 |
| 最近记录: |