【问题标题】:Why does git using ssh use git as a username为什么git使用ssh使用git作为用户名
【发布时间】:2018-05-19 18:32:05
【问题描述】:

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

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

【问题讨论】:

  • 通常有服务于存储库。该服务必须与某些用户一起运行,而不是 root。有一种约定说用户名与服务相同。例如,经常看到 apache 使用 apache 用户运行,ftp 使用 ftp,nginx 使用 nginx 等等。所以人们也使用 git。您可以创建一个用户名 TaylorSwift,但不太容易掌握该用户的用途......
  • 不一定是“git”。这对密钥的用户名通常是您在本地机器上登录的用户名。一些远程服务是由一个用户运行的,这个用户在 Github 上的名字是“git”。本地用户的公钥被发送到“git”,以便本地用户可以使用用户名“git”访问服务。对于 Gerrit 等其他托管服务,每个用户都会注册一个用户名,该用户名通常与本地用户名相同。

标签: git github ssh


【解决方案1】:

当您使用来自 git 服务器的 ssh 链接进行克隆时,它使用的用户名是 git,而不是您尝试使用的密钥的用户名。

正确。原因是该服务与用户帐户绑定,您需要以该用户身份访问服务器才能调用该服务。这实际上是 SSH 而不是 Git 的一个特性——Git 只是使用 SSH 作为传输。 此外,SSH——因此 Git——对与 SSH 密钥关联的用户一无所知——只是它已被批准访问该帐户。这通常是通过 Git 用户的 authorized_keys 文件完成的,而 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

我不确定这是否完全回答了你的问题,但我希望它会有所帮助。

更新

SSH 是一个非常强大的工具,它也有很多配置选项。为了让生活更轻松,您可以做的一件事是设置您的配置以应用您要使用的用户名和密钥文件:

~/.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 密钥进行身份验证。我一直都在使用这个功能,而不仅仅是与 Git 相关的东西。

【讨论】:

  • 谢谢,这确实有帮助。我已经使用 git 多年了,这只是它的一个小功能,一直让我感到困惑。
  • @MattG。我很高兴它有帮助!当我第一次看到类似的东西时,我肯定很困惑。这是解决问题的好方法,但用户名位令人困惑。另外,我将更新我的答案以显示 ssh 的示例配置,它可以让您创建主机名并为您应用用户名。至少那时你不必看它。 :-)
  • 最后的 extra 部分非常有用。我知道我们不应该在这里说谢谢,但该死的,谢谢。
【解决方案2】:

不要忘记,您实际上无法在远程 Git 存储库托管服务器上打开 交互式安全 shell:

  1. 您没有在该服务器上执行任何您想要的命令的业务
  2. 您的帐户永远不会被创建(因为 1.)
  3. 您应该感兴趣的唯一命令是 Git 命令(upload-pack、receive-pack,用于实现您正在执行的 git clone/pull/push。)

出于所有这些原因,您需要为其请求远程 shell 的唯一帐户是管理这些 Git 远程存储库的管理帐户,通常命名为“git”。

其中一些服务器会使用名为“强制命令”的 ssh 功能(我 presented it here for Gitolite,但它也适用于 GitLab)
这意味着它知道您的帐户名称,因为它是作为与~git/.ssh/authorized_keys 中的公钥关联的强制命令的参数写入的,它跟踪所有被授权打开(非交互式)shell 的公钥。
这就是 Git 托管服务知道哪些帐户正在发出 Git 请求的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2010-10-08
    • 1970-01-01
    相关资源
    最近更新 更多