gpt4 book ai didi

git - 为什么git使用ssh使用git作为用户名

转载 作者:行者123 更新时间:2023-12-02 13:46:51 25 4
gpt4 key购买 nike

这只是关于 git 如何运行的一般问题。我在 git 手册中找不到与此相关的任何内容,也许我没有找对地方。

当您使用来自 git 服务器的 ssh 链接进行克隆时,它使用的用户名是 git,而不是您尝试使用的 key 的用户名。为什么 git 这样做以及 git 如何判断它应该使用哪个 key 对来验证连接。

最佳答案

When you clone using an ssh link from a git server, the username it uses is git, not the username of the key you are trying to use.


正确的。原因是该服务与用户帐户绑定(bind),您需要以该用户身份访问服务器才能调用该服务。这实际上是 SSH 而不是 Git 的一个特性——Git 只是使用 SSH 作为传输。
此外,SSH——因此 Git——对与 SSH key 关联的用户一无所知——只是它已被批准访问该帐户。这通常通过 authorized_keys 来完成。 Git 用户的文件,Gerrit 或 Gitolite 等工具会在您添加和删除用户时管理该文件。 authorized_keys 文件允许指定在经过身份验证时运行的特定命令,并且这些工具使用该功能与用户交流 - 然后应用程序从那里确定权限。

Why does git do this and how is git able to tell which keypair that it should use to authenticate the connection.


这里有点误解。其中一些与其说是 Git,不如说是 SSH。像 Git 这样的工具使用 SSH 作为传输,因为它完成了身份验证、保护网络事件的艰苦工作,并且拥有像 ssh-agent 这样的工具来简化身份验证。为什么要重新发明轮子?
key 对实际上是通过以下两种方式之一确定的:您在 ~/.ssh/config 中指定它。 (这是我做的),或者你让 ssh遍历可用的键并弄清楚。如果管理员设置了严格的规则,后者可能会导致问题,因为任何不起作用的 key 都将被视为身份验证尝试。大多数人没有超过一把 key ,所以一般来说这不是问题。
您可以通过使用 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
我不确定这是否完全回答了您的问题,但我希望它有所帮助。
更新
SSH 是一个非常强大的工具,它也有很多配置选项。为了让生活更轻松,您可以做的一件事是设置您的配置以应用您要使用的用户名和 key 文件:
~/.ssh/config
Host gh
User git
Hostname github.com
IdentityFile ~/.ssh/id_github
你可以使用类似上面的方法来允许 git clone gh:foo/bar.git本质上是指 git clone git@github.com:foo/bar.git并使用不同的 SSH key 进行身份验证。我一直都在使用这个功能,而不仅仅是与 Git 相关的东西。

关于git - 为什么git使用ssh使用git作为用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47664768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com