为什么 git 不使用 ssh 配置文件中设置的身份?

为什么 git 不使用 ssh 配置文件中设置的身份?

我正在尝试连接到本地希泰亚服务器。我已将其设置为使用端口 2222 上的集成 SSH 服务器。我运行的是 Windows。Gitea 运行良好。

现在我想使用Cygwin 的 git。为了测试与我的存储库的连接,我使用ls-远程如果我使用命令,该命令可以正常工作GIT_SSH_COMMAND像这样的选项:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa" git ls-remote --exit-code -h ssh://username@localhost:2222/username/Repo.git

接下来我想使用以下方法来简化生活~/.ssh/config

host gitea
 HostName localhost
 Port 2222
 IdentityFile ~/.ssh/id_rsa
 User username

但是,这确实会失败并出现错误Unable to open connection

git ls-remote --exit-code -h ssh://gitea/username/Repo.git

问题:IdentityFile不适用。这有效:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa" git ls-remote --exit-code -h ssh://gitea/username/Repo.git

但我确信我的猜想~/.ssh/config是正确的,因为直接连接ssh -vv gitea是可行的。输出(摘录):

[...]
debug1: Connecting to localhost [::1] port 2222.
debug1: Connection established.
[...]
debug1: Offering public key: RSA SHA256:XXX /home/username/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug2: input_userauth_pk_ok: fp SHA256:XXX
debug1: Authentication succeeded (publickey).
Authenticated to localhost ([::1]:2222).
[...]

那么为什么git不使用IdentityFilefrom呢~/.ssh/config

答案1

事实证明,该设置GIT_SSH_COMMAND="ssh"已经足够了。这意味着git正在使用其他 SSH 客户端。可能是恰好位于系统路径上的 OpenSSH 客户端:

$ whereis ssh
ssh: /usr/bin/ssh.exe /cygdrive/c/WINDOWS/System32/OpenSSH/ssh.exe /usr/share/man/man1/ssh.1.gz

我通过添加解决了这个export GIT_SSH_COMMAND="/usr/bin/ssh"问题~/.bash_profile

相关内容