Git 通过 ssh 忽略 ssh 配置

Git over ssh is ignoring the ssh config

所以,我已经弄乱了一段时间了,但 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 存储库中播放到遥控器,因为我也需要在那里附加 ssh.git remote add orgin git@github.com:user/test.git 不起作用,但 git remote origin2 git@ssh.github.com:user/test.git 起作用。我测试了 git 是否可以通过 运行ning git remote show <remote> 连接并将 <remote> 替换为 originorigin2.

但是,如果不指定 sshCommandsshCommand = 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 <remote> 来获得尽可能详细的信息,并且如前所述,普通 ssh 完全可以正常工作。当命令为 运行 时,无论我在目录树中的哪个位置,无论我正在调用哪个 host,它都可以正常工作。这就是为什么我感到困惑,因为当 git 参与该过程时它不起作用。

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

现在,我只需为每个回购 git 配置添加一个 sshCommand由于 Github 不支持 shell 访问并且在工作中我的机器目前没有用于任何其他适当的 ssh-ing。

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

如我所料,这是基于 hosthostnamegit 遥控器之间本质的误解。老实说,我并没有真正找到正确的解释,这就是他们彼此互动的方式。

就像我在第一个 post 中提到的,我必须将 ssh. 附加到其他正常的 git@github.com:user/repo,但是没有必要使用 github.comssh.github.com 在远程 link 因为它将在您的 .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@<ssh config host>:<username on remote>/<remote repo name>,这是因为它调用.ssh/confighost时,实际上读取的是host配置下定义的hostname

最后要注意的一件事是,就我而言,由于我们的网络设置方式以及 VPN 与其交互的方式,我仍然需要使用 ssh.github.com 作为 hostname 和端口 443 才能连接。