【问题标题】:Git over ssh is ignoring the ssh configGit over ssh 忽略了 ssh 配置
【发布时间】:2021-02-11 20:33:56
【问题描述】:

所以,我已经搞砸了一段时间,但 git 仍然在起作用。虽然 ssh 现在工作得很好,但 git 似乎忽略了我的 ssh 配置,但我至少知道当我在相关存储库的 .git/config 中使用 sshCommand 指定端口和密钥时它可以工作。

纯 ssh 使用配置文件可以正常工作,但是,我使用的是 Github 部署密钥,所以我的 ssh 配置看起来更像这样:

host test
        Hostname ssh.github.com
        User git
        Port 443
        IdentityFile ~/.ssh/id_ed25519-test.github
        IdentitiesOnly yes
host test2
        Hostname ssh.github.com
        User git
        Port 443
        IdentityFile ~/.ssh/id_ed25519-test2.github
        IdentitiesOnly yes

使用 ssh 连接仍然没有任何问题,但我认为我的问题归结为我对主机和主机名如何工作的一些误解,可能?不太确定,在 ssh_config 和 ssh 的 man 页面上没有什么特别突出的。

由于工作相关的原因,主要是我们的VPN有点不稳定,我需要使用443端口。

奇怪的是,ssh 不能使用github.com 作为Hostname,只有当它是ssh.github.com。我在连接它时遇到了问题,只是在 Github 文档的一个示例中遇到了这个问题,但没有提到。对其进行了测试,此后一直运行良好。

这也适用于 git repo 中的远程,因为我也需要在那里附加 ssh.git remote add orgin git@github.com:user/test.git 不起作用,但 git remote origin2 git@ssh.github.com:user/test.git 起作用。我已经通过运行git remote show <remote> 并将<remote> 替换为originorigin2 来测试git 是否可以连接。

但是,如果没有将sshCommand 指定为sshCommand = ssh -p 443 -i ~/.ssh/id_ed25519-test.github,它将无法连接。

我试过用这个调试:

GIT_TRACE=true \                                                                  at 11:17:27
GIT_CURL_VERBOSE=true \
GIT_SSH_COMMAND="ssh -vvv" \
GIT_TRACE_PACK_ACCESS=true \
GIT_TRACE_PACKET=true \
GIT_TRACE_PACKFILE=true \
GIT_TRACE_PERFORMANCE=true \
GIT_TRACE_SETUP=true \
GIT_TRACE_SHALLOW=true \
git remote show <remote>

在使用 git 时,虽然我有时会删除其中的 GIT_SSH_COMMAND="ssh -vvv" \ 部分,因为它似乎覆盖了 git 配置文件,我想这并不奇怪。

如果我在调试 ssh_command 中指定密钥,它至少会选择那个,但它仍在尝试通过端口 22 连接到 Github。这是日志:

11:20:17.116821 trace.c:375             setup: git_dir: .git
11:20:17.116867 trace.c:376             setup: git_common_dir: .git
11:20:17.116872 trace.c:377             setup: worktree: /home/user/code/test
11:20:17.116874 trace.c:378             setup: cwd: /home/user/code/test
11:20:17.116877 trace.c:379             setup: prefix: (null)
11:20:17.116879 git.c:439               trace: built-in: git remote show test
11:20:17.117075 run-command.c:663       trace: run_command: unset GIT_PREFIX; 'ssh -vvv' git@ssh.github.com 'git-upload-pack '\''github-user/test.git'\'''
OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /home/user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolving "ssh.github.com" port 22
debug2: ssh_connect_direct
debug1: Connecting to ssh.github.com [140.82.121.35] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/id_rsa type -1
debug1: identity file /home/user/.ssh/id_rsa-cert type -1
debug1: identity file /home/user/.ssh/id_dsa type -1
debug1: identity file /home/user/.ssh/id_dsa-cert type -1
debug1: identity file /home/user/.ssh/id_ecdsa type -1
debug1: identity file /home/user/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/user/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/user/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/user/.ssh/id_ed25519 type -1
debug1: identity file /home/user/.ssh/id_ed25519-cert type -1
debug1: identity file /home/user/.ssh/id_ed25519_sk type -1
debug1: identity file /home/user/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/user/.ssh/id_xmss type -1
debug1: identity file /home/user/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1

