Git over ssh 忽略了 ssh 配置

问题描述

所以,我一直在纠结这个问题,但 git 仍然在起作用。虽然 ssh 现在工作得很好,但 git 似乎忽略了我的 ssh 配置,但我至少知道当我在相关存储库的 sshCommand 中使用 .git/config 指定端口和密钥时它可以工作。>

使用配置文件,纯 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 配置文件,我想这并不奇怪。

如果我在 debug 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 客户端。

就目前而言,我只需要为每个 repo sshCommand 配置添加一个 git,但是,通过这样做,当 ssh 配置不常使用时,实际上拥有一个 ssh 配置几乎没有意义由于 Github 不支持 shell 访问,并且在工作中,我的机器目前不用于任何其他适当的 ssh-ing。

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

解决方法

正如我所假设的,这是基于 hosthostname 之间与 git 遥控器的性质的误解。虽然老实说,我并没有真正找到正确解释这就是他们彼此互动的方式。

就像我在第一篇文章中提到的那样,我必须将 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@<ssh config host>:<username on remote>/<remote repo name>,这是因为当它调用 .ssh/config host 时,它实际上读取了 hostname 配置下定义的 host

最后要注意的一点是,在我的情况下,由于我们的网络是如何设置的以及 VPN 如何与之交互,我仍然需要使用 ssh.github.com 作为 hostname 和端口 { {1}} 才能连接。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...