如您所见,它完全跳过了~/.ssh/config 定义。即使我在 ssh 命令中指定它,它也会忽略它。这就是让我相信我对 hosthostname 的工作原理有一些误解,或者有一些不言而喻的行为导致了这种情况。

我还使用了ssh -vvvT &lt;remote&gt; 来获得尽可能多的详细信息,并且如前所述,普通 ssh 完全可以正常工作。无论运行命令时我在目录树中的哪个位置,无论我调用的是哪个host,它都能正常工作。这就是为什么我很困惑,因为当git 参与该过程时它不起作用。

也只安装了 OpenSSH,所以我认为它不应该是一个冲突的 ssh 客户端。

现在,我只需为每个 repo git 配置添加一个 sshCommand 就可以了,但是,这样做实际上有一个 ssh 配置没有什么意义,因为 Github 没有t 支持 shell 访问并且在工作中我的机器目前不用于任何其他正确的 ssh-ing。

在过去两周左右的时间里,我一直在为这个问题摸不着头脑,但我已经放弃了一半,并决定至少尝试发布一个关于这个的问题,因为其他人都喜欢这个(标题至少)没有解决我的问题。希望这里有人知道出了什么问题并可以告诉我如何解决它,或者至少将我指向一些文档或解释我做错了什么的东西。

【问题讨论】:

  • 提示:当你在~/.ssh/config 中使用host test2 时,你不应该以某种方式直接使用gitssh 使用test2 吗?
  • 好吧,我尝试将遥控器设置为相同的 host 名称,并尝试使用仅包含一个 host 的配置文件并在 sshCommand 中指定它以确保,但这仍然没用。如果那是你的意思?或者你的意思是反过来,在 ssh 配置中指定 git?
  • Git 实际上只是 runs ssh 来实现 SSH-ness。如果 Git 正在运行的 ssh 的行为与您从命令行运行的 ssh 的行为方式不同,我首先怀疑您的机器上安装了两个或多个不同的 ssh 程序,以及 Git 运行时的 Git 路径当您运行 ssh 时,ssh 与您的路径不同,因此 Git 正在运行 ssh 但您正在运行 ssh。这就像参加一个所有人都叫布鲁斯的派对。
  • @WidgetMidget 第一次使用ssh -Tv testssh -Tv test2 开始调试
  • run_command: unset GIT_PREFIX; 'ssh -vvv' git@ssh.github.com 根本不运行 ssh test。为什么不做Host ssh.github.com 部分?为什么命名为“test”和“test2”

标签: git windows-subsystem-for-linux git-config ssh-config deploy-keys


【解决方案1】:

正如我所假设的,这是基于hosthostnamegit 遥控器之间的性质的误解。虽然老实说,我并没有真正找到正确解释这就是它们相互交互的方式。

就像我在第一篇文章中提到的,我必须将ssh. 附加到其他正常的git@github.com:user/repo,但是没有必要在远程链接中使用github.comssh.github.com,因为它将被定义在你的.ssh/config

如果您的.ssh/config 是:

host test
        Hostname ssh.github.com
        User git
        Port 443
        IdentityFile ~/.ssh/id_ed25519-test.github
        IdentitiesOnly yes

你的 GitHub 用户名是 user,你的仓库是 test,你的 git remote 不会是 git@github.com:user/test.gitgit@ssh.github.com:user/test.git。它将是git@test:user/test(在本例中)。换句话说:git@&lt;ssh config host&gt;:&lt;username on remote&gt;/&lt;remote repo name&gt;,这是因为当它调用.ssh/confighost时,它实际上读取的是host配置下定义的hostname

最后要注意的是,就我而言,由于我们的网络是如何设置的以及 VPN 如何与之交互,我仍然需要使用 ssh.github.com 作为 hostname 和端口 443可以连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2018-08-02
    • 2016-03-25
    • 1970-01-01
    相关资源
    最近更新 更多